#! /usr/bin/env bash

# $Header: /u/gcmpack/MITgcm/tools/example_scripts/csail/test_faulks,v 1.22 2011/09/07 21:42:52 jmc Exp $

#  Test script for MITgcm that should work on most of the csail.mit.edu
#  Linux machines.

# defaults
export PATH='/usr/local/bin:/bin:/usr/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
#  MPI test (for now, only with gfortran)
#  export MPI_GCC_DIR=/srv/software/gcc/gcc-packages/gcc-4.4.5/mpich2/mpich2-1.3
#  export MPI_INC_DIR=$MPI_GCC_DIR/include
#  export PATH="$PATH:$MPI_GCC_DIR/bin"

#- method to acces CVS:
 # export CVSROOT='/u/gcmpack'
 # export CVSROOT=':ext:@mitgcm.org:/u/gcmpack'
 # export CVS_RSH='ssh' ; cvs co -P MITgcm > /dev/null
 # cvs -d :pserver:cvsanon@mitgcm.org:/u/gcmpack co -P MITgcm > /dev/null
cmdCVS='cvs -d :pserver:cvsanon@mitgcm.org:/u/gcmpack'
cmdCVS='cvs -d /u/gcmpack'

TESTDIR="/scratch/jmc/test_"`hostname -s`
MC=13
checkOut=1
sepDir=1
option=
#tst_list='g7a adm mpa g77 gfo+rs mth mp2+rs mpi ifc'
#tst_list='g77 adm gfo ifc mth pgi+rs'
#tst_list='g77 adm gfo ifc mth'
tst_list='g77 gfo ifc mth'

#option="-nc" ; checkOut=0
#option="-q"  ; checkOut=0

TODAY=`date +%d`
tdir=$TESTDIR
if test $checkOut = '0' ; then
  if test -e $tdir/MITgcm/CVS ; then
    echo $tdir/MITgcm/CVS 'exist'
    echo -n "Update the MITgcm code using: $cmdCVS ..."
    cd $tdir/MITgcm
    if test $sepDir = 1 ; then
      $cmdCVS update -P -d > /dev/null
    else
      $cmdCVS update -P -d
    fi
    echo "  done"
  else
    echo -n $tdir/MITgcm 'missing ; '
    checkOut=1
  fi
fi
if test $checkOut = '1' ; then
  if test -e $tdir ; then
    echo -n "Removing working copy: $tdir/MITgcm ..."
    test -e $tdir/MITgcm  &&  rm -rf $tdir/MITgcm
  else
    echo -n "Creating a working dir: $tdir ..."
    mkdir $tdir
  fi
  echo "  done"
  echo -n "Downloading the MITgcm code using: $cmdCVS ..."
  cd $tdir
  $cmdCVS co -P MITgcm > /dev/null
  echo "  done"
else
  cd $tdir
fi

#------------------------------------------------------------------------

