#
#                           Script domopac
#
#      This script runs a single MOPAC problem for version MOPAC 5.0.
#
#      This script uses 3 input variables; the first one is mandatory, and the 
#      other two are optional.
#          $1 = the name of the problem, the data file has a default extension
#                of dat.
#          $2 = the name of the executable to to run, the default is currently
#               mopac.exe, the fastest one to date.
#          $3 = a simple y/n to decide for profiling. 
# 
#  The variable rel is the object code to execute MOPAC.
#  The variable p is to determine if profiling should be done.
#
#
#  First check the data file parameter.
#
if ( "$1" == "" ) then
   echo The data file name without the extension is required.  Try again.
   exit
endif
#  
echo  "                      Begin domopac script."
#
#     Set the script variables rel and p.
#
set rel = ../mopac.exe
set p = n
if ( "$2" != "" ) then
   set rel="$2"
endif
if ( "$3" != "" ) then
   set p = "$3"
endif
#
#  This is for debug.
#
# echo This is for debug.
# echo rel = $rel  
# echo p = $p 
# exit
#
#     Begin the MOPAC run.
#
echo First check for the $1.dat file
#
if -e $1.dat then
          echo Looking for $1 subdirectory.
#
          if -e $1 then
                    echo Found $1 subdirectory  - removing contents.
                    cd $1
                    if -e FOR013.DAT rm FOR013.DAT
                    if -e FOR012.DAT rm FOR012.DAT
                    if -e FOR010.DAT rm FOR010.DAT
                    if -e FOR009.DAT rm FOR009.DAT
                    if -e FOR006.DAT rm FOR006.DAT
                    if -e FOR005.DAT rm FOR005.DAT
                    if -e shutdown rm shutdown
                    if -e core rm core
                    if -e mon.out rm mon.out
                    if -e $1.prf rm $1.prf
                    if -e $1.esp rm $1.esp
          else
            echo No $1 subdirectory exists - creating subdirectory.
            mkdir $1
            cd $1
          endif
#
          pwd
          echo Creating input files for problem $1.
          cp ../$1.dat FOR005.DAT
          if -e ../$1.res cp ../$1.res FOR009.DAT
          if -e ../$1.den cp ../$1.den FOR010.DAT
#
          echo Starting MOPAC run for problem $1.
          nohup time $rel >&FOR006.DAT
          echo MOPAC run for problem $1 completed, moving output files.
#
          if -e FOR006.DAT mv FOR006.DAT ../$1.out
          if -e FOR009.DAT mv FOR009.DAT ../$1.res
          if -e FOR010.DAT mv FOR010.DAT ../$1.den
          if -e FOR012.DAT mv FOR012.DAT ../$1.arc
          if -e FOR013.DAT mv FOR013.DAT ../$1.gra
          if -e FOR021.DAT mv FOR021.DAT ../$1.esp
          if -e shutdown rm shutdown
          if -e core rm core
          rm FOR005.DAT
#
          if ($p == y) then
             echo Running profile.
             prof $rel > $1.prf
             mv $1.prf ../$1.prf
             rm mon.out
          else
             echo No profiling for problem $1.
          endif
#
          echo End of problem $1.
          cd ../
          echo Removing subdirectory.
          rm $1/ESP.DUMP
          rmdir $1
else
          echo  "******** $1.dat not found\!  Please check files/directory."
          pwd
endif
#
#
echo "            Finish domopac script."
