#!/bin/bash

# For details see function printusage at https://trac.greenstone.org/browser/main/trunk/gs-release-builder/envi/etc/tasks/diffcol/task.pl
usage() {
    echo ""
    echo "RUN AS: ./run-gs-diffcol.sh --gs3|--gs2|--help [setup_greenstone|run_test|summarise|upload] [opts] [colnames]"
    echo "    If any collection name(s) are passed in, diffcol will only process them for diffing."
    echo " e.g."
    echo " all: ./run-gs-diffcol.sh --gs3|--gs2"
    echo " or: ./run-gs-diffcol.sh --gs3|--gs2 run_test [colnames]"
    echo "    This is when you want to re-run all of diffcol but don't want to grab greenstone again."
    echo " or: ./run-gs-diffcol.sh --gs3|--gs2 summarise"
    echo " or: run-gs-diffcol.sh setup_greenstone --gs3|--gs2 --svn|--bin"
    echo " or: ./run-gs-diffcol.sh --help"
    echo ""
    echo " FURTHERMORE run_test can take options [opts] of --justdiff and/or --nosvn:"
    echo " or: ./run-gs-diffcol.sh --gs3|--gs2 run_test --justdiff [colnames]"
    echo " or: ./run-gs-diffcol.sh --gs3|--gs2 run_test --nosvn [colnames]"
    echo " or ./run-gs-diffcol.sh --gs3|--gs2 run_test --nosvn --justdiff"
    echo "    which won't svn check out collections again and works with previously checked out ones"
    echo "    and it will straightaway diff without first rebuilding collections."
    echo ""
    echo " AND setup_greenstone can take opts of --bin|--svn (default)"
    echo "  e.g. run-gs-diffcol.sh --gs3 setup_greenstone --bin"
    echo "     which expects you to have sourced a GS3 binary installation to run diffcol with"
    echo ""
#    echo "RUN AS: ./run-gs-diffcol.sh [setup_greenstone|run_test|summarise|upload] [opts] --gs3|--gs2 [colnames]"
#    echo " e.g."
#    echo " all: ./run-gs-diffcol.sh --gs3|--gs2"
#    echo " or: ./run-gs-diffcol.sh run_test --justdiff --gs3|--gs2 [colnames]"
#    echo " or: ./run-gs-diffcol.sh run_test --nosvn --gs3|--gs2 [colnames]"
#    echo " or: ./run-gs-diffcol.sh setup_greenstone --gs3|--gs2 --svn|--bin"
#    echo " or: ./run-gs-diffcol.sh summarise --gs3|--gs2"
#    echo " or: ./run-gs-diffcol.sh --help"
}

# greenstone version
ver=""
#verflag=""

# Check for empty param (string length of 0)
if [ -z "$1" ]; then
    echo "No parameters provided"
    usage
    exit -1
elif [ "$1" = "--help" ] ; then
    usage
    exit 0
elif [ "$1" != "--gs2" ] && [ "$1" != "--gs3" ] ; then
    echo "First parameter must be --gs2 or --gs3 (or --help)"
    usage
    exit -1
else
    #verflag=$1
    if [ "$1" = "--gs2" ] ; then
        ver=2
    else
        ver=3
    fi
fi

# Finished processing parameter 1 of cmdline args, so shift the parameter arguments by 1
# to only process the rest (so that $* no longer includes the 1st parameter --gs3|--gs2)
# https://stackoverflow.com/questions/78195964/how-do-i-remove-positional-parameters-in-bash
#echo "The arguments BEFORE are: $*"
shift # deletes $1, the first positional argument
#echo "The arguments now are: $*"

echo "RUNNING GS${ver} DIFFCOL at " `date` > outdiffcol.txt
env >> outdiffcol.txt
echo "@@@ Diffcol for GS version ${ver}"

OS_ALLCAPS=`uname -s | sed 's/_.*$//' | tr 'a-z' 'A-Z'`
RELEASES_DIR=/greenstone/gs-release-builder
if [ "$OS_ALLCAPS" = "DARWIN" ]; then
    RELEASES_DIR=/Users${RELEASES_DIR}
    cd ${RELEASES_DIR}/envi/bin
    PERL_HOME=${RELEASES_DIR}/ext-cli/selfcontained-perl-with-cpan/gsperl5-with-cpan-darwin
    DYLD_LIBRARY_PATH="$PERL_HOME/lib/:$DYLD_LIBRARY_PATH"
    export DYLD_LIBRARY_PATH
else
    #cd /greenstone/gs-release-builder/envi/bin
    ##cd /greenstone/envi/bin
    cd ${RELEASES_DIR}/envi/bin
    PERL_HOME=${RELEASES_DIR}/ext-cli/selfcontained-perl-with-cpan/gsperl5-with-cpan-linux64
    LD_LIBRARY_PATH="$PERL_HOME/lib/:$LD_LIBRARY_PATH"
    export LD_LIBRARY_PATH
fi

# On mac, this used to work:
#perl -S ./envi.pl diffcol $*
#$PERL_HOME/bin/perl -S ./envi.pl diffcol --gs3 $*
$PERL_HOME/bin/perl -S ./envi.pl diffcol --gs${ver} $*
