#!/bin/bash
 
# get arguments
JOB=$1
if [ -z $JOB ]; then
  echo "Job file not specified. Quitting."
  exit 1
fi
JOB_DIR=$2
if [ -z $JOB_DIR ]; then
   JOB_DIR=`pwd`
fi

# get username
USR=`whoami`

#####set up scratch directory 
   GAUSS_SCRDIR="/home/XXX/scratch"
if [[ ! (-a ${GAUSS_SCRDIR}) ]]; then
   mkdir ${GAUSS_SCRDIR} 
fi

#####set up G03 environment. g09root points to root directory of G09.
g09root="/home/XXX/g09exe"
export g09root GAUSS_SCRDIR
. $g09root/g09/bsd/g09.profile

export GAUSS_DIR="$g09root/g09"
export GAUSS_EXE="$GAUSS_DIR/g09"

# set up file names
COM_FILE="${JOB_DIR}/${JOB}.com"
LOG_FILE=${JOB}.log
CHK_FILE=${JOB}.chk
CHK_FILE1="${JOB_DIR}/${JOB}.chk"

#check whether ${JOB}.com actually exists.
if [[ ! (-a ${COM_FILE}) ]]; then
  echo "File ${COM_FILE} does not exist. Quitting."
  exit 1
fi

#copy an existing checkpoint file
if [[ (-a ${CHK_FILE1}) ]]; then
   cp ${CHK_FILE1} ${GAUSS_SCRDIR} 
fi

#move to scratch directory and run job
cd ${GAUSS_SCRDIR}
${GAUSS_EXE} <${COM_FILE} >${LOG_FILE}

#copy files back to user directory
mv ${LOG_FILE} ${JOB_DIR}
if [[ (-a ${CHK_FILE}) ]]; then
   mv ${CHK_FILE} ${JOB_DIR}
fi



