#!/bin/bash

# This must be run in lsb environment

# Print usage if we didn't get a cmdline parameter
ver=""
bit=""

# bitness folder suffix: 64 becomes -x64
bitsuffix=""

usage() {
    echo "Run as follows:"
    echo "$0 -v 2|3 -b 32|64 <wrap|..>"
    echo "e.g. $0 -v 3 -b 64"
    echo "     will generate the GS3 64 bit release"
    echo "e.g. $0 -v 2 -b 32 wrap"
    echo "     will redo the stages from wrap and onward of GS2 32 bit release"
}

# https://www.baeldung.com/linux/use-command-line-arguments-in-bash-script
# Googled also: getopts compulsory parameters
# on how to enforce compulsory parameters
while getopts :hv:b: flag
do
    case "${flag}" in
        h) usage; exit -1 ;;
        v) ver=${OPTARG};;
        b) bit=${OPTARG};;
        :) echo "Error: -$OPTARG requires an argument." >&2; usage; exit 1 ;;        
        \?) echo "Error: Unknown option -$OPTARG" >&2; usage; exit 1 ;;
    esac
done

# Chatbot suggestions for checking if mandatory options were provided
# For which the arg variables must be initialized to empty string literals
if [ -z "$ver" ] || [ -z "$bit" ]; then
    echo "Must provide -v and -b arguments"
    usage
    exit 1
fi

# I want to further check the correct values for the compulsory arguments were passed in
if [ "$ver" -lt 2 ] && [ "$ver" -gt 3 ]; then
    echo "GS version -v has to be 2 or 3"
    usage
    exit 1
fi
if [ "$bit" != "32" ] && [ "$bit" != "64" ]; then
    echo "Bitness -b has to be 32 or 64"
    usage
    exit 1
fi

if [ "$bit" = "64" ]; then
    bitsuffix="-x64"
fi

# Now handle positional arguments (shifts remaining parameters to start at 1st position)
# https://www.baeldung.com/linux/use-command-line-arguments-in-bash-script
# This will also update $* (and $# and $@) to deal with just the positional arguments
shift $((OPTIND - 1))

echo "v: $ver; b: $bit"
echo "Remaining args: $*"
#echo "Prematurely exiting for testing"
#exit 0

# So now the rest of the script can be as before
echo `date`


if [ "x$1" != "xwrap" ]; then
    echo "RUNNING GS${ver} 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 gs${ver}-caveat${bitsuffix} $*
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/gs${ver}-caveat${bitsuffix}/from-*
#rk${ver} create-installer
#rk${ver} wrap
# go back and run the upload step
#cd /
##cd -
#./run-gs-caveat-linux.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

    pushd $BASEDIR
    source ./setup-env.sh

    # sourcing setup-env.sh script above already did this:
    #export PATH=/greenstone/gs-release-builder/release-kits/bin/:$PATH

    # the star at the end does tab-completion
    pushd /greenstone/gs-release-builder/snapshots/gs${ver}-caveat${bitsuffix}/from*
    
    # store a copy of the original build log
    mv rk${ver}.out rk${ver}orig.out

    #rk${ver} create-installer
    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${bitsuffix} upload
    $BASEDIR/upload-files-to-www-internal.sh

    # back to lsb root
    #cd /
    popd
    popd
    popd
fi
