#!/bin/bash


cd $( dirname $0 )

## Set defaults
##---------------------------------------------------
TRUST_FLAG_DEBUG=0; TRUST_FLAG_PROD=0; TRUST_FLAG_HELP=0


## Get options (for replacing initials settings)
##---------------------------------------------------
while [ $# -ne 0 ]; do
    case $1 in
	'-d'|'--debug'  ) TRUST_FLAG_DEBUG=1 ; shift  ;; '-j'|'--job' 	  ) TRUST_COMPILE_NPROC=$2; shift 2;;
	'-m'|'--machine') TRUST_MAIN_HPCC=$2; shift 2;; '-h'|'--help'	  ) TRUST_FLAG_HELP=1 ; shift  ;;
	'-u'|'--user'   ) TRUST_MAIN_USER=$2; shift 2;; "*"             ) TRUST_FLAG_HELP=1 ; shift  ;;
    esac
done


## Initialization (HPC & user environment)
##---------------------------------------------------
if [[ ! -e cfg/${TRUST_MAIN_USER}.cfg || ! -e cfg/${TRUST_MAIN_HPCC}.cfg || ${TRUST_FLAG_HELP} -eq 1 ]]; then
    cat ./inc/trusting_help.txt

    if [ ${TRUST_FLAG_HELP} -eq 0 ]; then
	printf "\n\n\033[0;33m"
	printf "At least one configuration (arch or user) file is missing or misspelled:"
	printf "\t'%s'.cfg\t'%s'.cfg" ${TRUST_MAIN_USER} ${TRUST_MAIN_HPCC}
	printf "\033[0m"
    fi

    printf "\n\nContent of 'config' folder:\n"
    find config -name *.cfg | cut -d/ -f2 \
	| xargs -n 4 printf "\t- %-25s\t- %-25s\t- %-25s\t- %-25s\n"
    exit 1
else
    . ./inc/trusting.env
    [ ${TRUST_FLAG_DEBUG} -eq 1 ] && set -vx
fi


## List last branches from NEMO Forge
##---------------------------------------------------
printf "\nWhat branch do you want to install in "${TRUST_DIR_WORK}" for trusting test ? "
echo 'Enter 0 to abort'
select branch in 'trunk' $( svn ls ${TRUST_SVN_REPO}/branches/2015 | tr -d / | sort -r ); do

    if [ $REPLY -eq 0 ]; then exit 1; else export TRUST_SVN_BRANCH=$branch; fi

    printf "\nInstallation of a working copy of '%s' branch in '%s'? " ${TRUST_SVN_BRANCH} ${TRUST_DIR_WORK}
    printf "\nType [Y|y|yes] to confirm, if not back to branches list number\n"
    read answer
    [[ $answer == 'Y' || $answer == 'y' || $answer == 'yes' ]] && break

done

echo


## First checkout of selected branch
##---------------------------------------------------
echo 'Initial checkout of '${TRUST_SVN_BRANCH}' branch'
mkdir -p ${TRUST_DIR_WORK}/${TRUST_SVN_BRANCH}/NEMOGCM
cd       ${TRUST_DIR_WORK}/${TRUST_SVN_BRANCH}/NEMOGCM

svn_bran=branches/2015/${TRUST_SVN_BRANCH}
[ ${TRUST_SVN_BRANCH} == 'trunk' ] && svn_bran=${TRUST_SVN_BRANCH}

for elmt in ${TRUST_SVN_CO}; do
    [ $elmt == '\' ] && continue
    printf "%s " $elmt

    if [ $elmt == 'TOOLS/maketools' ]; then
	svn co -q ${TRUST_SVN_REPO}/${svn_bran}/NEMOGCM/TOOLS --depth empty
	svn up -q $elmt
    else
	svn co -q ${TRUST_SVN_REPO}/${svn_bran}/NEMOGCM/$elmt $elmt
    fi

done

printf "\n\n"


## Compile rebuild_nemo.exe in anticipation
##---------------------------------------------------
if [ $( find ARCH -name arch-${TRUST_MAIN_HPCC}.fcm ) ]; then
    echo 'Compile NEMO rebuild tool'
    cd TOOLS && ./maketools -n REBUILD_NEMO -m ${TRUST_MAIN_HPCC} -j ${TRUST_COMPILE_NPROC} >& /dev/null
    [ -e REBUILD_NEMO/rebuild_nemo.exe ] && printf "\033[0;32mOK\033[0m" || printf "\033[0;31mKO\033[0m"
    printf "\n\n"
else
    printf "\033[0;31mNo arch file found to compile NEMO rebuild tool\033[0m\n\n"
fi

exit 0

