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

set -e

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


# Detect & support custom directories

collect_scratch_objects () {
	# Usage: cat $FILE | collect_scratch_objects LINK0_KEYWORD
	# Parses line like this:
	#     %RWF=/dalton/s0/
	#     %RWF=/dalton/s0/,4GB,/scratch/,3GB,/temp/s0/my_job,-1
	# and prints the scratch dirs/files found to the standard output
	#
	# The first argument is the keyword to look for, and it must
	# be must be in lowercase
	local KWD="$1"
	awk 'tolower($0) ~ /^ *% *'$KWD' *=/ {
	    A = $0
	    # Erase keyword
	    sub(/^[^=]*= */, "", A)
	    # Erase comment, if any
	    sub(/ *\!.*$/, "", A)
	    Args_count = split(A, Args, ",")
	    # print out the directory/file parts
	    for (i = 1; i <= Args_count; ++i)
	        print(Args[i])
	    }'
}

create_scratch_dirs () {
	# Usage: create_scratch_dir "$scratch_dir_lines" LINK0_KEYWORD
	local SCR_DIRS="$1"
	local KWD="$2"
	if [ -n "$SCR_DIRS" ]; then
		echo "Found scratch dirs for $KWD:"
		local IFS=$'\n'
		for D in $SCR_DIRS; do
			echo "  $D"
			case "$D" in
			*/)
				mkdir -p -v "$D"
				;;
			*)
				mkdir -p -v "$(dirname "$D")"
				;;
			esac
		done
	fi
}


rwf_scratch_dirs=$(cat "$input_file" | collect_scratch_objects rwf)
create_scratch_dirs "$rwf_scratch_dirs"  "RWF files"

int_scratch_dirs=$(cat "$input_file" | collect_scratch_objects int)
create_scratch_dirs "$int_scratch_dirs"  "INT files"

d2e_scratch_dirs=$(cat "$input_file" | collect_scratch_objects d2e)
create_scratch_dirs "$d2e_scratch_dirs"  "D2E files"


# 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"