#!/bin/bash

version=$(grep -i 'ebe - version' html/about.html | cut -d ' ' -f 5)
file=~/sf/qtebe/Linux/ubuntu-14.10.ebe.${version}.tgz

if [ -e $file ]
then
    echo "Version $version has already been used.  Bump it?"
    exit 1
fi

exe=${1:-ebe}

mkdir -p $exe.d
rm -f $exe.d/*
mkdir -p $exe.d/plugins
name=$(uname)
if [[ $name = Darwin ]]
then
    ./build_package_mac
    exit 0
fi

ldd $exe | while read x
do
    set -- $x
    if [[ $# == 4 ]]
    then
        cp $3 $exe.d
    fi
done

cp /usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/* ebe.d/plugins

cp $exe $exe.d/$exe.exe
cp *.qm $exe.d

cat > $exe.d/README << EOF1
            Installation instructions for $exe

Select a directory to install $exe and execute the following command
from within this directory:

    sh ./install directory_to_install

This will install a shell script named $exe into the chosen directory
and will copy all required shared object files into another directory
named $exe.d in that same installation directory.  If you leave off
the installation directory name, it will install in /usr/local/bin.
EOF1

cat > $exe.d/$exe.init << EOF2
#!/bin/bash
export LD_LIBRARY_PATH=XXX
exec DDD/$exe.d/$exe.exe "\$@"
EOF2

chmod a+x $exe.d/$exe.exe

cat > $exe.d/install << EOF3
#!/bin/bash

dir=\${1:-/usr/local/bin}
mkdir -p \$dir/$exe.d
sed -e "s:XXX:\$dir/$exe.d:" -e "s:DDD:\$dir:" < $exe.init > \$dir/$exe
cp -p * \$dir/$exe.d
chmod a+rx \$dir/$exe \$dir/$exe.d/$exe.exe
chmod a+r \$dir/$exe.d/*
cd \$dir/$exe.d
for lib in lib*.[0-9]*
do
    ln -sf \$lib \$(echo \$lib | sed -e 's/\.[[0-9]]*\$//')
done
rm $exe.init
EOF3

chmod a+rx $exe.d/install

tar cvzf $file $exe.d

