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

cd $tarball

if [ ! -d etc -o ! -d usr -o ! -d var ]; then
    echo That does not look like an HTCondor tarball
    exit 1
fi
# On Debian and Unbuntu consolidate shared objects into a single directory
if [ -d lib ]; then
    find lib -name \*.so\* -exec mv {} usr/lib \;
    rm -rf lib
fi
if [ -d usr/lib/x86_64-linux-gnu ]; then
    find usr/lib/x86_64-linux-gnu -name \*.so\* -exec mv {} usr/lib \;
fi
# Make compatabilty links to conform to the old tarball layout
ln -s usr/bin bin
ln -s usr/sbin sbin
ln -s usr/lib lib
if [ -d usr/lib64 ]; then
    ln -s usr/lib64 lib64
fi
if [ -d usr/libexec/condor ]; then
    ln -s usr/libexec/condor libexec
fi
ln -s usr/share/man man
ln -s usr/include include
ln -s sbin/condor_configure condor_configure
ln -s sbin/condor_install condor_install
if [ -d usr/share/doc/condor/examples/etc ]; then
    # Debian and Ubuntu
    ln -s usr/share/doc/condor/examples/etc examples
    cp -p /usr/share/common-licenses/Apache-2.0 LICENSE
    ln -s usr/share/doc/condor/NOTICE.txt NOTICE.txt
elif [ -d usr/share/doc/condor/examples ]; then
    # Modern RPM
    ln -s usr/share/doc/condor/examples examples
    ln -s usr/share/doc/condor/LICENSE LICENSE
    ln -s usr/share/doc/condor/NOTICE.txt NOTICE.txt
elif [ -d usr/share/doc/packages/condor/examples ]; then
    # openSUSE
    ln -s usr/share/doc/packages/condor/examples examples
    ln -s usr/share/doc/packages/condor/LICENSE LICENSE
    ln -s usr/share/doc/packages/condor/NOTICE.txt NOTICE.txt
else
    # Old RPM
    ln -s usr/share/doc/condor-[0-9]*/examples examples
    ln -s usr/share/doc/condor-[0-9]*/LICENSE LICENSE
    ln -s usr/share/doc/condor-[0-9]*/NOTICE.txt NOTICE.txt
fi
ln -s ../examples etc/examples
if [ -d usr/lib64 ]; then
    # RPM based systems
    # Link to pickup condor_ssh_to_job_template
    ln -s ../lib64/condor/condor_ssh_to_job_sshd_config_template usr/lib/condor_ssh_to_job_sshd_config_template
    # Link to pickup libgetpwnam.so
    ln -s ../lib64/condor/libgetpwnam.so usr/lib/
    # Link for executables to pick up shared libaries
    ln -s ../lib64 usr/libexec/lib64
    # Links for Java Universe
    (cd usr/lib; ln -s ../share/condor/*.class .)
    # Standard place for Python3 packages)
    (cd lib; ln -s ../lib64/python3*/site-packages python3)
    # Link for Python to pick up shared libaries
    (cd usr/lib64/python3*/site-packages; ln -s ../.. lib64)
else
    # Debian based systems
    # Link to pickup condor_ssh_to_job_template
    ln -s condor/condor_ssh_to_job_sshd_config_template usr/lib/condor_ssh_to_job_sshd_config_template
    # Link for executables to pick up shared libaries
    ln -s .. usr/lib/condor/lib
    # Links for Java Universe
    (cd usr/lib; ln -s condor/*.class .)
    # Standard place for Python3 packages)
    (cd lib; mv python3 tmp; mv tmp/dist-packages python3; rmdir tmp)
    # Link for Python to pick up shared libaries
    (cd usr/lib/python3; ln -s .. lib)
    # Remove the python2 packages
    rm -rf usr/lib/python2*
fi
# Add links and configuration for Bosco
if [ ! -f etc/blah.config ]; then
    # Fedora does not have blahp (yet)
    exit 0
fi
# batch_gahp -> blahp compatibility links
ln -s blahpd usr/bin/batch_gahp
ln -s blahpd_daemon usr/sbin/batch_gahp_daemon
# Construct a glite directory for Bosco
if [ -d usr/libexec/condor ]; then
    ln -s ../blahp usr/libexec/condor/blahp
fi
# Comment out default blahp location
sed -i -e s/blah_libexec_directory/#blah_libexec_directory/ etc/blah.config
# Make absolute links relative
(cd usr/libexec/blahp; ln -sf ../../../etc/blahp/*_local_submit_attributes.sh .)
