#!/usr/bin/env bash

dir=`dirname $0`

if [ $?SSW ]
then
    sdir=${SSW}/packages/panorama/binaries
else
    sdir=/net/solserv/data1/dynamo/packages/panorama/binaries
fi


if [ ${dir} != ${sdir} ]
then
    # Updated cover script available?
    if [[ -f ${sdir}/plauncher  &&  ${sdir}/plauncher -nt ${dir}/plauncher ]]
    then
        echo -n "Installing updated Panorama launch script..."
        /bin/mv -f ${dir}/plauncher ${dir}/plauncher-prev
        /bin/cp -f ${sdir}/plauncher ${dir}/plauncher
        chmod 755 ${dir}/plauncher
        echo "done!"

        # Now relaunch using the new cover script
        exec ${dir}/plauncher $*
    fi


    # Updated binary available?
    if [[ -f ${sdir}/panorama_launcher.jar  &&  ${sdir}/panorama_launcher.jar -nt ${dir}/panorama_launcher.jar ]]
    then
        # Yep.  Move the old one aside and copy in the new one.
        echo -n "Installing updated Panorama launcher .jar..."
        /bin/mv -f ${dir}/panorama_launcher.jar ${dir}/panorama_launcher.jar-prev
        /bin/cp -f ${sdir}/panorama_launcher.jar ${dir}/panorama_launcher.jar
        echo "done!"
    fi

    # Updated Annotator available?
    if [[ -f ${sdir}/annotator_gui.jar  &&  ${sdir}/annotator_gui.jar -nt ${dir}/annotator_gui.jar ]]
    then
        # Yep.  Move the old one aside and copy in the new one.
        echo -n "Installing updated Annotator .jar..."
        /bin/mv -f ${dir}/annotator_gui.jar ${dir}/annotator_gui.jar-prev
        /bin/cp -f ${sdir}/annotator_gui.jar ${dir}/annotator_gui.jar
        echo "done!"
    fi
fi


exec java -jar ${dir}/panorama_launcher.jar $*


