#!/bin/bash
# This command takes a directory of HTCondor RPMs and creates a tarball
# Named externals are download and extracted from existing repositories
#
# exit on any error
set -e

if [ $# -ne 1 ]; then
    echo 'Error: missing argument'
    echo "Usage: $(basename $0) RPMs-directory"
    exit 1
fi
rpmdir=$1
if [ ! -d $rpmdir ]; then
    echo "Error: $rpmdir is not a directory"
    echo "Usage: $(basename $0) RPMs-directory"
    exit 1
fi

extract_rpm() {
    what=$1
    where=$2
    rpm2cpio $what | (cd $where; cpio -imd --no-absolute-filenames)
}

cmd_dir=$(dirname $0)

mkdir tarball

for file in $rpmdir/*.rpm; do
    if [[ $file == *.src.rpm ]]; then
        # Skip src RPM
        echo Skipping $file
    elif [[ $file == *-debuginfo-* ]]; then
        # Skip debug information
        echo Skipping $file
    elif [[ $file == *-debugsource-* ]]; then
        # Skip debug source
        echo Skipping $file
    elif [[ $file == */python2-* ]]; then
        # Skip Python 2 bindings
        echo Skipping $file
    elif [[ $file == *-credmon-ouath* ]]; then
        # We only want sbin/condor_credmon_ouath for tests, for now,
        # but that's in the credmon-local package for some reaosn.
        echo Skipping $file
    elif [[ $file == *-credmon-vault* ]]; then
        echo Skipping $file
    else
        eval $(perl -e "(\$package, \$version, \$release, \$dist, \$arch) = '$file' =~ m:^.*/(.*)-([0-9][0-9.]*)-([0-9][0-9.]*)\.([^.]+)\.([^.]+)\.rpm$:; print \"package=\$package version=\$version release=\$release dist=\$dist arch=\$arch\";")
        echo ======= $package-$version-$release.$dist.$arch.rpm =======
        echo $package-$version-$release.$dist.$arch.rpm >> tarball/Manifest.txt
        extract_rpm $rpmdir/$package-$version-$release.$dist.$arch.rpm tarball
    fi
done

if [[ $dist == amzn2023 ]]; then
    arch_dist="${arch}_AmazonLinux2023"
elif [[ $dist == el7 ]]; then
    arch_dist="${arch}_CentOS7"
elif [[ $dist == el8 ]]; then
    arch_dist="${arch}_AlmaLinux8"
elif [[ $dist == el9 ]]; then
    arch_dist="${arch}_AlmaLinux9"
elif [[ $dist == fc38 ]]; then
    arch_dist="${arch}_Fedora38"
elif [[ $dist == fc39 ]]; then
    arch_dist="${arch}_Fedora39"
elif [[ $dist == fc40 ]]; then
    arch_dist="${arch}_Fedora40"
elif [[ $dist == leap15 ]]; then
    arch_dist="${arch}_openSUSE15"
else
    echo "Platform not defined for $dist"
    exit 1
fi

# Blow away whatever BaTLab made
rm -rf condor_tests-*
# Repack the testing tarball
tar xfp tarball/usr/lib64/condor/condor_tests-$version.tar.gz
rm tarball/usr/lib64/condor/condor_tests-$version.tar.gz
mv condor_tests-* condor_tests-$version-$release-$arch_dist
(cd condor_tests-$version-$release-$arch_dist; chmod a+x $(file $(find -type f)|grep 'executable'|sed -e s/:.*//))
(cd condor_tests-$version-$release-$arch_dist; chmod a+x $(find -type f -name script\*))
$cmd_dir/set-tarball-rpath condor_tests-$version-$release-$arch_dist '$ORIGIN/../lib64'
if [ $? -ne 0 ];then
    echo 'ERROR: set rpath script failed'
    exit 1
fi
tar cfz condor_tests-$version-$release-$arch_dist.tar.gz condor_tests-$version-$release-$arch_dist
rm -r condor_tests-$version-$release-$arch_dist

# Extract external RPMs
for path in $(find /usr/local/condor/externals -name \*.rpm); do
    file=${path##*/}
    echo ======= $file =======
    echo $file >> tarball/Manifest.txt
    extract_rpm $path tarball
done

$cmd_dir/make-tarball-links tarball
if [ $? -ne 0 ];then
    echo 'ERROR: tarball link script failed'
    exit 1
fi
$cmd_dir/set-tarball-rpath tarball '$ORIGIN/../lib64:$ORIGIN/../lib64/condor'
if [ $? -ne 0 ];then
    echo 'ERROR: set rpath script failed'
    exit 1
fi

# Move configuration files back to example directory
mv tarball/etc/condor/config.d/* tarball/examples/

# Fixup directory permissions for the tarball
chmod 755 tarball/etc/condor/passwords.d
chmod 755 tarball/etc/condor/tokens.d
chmod 755 tarball/var/lib/condor/execute
chmod 755 tarball/var/lib/condor/oauth_credentials
chmod g-s tarball/var/lib/condor/oauth_credentials

# Add in apptainer if available
if [ -d /usr/local/condor/externals/apptainer ]; then
    /usr/local/condor/externals/apptainer/libexec/apptainer --version
    cp -a /usr/local/condor/externals/apptainer/* tarball/usr
    tarball/usr/libexec/apptainer --version >> tarball/Manifest.txt
fi

echo ======= Manifest.txt =======
cat tarball/Manifest.txt

# Package final tarball
mv tarball condor-$version-$release-$arch_dist-stripped
tar --create --gzip --owner=0 --group=0 --numeric-owner --file=condor-$version-$release-$arch_dist-stripped.tar.gz condor-$version-$release-$arch_dist-stripped
rm -r condor-$version-$release-$arch_dist-stripped

ls -lh condor*stripped.tar.gz
exit 0
