#!/bin/bash -f
# set -vx

SETTE_DIR=$(cd $(dirname "$0"); pwd)
MAIN_DIR=$(dirname $SETTE_DIR)

. ./param.cfg

if [ $# -gt 0 ]; then
  while getopts c:v:h option; do 
     case $option in
        c) COMPILER=$OPTARG;;
        v) SETTE_SUB_VAL=$OPTARG;;
        h | *) echo ''
               echo 'sette_list_avail_rev.sh : ' 
               echo '     list all sette directory and available revisions created with the compiler specified in param.cfg or in the startup file)'
               echo '-c COMPILER_name :'
               echo '     list all sette directory and available revisions created with the compiler specified'
               echo ' -v sub_dir :'
               echo '     validation sub-directory below NEMO_VALIDATION_DIR'
               echo ''
               exit 42;;
     esac
  done
  shift $((OPTIND - 1))
fi
if [ ! -z $SETTE_SUB_VAL ] ; then
 NEMO_VALIDATION_DIR=$NEMO_VALIDATION_DIR/$SETTE_SUB_VAL
fi

#
lst_rev () {
    # get the list of revision available for a configuration
    # base directory
    VALSUB=$1
    # config name
    CONFIG=$2
    # list of all revision available
    ALLLST=${@:3}
    # display
    echo ""
    printf "%-27s : " $CONFIG
    for rev in $ALLLST
    do
       if [ -d ${VALSUB}/$rev/${CONFIG} ]  ; then
          printf "%-6s  " $rev
       else
          printf "%-5s  " "----- " 
       fi
    done
}


  NEMO_VALID=${NEMO_VALIDATION_DIR}/
 
 # list of all revision available
 DIRLIST=`find ${NEMO_VALID}/${COMPILER} -maxdepth 1 -mindepth 1 -type d | sort -u -r`
 DIRLIST=`basename -a $DIRLIST`

 # display header
 echo ""
 echo " Compiler used is : $COMPILER"
 echo ""
 printf " List of all avail. rev. is : "
 for dir in `echo $DIRLIST`; do printf "%-6s  " $dir ; done
 printf "\n"

 # start checking configuration revision
 echo ""
 echo "   !---- check revision available for each configuration ----!   "
 for CONFIG in GYRE_PISCES ORCA2_ICE_PISCES ORCA2_OFF_PISCES AMM12 ORCA2_SAS_ICE AGRIF_DEMO SWG ISOMIP+ OVERFLOW LOCK_EXCHANGE VORTEX ICE_AGRIF 
 do
    DIR=${NEMO_VALIDATION_DIR}/${COMPILER}/
    lst_rev $DIR $CONFIG $DIRLIST
 done
 printf "\n"
 printf "\n"
#
exit
