#!/bin/bash

# This must be run in lsb environment

echo `date`


if [ "x$1" != "xwrap" ]; then
    echo "RUNNING GS2 CAVEAT at " `date` > out.txt
    env >> out.txt
    cd /greenstone/gs-release-builder/envi/bin
    ./envi.pl gs2-caveat $*
else

# REDO the WRAP step, in case it failed
# - prepend release-kits/bin to PATH (and also have access to svn)
#source ./setup-env.sh
# - go to the release folder and run the steps for the wrap stage:
#cd /home/sjm84/snapshots/gs2-caveat-x64/from-*
#rk2 create-installer
#rk2 wrap
# go back and run the upload step
#cd /
##cd -
#./run-gs2-caveat.sh upload


# $1 = "wrap"
# On the 32 bit linux, the wrapping stage sometimes 
# fails when calculating sizeof during wrapping
# Everything until then has usually worked, so we just
# re-try wrapping to make the nightly binaries go through

    cd /greenstone/gs-release-builder
    source ./setup-env.sh

    export PATH=/greenstone/gs-release-builder/release-kits/bin/:$PATH

    # the star at the end does tab-completion
    cd /greenstone/gs-release-builder/snapshots/gs2-caveat/from*
    
    # store a copy of the original build log
    mv rk2.out rk2orig.out

    #rk2 create-installer
    rk2 wrap

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

    # generate the files for upload 
    cd /greenstone/gs-release-builder/envi/bin
    ./envi.pl gs2-caveat upload

    # back to lsb root
    cd /
fi
