#!/bin/sh -x
# $Id: run_Gromacs_data_recovery.sh,v 1.0 2017/11/23 12:15:00 Sudhakar Exp $

if [ $# -lt 1 -o $# -gt 11 ]; then
    echo 1>&2 "Usage: $0 -c coord_file -s tpr_file -g log_file -e ener_file [SEAGrid_UserName] [Gromacs_restart_input] "
    #echo 1>&2 "Usage: $0 subdir Gromacs_restart_input"
    exit 127
fi

# subdir depends on whether we're doing freq, water or PES. For freq and water,
# it should be hardcoded in the Xbaya workflow. For PES, it should be an
# additional array generated by the frontend. The contents of this array are
# trivial, but creating an extra Xbaya service to generate it would add
# unnecessary extra complexity. Besides, the frontend cannot avoid having to
# pass at least one array: the array with gjf files.
#subdir=$1
subdir="$PWD"
#Gromacs_res_input_full=$1
Coord_file=$2
Tpr_file=$4
Log_file=$6
Ener_file=$8
Rest_file=$10
if [ $AIRAVATA_USERNAME ]; then
  echo " The Airavata Gateway User is $AIRAVATA_USERNAME"
  SG_UserName="$AIRAVATA_USERNAME"
elif [ $9 ]; then
  SG_UserName=$9
else
  echo " The Airavata Gateway User is missing "
exit
fi
dos2unix -k $Gromacs_res_input_full
Gromacs_input=${Gromacs_res_input_full##*/}
Gromacs_output=${Gromacs_input%.*}.log
#clobber="$3" # set to "" for rerun or debug; otherwise set to 1
clobber="1" # set to "" for rerun or debug; otherwise set to 1
#gcvars=$4
gcvars="GCVARS"
null=""
localarc="$HOME/scratch"
# Next line will ususally return "cp: `GCVARS' and `GCVARS' are the same file"
if [ "$gcvars" ] ; then cp -p $gcvars GCVARS 2>/dev/null ; fi
##
#if [ ! "$LOCAL_LOCATION" ] ; then
#  if [ -s ~/.paramchemlocation ] ; then
#    read LOCAL_LOCATION < ~/.paramchemlocation
#   fi
# fi
#if [ ! -d "$LOCAL_LOCATION" ] ; then
#  echo "Error: no valid LOCAL_LOCATION found" >&2
#  exit 1
# fi
#. $LOCAL_LOCATION/environment

#read GC_UserName GC_ProjectName GC_WorkflowName TIMESTAMP < GCVARS
usrchkdir=$localarc/${SG_UserName}/
echo " The Airavata Gateway User Directory is $usrchkdir"
copysubdir="./"
#copysubdir=$localarc/${GC_UserName}/${GC_ProjectName}/${GC_WorkflowName}/$subdir
# The way a "false" boolean variable is passed seems to be unstable; it's
# been "", "0" and "false", so we try to cover all reasonable possibilities.
if [ ! "$clobber" ] ; then
  clobber=0
 fi
if [ "$clobber" = "0" -o "$clobber" = "false" -o "$clobber" = "no" ] ; then
  if [ -s $copysubdir/$Gromacs_output ] ; then
    echo Gromacs_output_log=$copysubdir/$Gromacs_output
    exit 0
   fi
  Gromacs_output_full=${Gromacs_input_full%.*}.log
  if [ -s $Gromacs_output_full ] ; then
    mkdir -p $copysubdir
    rm -f $copysubdir/$Gromacs_output # clean up symlink if something went wrong earlier
    if [ $Gromacs_input_full -nt $copysubdir/$Gromacs_input ] ; then sed 's/\r$//' $Gromacs_input_full > $copysubdir/$Gromacs_input ; fi
    cp -up $Gromacs_output_full $copysubdir
    echo Gromacs_output_log=$Gromacs_output_full
    exit 0
   fi
 fi

#Process inputfile for run files and other job requirements
//"`export PJobID=`grep -i localjobid restart.txt| awk -F= '{print $2}' | sed "s/
    if [ ${PJobID:-null} != "$null" ]; then
    cp -r /home/scigap/scratch/${SG_UserName}/$PJobID*/* .
    fi
#  # PROCESS CHECKPOINT FILE
#  # Check to see if the checkpoint file is given a name in input deck
#  # Input file to look into
#  dummy="$Gromacs_input_full"
#  #dummy="$Diskinputdir/$Diskinputfile"
#  checkpt="no"
#  junk=`/usr/bin/head -5 $dummy | /bin/grep -i "%chk"`
#  if [ "$junk" !=  "" ]; then
#    junk=`echo $junk | /bin/sed 's/=/@/'`
#    junk=`echo $junk | /bin/sed 's/ //'`
#    #
#    # Be careful: Don't lose the subdirectory information for CHKPT file
#    # Also, add .chk if there is no extension to the Checkpoint file
#    #
#    Chkfile=`expr $junk : '.*@\(.*\)'`
#    Chkdir="$Diskinputdir"
#    Chkfile=`/bin/basename $Chkfile`
#    dummy=`expr $Chkfile : '.*\(\..*\)'`
#    Chkfile=`/bin/basename $Chkfile $dummy`
#    ChkfileWNE="$Chkfile"
#    Chkfile="$Chkfile${dummy:-.chk}"
//"` Chkfile=`echo $Chkfile | sed "s/
#    # 2005/12/08 create name for $formated_chkfile
#    formated_chkfile="$ChkfileWNE.fchk"
#    Chkfile_with_arch="${Chkfile}_$architecture"
#    echo "DEBUG: checkfile = $Chkfile and formated_chkfile = $formated_chkfile ";
#    checkpt="yes"
##Retrieve the checkpoint file from the user archive directory
#     if [ -f "$usrchkdir/$Chkfile" ]; then
#        cp $usrchkdir/$Chkfile .
#     fi
#   else
#    echo "******** NO CHECKPOINT FILE IDENTIFIED ******** "
#  fi

mkdir -p $copysubdir
mkdir -p $subdir
cd $subdir
cwd=`pwd`
if [ $Gromacs_input_full -nt $copysubdir/$Gromacs_input ] ; then sed 's/\r$//' $Gromacs_input_full > $copysubdir/$Gromacs_input ; fi
cd $copysubdir
rm -f $Gromacs_output
if [ "$cwd" != "$subdir" ]; then
   ln -s $cwd/$Gromacs_output $subdir/$Gromacs_output
fi
cd $cwd
if [ $Gromacs_input_full -nt $Gromacs_input ] ; then sed 's/\r$//' $Gromacs_input_full > $Gromacs_input ; fi
module unload intel; module load gromacs
if [ ${PJobID:-null} != "$null" ]; then
    cp -r /home/scigap/scratch/${SG_UserName}/$PJobID*/* .
    #mpiexec -genv I_MPI_FABRICS shm:ofa gmx_mpi mdrun -s $Tpr_file -cpi state.cpt
    mpirun -np $SLURM_NTASKS  -genv I_MPI_FABRICS shm:ofa gmx_mpi mdrun -v -deffnm em -s $Tpr_file -cpi state.cpt
else
    mpirun -np $SLURM_NTASKS  -genv I_MPI_FABRICS shm:ofa  gmx_mpi mdrun -v -deffnm em -s $Tpr_file -c $Coord_file -g $Log_file -e $Ener_file
fi

mpiexec gmx_mpi mdrun -s $Tpr_file -cpi state.cpt
##$LOCAL_LOCATION/run_Gromacs_local.sh $Gromacs_input $Gromacs_output
##$LOCAL_LOCATION/run_Gromacs_local.sh $Gromacs_input $Gromacs_output
#BEGIN{while(getline < infile) if ($0 ~ "^ *--[lL][iI][nN][kK]1--") nlink++}

#if awk -v infile=$Gromacs_input '
#    BEGIN{while(getline < infile) if ($0 ~ "^ *[lL][iI][nN][kK]1") nlink++}
#    /^ *Normal termination of Gaussian/{nnormal++}
#    END{if (nnormal == nlink+1) exit 1}' $Gromacs_output ; then
#  echo "Gaussian terminated abnormally." >&2
#  exit 1
#fi
#rm $copysubdir/$Gromacs_output
cp -p $Gromacs_output $copysubdir
  # Save  checkpoint file to usrchkdir
#  mkdir -p $usrchkdir
#  if [ -f "$Chkfile" ]; then
#  	cp -f $Chkfile $usrchkdir
#  fi
#  # Create a link directory for this job with jobID in user scratch
  ln -s $PWD ~/scratch/${SG_USERNAME}/$PBS_JOBID
echo Gromacs_output_log=$cwd/$Gromacs_output
#rm *.wfc*
