#! /bin/bash

# try to kill parallel orac smoothly
#
# syntax: kill_orac_rem <program-name> 
#

# ----------------------------------------------------------------------
# help: copy first comments block in this file
# ----------------------------------------------------------------------
function help() {
echo $0
name=`basename "$0"`;
echo "-->" $name;
sed -n "s/\$0/$name/;2,/^\$/p" $0; exit
}


# ----------------------------------------------------------------------
# init and defaults   
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
# command line options
# ----------------------------------------------------------------------
while getopts "h-" Option
do
  case $Option in
    *     )    help;;   # help: copy first comments block
  esac
done

shift $(($OPTIND - 1))
#  Decrements the argument pointer so it points to next argument.
#  $1 now references the first non option item supplied on the command line
#+ if one exists.

# ----------------------------------------------------------------------
# command line arguments
# ----------------------------------------------------------------------
O_BIN_P=$1

# ----------------------------------------------------------------------
# read user's configuration
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
# final parameters setting
# ----------------------------------------------------------------------

if [[ $# < 1 ]]; then
    printf "Please provide a program name as argument\n "; 
    awk '/^ *O_BIN_P *=/ {gsub("O_BIN_P *=",""); printf "... maybe it is `%s` ?\n",$0 }' Makefile;
    help;
fi


# ----------------------------------------------------------------------
# engine
# ----------------------------------------------------------------------

printf "Trying to kill REM ORAC smoothly ... "

if `test -e tmp`; then rm tmp; fi
# find out startig RESTART file 
OLD_RESTART=`awk '/^ *read /{print $2}' INPUT`
if [[ ${OLD_RESTART}=="" ]]; then OLD_RESTART="no-old-restart-found!"; fi
# find out current restart file
RESTART=`awk '/^ *write /{print $4}' INPUT`
# generate STOP for orac  
OLD_PDB_FILE=`awk '/^ *ASCII/ {print $4}' INPUT`

for i in `ls -d PAR0*` ; do touch $i/STOP; done

new_name=`date +"%Y-%m-%d_%H%M%S"`  # e.g 2010-05-21_113027


# generate the input file for restarting the program 
sed "s/$RESTART/$new_name\.rst/g" INPUT >  x.0 
sed "s/$OLD_RESTART/$RESTART/g" x.0> y.0 ;  
sed "s/${OLD_PDB_FILE}/$new_name\.pdb/g" y.0>  ${new_name}.in 

rm x.0 y.0 

# wait for all processes to stop before removing the STOP files
for ((j=1;j<=10;j++)); do 
  sleep 10
  for i in `cat mpd.hosts`; 
    do ssh $i ps r -u $USER | grep ${O_BIN_P} >> tmp ; 
  done 
  if [[ `wc tmp | awk '{print $1}'` == "0" ]]; 
  then
     for i in `ls -d PAR0*` ; do rm $i/STOP; done
     printf " ... done\n"
     echo "====================================================================="
     echo "=    To restart the program, use the input file:"      ${new_name}.in
     echo "=    program will resume from latest restart file "
     echo "====================================================================="
     exit; 
  fi 
done; 

