#!/bin/bash
#
# GAUSSIAN09 SUBMISSION SCRIPT
#
# Author: ODU Research Computing Services
#
# Usage: g09slurm INPUT_FILE.com [OUTPUT_FILE.out] [START_TIME]


usage () {
        echo "Usage: g09slurm INPUT_FILE.com [OUTPUT_FILE.out] [START_TIME]"
        echo "Please refer to https://wiki.hpc.odu.edu/Software/Gaussian for more information"
}

if [ "$1" == --help ]; then
        echo "g09slurm - Gaussian09 submission script"
        usage
        exit 0
fi

if [ $# -lt 1 ]; then
	echo "Error: Missing Gaussian G09 input file"
        usage >&2
	exit 1
fi

input_file=$1
output_file=$2
begin_date=$3


if [ ! -r $1  ]; then
	echo "Error: Unable to open Gaussian G09 input file"
        usage >&2
	exit 2
fi

if [ -z "$output_file" ]; then
	output_file=${input_file%.[Cc][Oo][Mm]}.out
fi


# Detect & support multicore Gaussian calculations

ncpus=$(grep -E '^ *%' "$1" | grep -i -w nprocshared | awk '{ L = tolower($0); print gensub("%nprocshared=","","G", L)}')

if [ -z "$ncpus" ]; then
	ncpus=$(grep -E '^ *%' "$1" | grep -i -w 'nprocs?' | awk '{ L = tolower($0); print gensub("%nprocs?=","","G", L)}')
fi

if [ -z "$ncpus" ]; then
	ncpus=1
fi


# Allow delayed start

if [ -z "$begin_date" ]; then
	begin_date=now
fi

if [ -f "$output_file" ]; then
        read -p "Output file $output_file already exists, do you want overwrite? [N/y]" confirm

        if [ "$confirm" = "y" ]; then
                > "$output_file"
        else
                echo "Cancelling job submission" >&2
                exit 1
        fi
fi

CLUSTER=$(cat /etc/cluster)
case "$CLUSTER" in
    wahab)
        SBATCH=/shared/apps/common/slurm/current/bin/sbatch
        ;;
    turing)
        SBATCH=/cm/shared/applications/slurm/current/bin/sbatch
        ;;
    *)
        echo "Error: unsupported cluster. Please contact itshelp@odu.edu for assistance." >&2
        exit 2
        ;;
esac

GAUSSIAN_SCRIPT=/cm/shared/apps/gaussian/g09revD.01/script/g09/script/g09.slurm

"$SBATCH" \
	--job-name="G09-$input_file" \
	--ntasks=1                 \
	--cpus-per-task=$ncpus     \
	--output="$output_file"	   \
	--begin=$begin_date	   \
	"$GAUSSIAN_SCRIPT"  "$input_file"

