#!/bin/bash

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

if [ $# -lt 2 ]; then
	echo "Error: require a filename for Gaussian G09 output"
	exit -1
fi

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

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

ncpus=$(grep -w nprocshared ./$1|awk '{print gensub("%nprocshared=","","G")}'|grep -v !)

if [ -z "$ncpus" ]; then
	ncpus=$(grep -w nproc ./$1|awk '{print gensub("%nproc=","","G")}'|grep -v !)
fi

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

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

#--partition=phi	           \

/shared/apps/common/slurm/current/bin/sbatch \
	--job-name=G09-$input_file \
	--ntasks=1                 \
	--cpus-per-task=$ncpus     \
	--output=$output_file	   \
	--begin=$begin_date	   \
	/cm/shared/apps/gaussian/g09revD.01/script/g09/script/g09.slurm $input_file

