#!/bin/bash
# This command takes a directory of HTCondor debs 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) debs-directory"
    exit 1
fi
debdir=$1
if [ ! -d $debdir ]; then
    echo "Error: $debdir is not a directory"
    echo "Usage: $(basename $0) debs-directory"
    exit 1
fi

extract_deb() {
    what=$1
    where=$2
    ar x $what
    if [ -f data.tar.gz ]; then
        (cd $where; tar xfpz ../data.tar.gz)
        rm data.tar.gz control.tar.gz
    elif [ -f data.tar.xz ]; then
        (cd $where; tar xfpJ ../data.tar.xz)
        rm data.tar.xz control.tar.xz
    elif [ -f data.tar.zst ]; then
        (cd $where; tar -x --file ../data.tar.zst -p --zstd)
        rm data.tar.zst control.tar.zst
    else
        echo Unknown compression: $file
        exit 1
    fi
}

cmd_dir=$(dirname $0)

mkdir tarball

. /etc/os-release

for file in $debdir/*.deb; do
    if [[ $file == *-dbg_* ]]; then
        # Skip debug information
        echo Skipping $file
    else
        eval $(perl -e "(\$package, \$version, \$release, \$arch) = '$file' =~ m:^.*/(.*)_([0-9][0-9.]*)-([0-9][0-9.]*)_([^.]+)\.deb$:; print \"package=\$package version=\$version release=\$release arch=\$arch\";")
        echo ======= ${package}_$version-${release}_$arch.deb =======
        extract_deb $debdir/${package}_$version-${release}_$arch.deb tarball
        echo ${package}_$version-${release}_$arch.deb >> tarball/Manifest.txt
    fi
done

arch=$(uname -m)

if [ $VERSION_CODENAME = 'bullseye' ]; then
    arch_dist="${arch}_Debian11"
elif [ $VERSION_CODENAME = 'bookworm' ]; then
    arch_dist="${arch}_Debian12"
elif [ $VERSION_CODENAME = 'focal' ]; then
    arch_dist="${arch}_Ubuntu20"
elif [ $VERSION_CODENAME = 'jammy' ]; then
    arch_dist="${arch}_Ubuntu22"
elif [ $VERSION_CODENAME = 'chimaera' ]; then
    arch_dist="${arch}_Devuan4"
else
    echo 'Platform not defined'
    exit 1
fi

# Blow away whatever BaTLab made
rm -rf condor_tests-*
# Repack the testing tarball
tar xfp tarball/usr/lib/condor/condor_tests-$version.tar.gz
rm tarball/usr/lib/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/../lib'
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

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

# Extract external debs
for path in /usr/local/condor/externals/*.deb; do
    file=${path##*/}
    echo ======= $file =======
    extract_deb $path tarball
    echo $file >> tarball/Manifest.txt
done

# Make necessary?? directories
mkdir -p tarball/etc/condor/passwords.d
mkdir -p tarball/etc/condor/tokens.d
mkdir -p tarball/var/lib/condor/execute
mkdir -p tarball/var/lib/condor/oauth_credentials

$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/../lib:$ORIGIN/../lib/condor'
if [ $? -ne 0 ];then
    echo 'ERROR: set rpath script failed'
    exit 1
fi

# Add in apptainer if available
if [ -d /usr/local/condor/externals/apptainer ]; then
    /usr/local/condor/externals/apptainer/bin/apptainer --version
    cp -a /usr/local/condor/externals/apptainer/* tarball/usr
    tarball/bin/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
