#!/bin/sh
# Copyright (c) 2002 Peter Guntert. All rights reserved.

# Batch job submission script
# Peter Guntert, 12-03-2001

dir=`pwd`
name=
prog=cyana
nproc=1
queue=
opt=
after=
submit='nice $name.job >$name.out &'


# ------ Defaults for specific computers ------

  # Add to this section a new entry that the fits 
  # your compute server and batch system.

case `uname -n` in

  # Demo (explanation of options)
  demo)  nproc=5                   # default number of processors to use (can be changed with -n)
         queue=batch               # default batch queue (can be changed with -q)
         opt='-t demo-mpi -c mpirun -np NPROCPLUSONE'  # options to start CYANA
         submit='qsub $name.job'   # command to submit batch job
         ;;

  # Linux cluster with LAM-MPI
  cesg* | parzival | anfortas | gawan | ither | iwanet | kyot) nproc=6
         opt="-t gnu-lam -c 'mpirun -x CYANALIB -np NPROCPLUSONE'"
         queue=seriallong
         #submit='$name.job > $name.out &'
         submit='qsub -S /bin/ksh -N cyana_$name -l nodes=`expr $nproc / 2 + $nproc % 2`:ppn=2 -q $queue -o $name.out -e $name.err $name.job'
         ;;

  # Compaq Alpha cluster using a special implementation of MPI
  tone*) nproc=7
         queue=fci-64-07
         opt='-t alpha-fci -c $HOME/bin/fcienv'
         #opt="-t alpha-mpi -c 'mpirun -np NPROCPLUSONE -machinefile \$TMPDIR/machines'"
         submit='qsub -S /bin/ksh -cwd -l s_rt=4:00:00 -l s_cpu=4:00:00 -pe $queue `expr $nproc + 1` -o $dir/$name.out $name.job'
         prog=`which cyana`
         ;;

  # Compaq Alpha shared-memory system using LSF batch system
  c4-3*) nproc=4
         queue=s
         submit='bsub -q $queue -o $name.out $dir/$name.job'
         ;;

  # HP shared-memory system using LSF batch system
  tornado | stardust) 
         nproc=4
         queue=normal
         submit='bsub -q $queue -o $name.out $dir/$name.job'
     ;;

  # HP cluster using MPI and LSF batch system
  albula | bernina | maloja | julier) 
         nproc=4
         queue=normal
         opt="-t hp-mpi -c 'mpirun -np NPROCPLUSONE'"
         submit='bsub -q $queue -o $dir/$name.out $dir/$name.job'
         ;;

  # Linux cluster using MPI
  cop*)  nproc=8
         opt="-t gnu-mpi -c 'mpirun -np NPROCPLUSONE -machinefile machines$$'"
         submit='$name.job > $name.out &'
         if [ -f machines ]; then 
       machfil=machines
         else
       machfil=$HOME/machines
         fi
         list=`catn | fgrep SuSE_7.1 | sort -n -k 4,4 | awk '{printf("%s ",$2)}'`
     uname -n > machines$$
         for i in $list; do
           if [ `grep -c $i $machfil` -gt 0 ]; then
             case `rsh $i uname -r` in
               2.2.18* | 2.4*) echo $i >> machines$$; echo $i >> machines$$;;
             esac
           fi
         done
         ;;

  # SGI cluster using MPI
  lodur | mani | skinfaxi) nproc=2
         opt="-t sgi-mpi -c 'mpirun -np NPROCPLUSONE -machinefile $HOME/machines'"
         submit='$name.job > $name.out'
         ;;
     
  # By default, assume a shared-memory multiprocessor machine without batch system
  *)     nproc=5
         opt=""
         submit='$name.job > $name.out &'
         ;;
esac


# ------ Command line options ------

options=a:j:n:p:q:o:s:h

optind=1
usage=""
eval par=\$$optind
while true; do
  case $par in
  --) optind=`expr $optind + 1`; break;;
  -?*) option=`echo X$par | sed 's/^X-\(.\).*/\1/'`
       if [ `echo $options | sed 's/.*'$option'.*/+/'` != "+" ]; then
         echo "$0: -$option: unknown option"; usage=1
       elif [ `echo $options | sed 's/.*'$option':.*/+/'` = "+" ]; then
         optind=`expr $optind + 1`
         eval $option=\$$optind
       else
         eval $option=1
       fi
       par=`echo X$par | sed 's/^X-./-/'`
       if [ "$par" = "-" ]; then 
         optind=`expr $optind + 1`
     eval par=\$$optind
       fi
       ;;
  *) break;;
  esac
done
shift `expr $optind - 1`

if [ "$h" ]; then usage=1; fi
if [ "$usage" ]; then
  echo "Usage: cyanajob [-hnpqrs] macro parameters"            
  echo                                     
  echo "       -h        help"                     
  echo "       -a file       start job only after <file> exists"
  echo "       -j name       job name (default: <macro>)"          
  echo "       -p program    program name (default: $prog)"        
  echo "       -o options    options for the program (default: $opt)"      
  echo "       -n nproc      number of processors (default: $nproc)"       
  echo "       -q queue      batch queue (default: $queue)"        
  echo "       -s command    submit command (default: $submit)"        
  exit 2                                   
fi
if [ "$a" ]; then after="$a"; fi
if [ "$j" ]; then name="$j"; fi
if [ "$n" ]; then nproc="$n"; fi
if [ "$p" ]; then prog="$p"; fi
if [ "$q" ]; then queue="$q"; fi
if [ "$o" ]; then opt="$o"; fi
if [ "$s" ]; then submit="$s"; fi


# ------ Macro name ------

if [ $# -lt 1 ]; then
  echo "Missing macro file."
  exit 2
fi
macro=`echo $1 | sed 's/\.cya$//'`
if [ ! -f $macro.cya ]; then
  echo "Macro file $macro.cya not found."
  exit 2
fi
shift
if [ "$name" = "" ]; then name=`basename $macro`; fi

opt=`echo $opt | sed "s/NPROCPLUSONE/$((nproc+1))/"`
#echo "opt=$opt"

# ------ Create job script ------

echo "\
#!/bin/sh
cd `pwd`
date +'%d-%b-%Y %H:%M:%S'
lamboot \$PBS_NODEFILE
$prog $opt << EOF
erract:=abort
nproc=$nproc
$macro $* 
quit
EOF
lamhalt
echo
date +'%d-%b-%Y %H:%M:%S'
chmod go+rX *
" > $name.job
chmod +x $name.job


# ------ Submit job ------

rm -f $name.out
#echo "after=$after"
(if [ "$after" ]; then
   while [ ! -f $after ]; do sleep 10; done
 fi
 eval $submit
) &
