#!/bin/bash


if [ "x$1" != "xwrap" ]; then
    echo "RUNNING GS3 CAVEAT at " `date` > out.txt
    env >> out.txt
    # this doesn't work as envi wipes out the environment
    #cd /greenstone/release-kits
    #source ./rk-setup.bash
    cd /greenstone/gs-release-builder/envi/bin
    ./envi.pl gs3-caveat-x64 $*

else
# $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/gs3-caveat-x64/from*
    
    # store a copy of the original build log
    mv rk3.out rk3orig.out

    #rk3 create-installer
    rk3 wrap

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

    # upload the newly wrapped binary and the log
    cd /greenstone/gs-release-builder/envi/bin
    ./envi.pl gs3-caveat-x64 upload

    # back to lsb root
    cd /
fi
