#!/bin/sh
#---------------------------------------------#
# ~il/cry95/runmpi                            #
#                                             #
#   Executing Crystal95 on the MPI cluster    #
#   For all details see:                      #
#                                             #
#   http://www.csa.ru/~il/cry95mpi/           #
#                                             #
#   Written by Ilya Evseev, Mar 1999          #
#                                             #
#---------------------------------------------#

#---------------------------------------------#
#   Check and parse command line parameters   #
#---------------------------------------------#

#  NOTE: under AIX 'which' ignores PATH environment variable
#        and reads "path=(...)" directly from ~/.cshrc file
#
MPI_RUN=`which mpirun`
MPI_RUN=${MPI_RUN:-/usr/local/mpich/bin/mpirun}
if [ ! -x ${MPI_RUN} ]; then
  echo ERROR: Cannot find mpirun, please set PATH to proper value!!!
  exit 1
fi

if [ -z "$1" ]; then
  echo Syntax: $0 input_file
  exit 1
fi
echo "You enter file name: $1"

for ff in $1 MPIintegrals MPIscf paths.lst ; do
  if [ ! -f ${ff} ]; then
    echo ERROR: Required file ${ff} is missed, stop.
    exit
  fi
done

#---------------------------------#
#   Prepare working directories   #
#---------------------------------#

# save original paths list, then write back in clean form:
#   ignore all comment lines and blank lines
#   save data lines to temp file
# save count of data lines to NNODES
#
# WARNING: Without my recode utility paths.lst with DOS-like text
#          (\0D\0A as EOL) will be interpreted incorrectly!!!
#
mv paths.lst __paths_.lst
NNODES=`egrep -v '(^#.*)|(^[\t ]*$)' __paths_.lst | tee paths.lst | wc -l`
#test -x ${HOME}/../il/recode && ${HOME}/../il/recode -- paths.lst

for dd in `cat paths.lst`;
do
  if [ -d ${dd} ]; then
    ddold=${dd}.`date | tr ' :.' '___'`
    echo Directory ${dd} already exist and will be renamed to ${ddold}
    mv ${dd} ${ddold}
  fi
  mkdir ${dd}
  if [ $? != 0 ]; then
    echo ERROR: ${dd} unaccessible, stop.
    exit 1
  fi

done

cp $1 `head -n 1 paths.lst`/INPUT

#--------------------#
#   Start programs   #
#--------------------#  Step 1

echo "Program Integrals started at  `date`"
${MPI_RUN} -np ${NNODES} MPIintegrals || exit 1
cp `head -n 2 paths.lst | tail -n 1`/OUTPUT step1
echo "Program Integrals finished at `date`"

#--------------------#  Step 2

echo "Program SCF       started  at `date`"
${MPI_RUN} -np ${NNODES} MPIscf || exit 1
cp `head -n 2 paths.lst | tail -n 1`/OUTPUT step2    #!! potentially rcp
echo "Program SCF       finished at `date`"

#--------------------#  Step 3

# cp ftn09 crys.000.009
# cp ftn09 savftn09 
# cp inp3 input3
# cryst > out3
# cp out3 step3
# rm input3
# rm output3

#---------------------------#
#   Erase temporary files   #
#---------------------------#

for dd in `cat paths.lst`;
do
  rm -f ${dd}/ftn* ${dd}/fort* ${dd}/crys.*
done
mv __paths_.lst paths.lst

## EOF ##
