#! /usr/bin/env bash

# $Header: /u/gcmpack/MITgcm/tools/example_scripts/nasa_ames/test_clean_pleiades,v 1.1 2017/04/21 19:21:28 jmc Exp $
# $Name:  $

today=`date +%Y%m%d`
dInWeek=`date +%a`

if test $# = 0
then
  TEST_LIST='ieee fast'
else
  TEST_LIST=$*
fi

headNode=`hostname -s`
QSUB="/PBS/bin/qsub"
QSTAT="/PBS/bin/qstat -u $USER"
#dNam=$headNode
#QSUB="/usr/bin/sbatch"
#QLIST="/usr/bin/squeue -u $USER"
dNam='pleiades'
HERE="$HOME/test_${dNam}"
TST_DISK="/nobackupp2/$USER"
TST_DIR="$TST_DISK/test_${dNam}"
logPfix='test_clean'

SUB_DIR="$HERE/nasa_ames"
OUT_DIR="$HERE/output"
LOG_FIL="$OUT_DIR/$logPfix."`date +%m%d`".log"
SUB_DIR="$HERE/temp"

#-- clean up old log files and start a new one:
cd $OUT_DIR

rm -f $logPfix.*.log_bak
if test -f $LOG_FIL ; then mv -f $LOG_FIL ${LOG_FIL}_bak ; fi
echo -n '-- Starting: '					| tee -a $LOG_FIL
date							| tee -a $LOG_FIL

n=$(( `ls $logPfix.*.log | wc -l` - 10 ))
if test $n -gt 0 ; then
  echo ' remove old log files:'				| tee -a $LOG_FIL
    ls -lt $logPfix.*.log | tail -"$n"			| tee -a $LOG_FIL
    ls -t  $logPfix.*.log | tail -"$n" | xargs rm -f
fi

#-------------------------------------------------------------
# defaults
#export PATH="$PATH:/usr/local/bin"
if [ -d ~/bin ]; then export PATH=$PATH:~/bin ; fi
#- to get case insensitive "ls" (and order of tested experiments)
export LC_ALL="en_US.UTF-8"
#  Turn off stack limit for FIZHI & AD-tests
#ulimit -s unlimited

if test -f /etc/profile.d/modules.sh    ; then . /etc/profile.d/modules.sh    ; fi
#- load standard modules:
#module add fedora slurm maui svante
#module add slurm
module list 						>> $LOG_FIL 2>&1

#- method to acces CVS:
cmdCVS='cvs -d :pserver:cvsanon@mitgcm.org:/u/gcmpack -q'

#---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|

echo "cd $TST_DISK ; pwd (x2)" | tee -a $LOG_FIL
cd $TST_DISK	| tee -a $LOG_FIL 2>&1
pwd		| tee -a $LOG_FIL
if test ! -d $TST_DIR ; then
   echo -n "Missing working dir: $TST_DIR ..."	| tee -a $LOG_FIL
   echo " --> exit"				| tee -a $LOG_FIL
fi
cd $TST_DIR
pwd		| tee -a $LOG_FIL

#---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
#-- now really do something:

  for sfx in $TEST_LIST
  do
    gcmDIR="MITgcm_$sfx"
    echo " "					| tee -a $LOG_FIL
    if test -d $gcmDIR/verification ; then
      echo "==>>  Cleaning testing dir $gcmDIR"	| tee -a $LOG_FIL
      ( cd $gcmDIR/verification
        echo " run: ../tools/do_tst_2+2 -clean"	| tee -a $LOG_FIL
        ../tools/do_tst_2+2 -clean	>> $LOG_FIL
        echo " running: clean_exp_fast mnc"	| tee -a $LOG_FIL
        clean_exp_fast mnc		>> $LOG_FIL
        echo " running: clean_exp_fast outp"	| tee -a $LOG_FIL
        clean_exp_fast outp		>> $LOG_FIL
        echo " running: clean_exp_fast Obj"	| tee -a $LOG_FIL
        clean_exp_fast Obj		>> $LOG_FIL
      )
      echo "  <==  cleaning $gcmDIR : Done"	| tee -a $LOG_FIL
    else
      echo "Missing testing dir $gcmDIR"	| tee -a $LOG_FIL
    fi
  done
  date							| tee -a $LOG_FIL

#---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
exit 0
