#!/bin/sh

echo "@@@ Ejecting disk 1 so that wrapping step (.dmg creation) won't suddenly fail"

# ensure disk1 is unmounted
#hdutil eject /dev/disk1
hdiutil eject /dev/disk1

# BASEDIR is the parent folder of envi and release-kits (assuming they share a parent folder)
#BASEDIR=/Users/Shared/RELEASES
#BASEDIR=/Users/greenstone/RELEASES
BASEDIR=/Users/greenstone/gs-release-builder


echo "BASEDIR: $BASEDIR"
# Set up ant and JDK with FX for expeditee
cd "${BASEDIR}/ext-cli"
. ./SETUP-CLI.sh
cd "$BASEDIR" # Is this needed?

wrap() {
    ver=$1

    pushd $BASEDIR
    # source env script that adds release-kits/bin to PATH
    . ./setup-env.sh

    # sourcing setup-env.sh script above already did this:
    #export PATH=$BASEDIR/release-kits/bin:$PATH
    
    # the star at the end does tab-completion
    pushd $BASEDIR/snapshots/gs${ver}-caveat/from*

    # store a copy of the original build log
    mv rk${ver}.out rk${ver}orig.out

    #rk${ver} create-installer
    echo "@@@ About to call rk$ver wrap"    
    rk${ver} wrap

    # concatenate current log to the original build log, overwriting any previous rk${ver}.out
    # http://stackoverflow.com/questions/2150614/bash-shell-scripting-combining-txt-into-one-file
    mv rk${ver}.out rk${ver}wrap.out
    cat rk*.out > rk${ver}.out
    rm rk${ver}wrap.out
    rm rk${ver}orig.out

    # generate the files for upload 
    pushd $BASEDIR/envi/bin
    # upload subroutine appears to have been removed/disabled from snapshot task
    #./envi.pl gs${ver}-caveat upload
    $BASEDIR/upload-files-to-www-internal.sh
    
    # back to lsb root
    #cd /
    popd
    popd
    popd
}


# Set up the correct versions of svn and Java if the correct versions aren't already on the PATH
# However, this is only for any svn and java commands in this file
# The release kit uses envi to set up its environment at
# envi/etc/environment.pl and envi/etc/tasks/snapshot/environment.pl
#export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
#export JAVA_HOME=$BASEDIR/JDK7u80/Home
#echo "@@@ JAVA_HOME set to $JAVA_HOME"
#export PATH=$BASEDIR/subversion/bin:$JAVA_HOME/bin:$PATH
#export HINT=/System/Library/Frameworks/JavaVM.framework/Versions/Current

#if test -e "$BASEDIR/logs/caveats.log" ; then
#    rm "$BASEDIR/logs/caveats.log"
#fi
env > $BASEDIR/logs/caveats.log 2>&1;

# update the release-kits
echo "@@@ Updating the release-kits"
#source $BASEDIR/setenv.sh
svn up $BASEDIR/release-kits


# Finally, generate the caveats
echo "Generating the caveats"

cd $BASEDIR/envi/bin

# if a param is passed in and it's 2, generate GS2 caveat
# if a param is passed in and it's 3, generate GS3 caveat
# if the first param is 'release' and the 2nd is the release number (e.g. 3.09rc2),
# generate a GS2 or GS3 release binary as indicated by the 2nd parameter
# if no param is passed in, or if it's other than 2 or 3 or release or help,
# generate everything (GS2 and GS3 caveats and stables)
if test ! -z "$1" ; then
	if test "$1" == "-h" || test "$1" == "--help" || test "$1" == "-?" ; then
		echo "*************************************************"
		echo "Usage: $0 <1|2|release <gs-release-version>[rc#]>"
		echo "     - $0 by itself will generate caveats for 2 followed by 3"
		echo "     - $0 2 will generate the caveat for 2"
		echo "     - $0 3 will generate the caveat for 3"
		echo "     - $0 release will print out the usage for generating a release"
		echo "     - $0 release 3.09 will generate the 3.09 release binary"
		echo "     - $0 release 3.09rc2 will generate the binary for 3.09 release candidate 2"
		echo "     - $0 -h|--help|-? will print these usage instructions"
		echo "*************************************************"
	elif test "$1" == "2" ; then
	    echo "Caveat 2";
	    if test -e "$BASEDIR/logs/caveats2.log" ; then
		rm "$BASEDIR/logs/caveats2.log"
	    fi
	    if test -z "$2" ; then
		#./envi.pl gs2-caveat >> $BASEDIR/logs/caveats2.log 2>&1;
		./envi.pl gs2-caveat 2>&1 | tee -a $BASEDIR/logs/caveats2.log
	    elif test "$2" != "wrap" ; then
		#./envi.pl gs2-caveat $2 >> $BASEDIR/logs/caveats2.log 2>&1;
		./envi.pl gs2-caveat $2 2>&1 | tee -a $BASEDIR/logs/caveats2.log
	    else
		wrap $1
	    fi
	    
	elif test "$1" == "3" ; then
	    echo "Caveat 3";
	    if test -e "$BASEDIR/logs/caveats3.log" ; then
		rm "$BASEDIR/logs/caveats3.log"
	    fi
	    if test -z "$2" ; then
		#./envi.pl gs3-caveat >> $BASEDIR/logs/caveats3.log 2>&1;
		./envi.pl gs3-caveat 2>&1 | tee -a $BASEDIR/logs/caveats3.log
	    elif test "$2" != "wrap" ; then		
		#echo "*** passing in parameter $2 to envi"
		#./envi.pl gs3-caveat $2 >> $BASEDIR/logs/caveats3.log 2>&1;
		./envi.pl gs3-caveat $2 2>&1 | tee -a $BASEDIR/logs/caveats3.log
	    else
		wrap $1
	    fi
	    
	elif test "$1" == "release" ; then
	    if test -z "$2" ; then
		echo ""
		echo "USAGE: Run as $0 $1 <gs-release-version>[rc#]"
		echo ""
	    else
		if test -e "$BASEDIR/logs/release-"* ; then
		    rm "$BASEDIR/logs/release-"*
		fi
    		echo "$*"; # "Release folder"
		export TASK_NAME=release;
		#./envi.pl $* >> $BASEDIR/logs/release-$2.log 2>&1;
		./envi.pl $* 2>&1 | tee -a $BASEDIR/logs/release-$2.log 
	    fi
	else
	    echo "Unrecognised parameter $1 passed in. Run $0 --help|-h|-? for usage"
	fi
else
    echo "Both caveats";
    #./envi.pl caveats >> $BASEDIR/logs/caveats.log 2>&1;
    ./envi.pl caveats 2>&1 | tee -a $BASEDIR/logs/caveats.log
fi



#./envi.pl caveats >> $BASEDIR/logs/caveats.log 2>&1
#./envi.pl gs3-caveat >> $BASEDIR/logs/caveats.log 2>&1
#./envi.pl gs2-caveat >> $BASEDIR/logs/caveats.log 2>&1