firstTst=`echo $tst_list | awk '{print $1}'`
last_Tst=`echo $tst_list | awk '{print $NF}'`
for tt in $tst_list
do

  echo "================================================================"
  typ=`echo $tt | sed 's/+rs//'`
  #- check day and time:
  curDay=`date +%d` ; curHour=`date +%H`
  if [ $curDay -ne $TODAY ] ; then
    date ; echo "day is over => skip test $typ"
    continue
  fi
  if [ $curHour -ge 18 ] ; then
    date ; echo "too late to run test $typ"
    continue
  fi
  #- clean-up old output files
  rm -f $tdir/output_${typ}*
  if test $sepDir = 1 ; then
    new_dir="MITgcm_$typ"
    if test -d $new_dir/CVS -a $checkOut = '0' ; then
      pushd $new_dir
      echo -n "Update the MITgcm code using: $cmdCVS ..."
      $cmdCVS update -P -d
      echo "  done"
    else
      test -e $new_dir  &&  rm -rf $new_dir
      mkdir $new_dir
      pushd $new_dir
      cp -ra ../MITgcm/* .
    fi
  else
    pushd MITgcm
  fi
  cd verification

#-- set the testreport command:
  nbl='-100'
  comm="./testreport"
  if test $typ = 'g7a' -o  $typ = 'adm' -o  $typ = 'mpa' ; then
    nbl='-60'
    comm="$comm -adm"
#   comm="$comm -skd global_ocean.cs32x15"
  elif test $typ = 'mth' -o  $typ = 'mp2' ; then
    export KMP_STACKSIZE=400m
    export OMP_NUM_THREADS=2
    comm="$comm -mth"
  fi
  comm="$comm -a jmc@mitgcm.org"
#-- set the optfile (+ mpi & match-precision)
  MPI=0
  case $typ in
   'g77'|'g7a')	OPTFILE='../tools/build_options/linux_ia32_g77' ;;
   'gfo'|'adm')	OPTFILE='../tools/build_options/linux_ia32_gfortran' ;;
   'ifc'|'mth')	OPTFILE='../tools/build_options/linux_ia32_ifort'
		comm="$comm -devel" ;;
   'pgi')	OPTFILE='../tools/build_options/linux_ia32_pgf77' ;;
       *)	OPTFILE= ;;
  esac
#-- set MPI command:
  if test $MPI != 0 ; then
    if test $typ = 'mp2' ; then MPI=3 ; fi
    if test $typ = 'mpa' ; then
      EXE="mpirun -np TR_NPROC ./mitgcmuv_ad"
    else
      EXE="mpirun -np TR_NPROC ./mitgcmuv"
    fi
  fi

#-- set specific Env Vars:
  if test $typ = 'ifc' -o  $typ = 'mth' ; then
    source /usr/local/pkg/intel/intel_fc_90/bin/ifortvars.sh
  fi
  if test $typ = 'pgi' ; then
    listT='fizhi-cs-32x32x40 fizhi-cs-aqualev20'
    export PGI=/usr/local/pkg/pgi/pgi-6.1-5
    export PATH="$PGI/linux86/6.1/bin:$PATH"
  fi

  if test $sepDir = 0 -a $checkOut = '1' -a  $tt = $firstTst ; then
#-- cleaning:
    echo "======================"
    echo "Cleaning test directories:"
    cmdCLN="./testreport -clean"
    echo " clean dir running: $cmdCLN"
    $cmdCLN > /dev/null 2>&1
    echo "======================"
    echo
  fi

  if test $typ = 'pg2' ; then
#- run 1rst time without sending email:
    echo "Running testreport using:"
    com1="./testreport -dd"
    if test "x$OPTFILE" != x ; then
      com1="$com1 -of=$OPTFILE"
    fi
    if test "x$option" != x ; then com1="$com1 $option" ; fi
    com1="$com1 -t \"\$listT\""
    echo "  \"eval $com1\""
    echo "======================"
    eval $com1 > $tdir/output_${typ}_1 2>&1
    tail $nbl $tdir/output_${typ}_1
    echo
#- ready to run a 2nd time (quick one)
    comm="$comm -q"
  fi

#-- run the testreport command:
  echo -n "Running testreport using:"
  if test "x$OPTFILE" != x ; then
    comm="$comm -of=$OPTFILE"
  fi
  if test $MPI = 0 ; then  echo ''
  else echo " (EXE='$EXE')"
    comm="$comm -MPI $MPI -command \"\$EXE\""
  fi
  if test "x$option" != x ; then comm="$comm $option" ; fi
  if test $typ = 'pgi' ; then comm="$comm -t \"\$listT\"" ; fi
  echo "  \"eval $comm\""
  echo "======================"
  eval $comm > $tdir/output_$typ 2>&1
  tail $nbl $tdir/output_$typ
  echo

#-- also test restart (test 2+2=4)
  if test $tt != $typ
  then
    echo "testing restart using:"
    comm="../tools/do_tst_2+2 -a jmc@mitgcm.org"
    if test $MPI = 0 ; then
      echo "  \"$comm\""
      echo "======================"
      $comm > $tdir/output_2+2 2>&1
    else
      echo "  \"$comm -mpi -exe $EXE\""
      echo "======================"
      $comm -mpi -exe "$EXE" > $tdir/output_2+2 2>&1
    fi
   #tail $nbl $tdir/output_2+2
    echo ; cat tst_2+2_out.txt
    echo
  fi
  export OMP_NUM_THREADS=1

  if test $sepDir = 0 ; then
#-- cleaning:
    echo "======================"
    echo "Cleaning test directories:"
    if test $tt != $typ ; then
      cmdCLN="../tools/do_tst_2+2 -clean"
      echo " clean tst_2+2 running: $cmdCLN"
      $cmdCLN >> $tdir/output_2+2 2>&1
    fi
    if test $tt != $last_Tst ; then
      cmdCLN="./testreport -clean"
      echo " clean dir running: $cmdCLN"
      $cmdCLN > /dev/null 2>&1
    fi
    echo "======================"
    echo
  fi
  popd

done