#!/usr/bin/env bash

#Optional Arguments for running batches of tests.
#   These are meant to be specified by command line, but the :-{VAL} specifies the default to use.
#   Can change the default here as desired. Ex: Changing to 2 Services would be: NUM_SERV=${1:-2}
NUM_SERV=${1:-1}
TYPE=${2:-sync} # sync or non-sync. Use sync unless you are specifically testing non-sync.
CARS=${3:-2} # used for network model scripting. Can ignore.
DBNAME=${4:-ag} # name of database table. can leave as default or change to whatever you prefer
NUM_THREADS=${5:-1} # number of OMP threads to use
NODES=${6:-1} # number of MPI nodes to use (for tasking or subgraphing)
MPI_TYPE=${7-serial} # tasking or subgraphing. Put serial or OMP if you do not wish to use MPI.
ESIZE=${8:-6} # exploit file scripting. Can ignore.

# Avoid multiple users fighting over same database if default name is used (hacky solution)
DBNAME="$DBNAME$USER"

### AG Gen Network Model and Exploit File settings - adjust as desired.
NM="../examples/WannaStripped.nm"
XP="../examples/WannaStripped.xp"
SYNC_XP="../examples/WannaStripped.xp"

### MPI Parameters - adjust as desired.
MPI_PARAMS="mpiexec --mca btl_openib_allow_ib 1 --mca opal_warn_on_missing_libcuda 0 -np ${NODES} --timeout 129600"
# Common Hammer parameters
MPI_HAMMER_PARAMS="mpiexec --mca btl_openib_allow_ib 1 --mca btl openib,self,vader --mca opal_warn_on_missing_libcuda 0 --bind-to numa --map-by numa -np ${NODES} --timeout 129600"

### AG Gen Parameters. Please run the AG generator with the '-h' parameter to see all options. Adjust as desired.
AG_PARAMS="-t ${NUM_THREADS} -q 1 -p -a 0.6 -z ${DBNAME}"
AG_SUBG_PARAMS="-t ${NUM_THREADS} -q 1 -p -a 0.6 -z ${DBNAME} -l 200 -s"
AG_TASK_PARAMS="-t ${NUM_THREADS} -q 1 -p -a 0.6 -z ${DBNAME} -e"

# Do you want to create an SVG from the DOTFILE? Uncomment/comment to select
CREATE_SVG="true"
#CREATE_SVG="false"

# If using MPI Subgraphing, do you want to use a DHT? Uncomment to select, and please adjust the BCL Hash Size
#DHT="true"
DHT="false"
BCLHASH=2300

### Running
# Var concats
if [ "$DHT" == "true" ]; then
	AG_SUBG_PARAMS+=" -uf "
	AG_SUBG_PARAMS+=${BCLHASH}
fi

if [ "$CREATE_SVG" == "true" ]; then
	AG_PARAMS+=" -g DOTFILE.dot"
	AG_SUBG_PARAMS+=" -g DOTFILE.dot"
	AG_TASK_PARAMS+=" -g DOTFILE.dot"
fi

if [ "$TYPE" == "sync" ]; then
	XP="$SYNC_XP"
fi

## Database Work
#Hammer
if [ "$(dnsdomainname)" = "hammer.esg.utulsa.edu" ]; then 
	#Database stored on node with port
	if psql -p 5240 -h compute03 ${DBNAME} -c '\q' 2>&1; then
		echo "Database ${DBNAME} exists. Dropping and recreating from dump."
	   
		psql -p 5240 -h compute03 postgres -c "DROP DATABASE "$DBNAME""
		psql -p 5240 -h compute03 postgres -c "CREATE DATABASE "$DBNAME" WITH OWNER = ag_gen"
		psql -q -p 5240 -h compute03 "$DBNAME" < schema.sql
	
	else
		echo "Database ${DBNAME} does not exist. Creating and importing from dump."
	   
 		psql -p 5240 -h compute03 postgres -c "CREATE DATABASE "$DBNAME" WITH OWNER = ag_gen"
		psql -q -p 5240 -h compute03 "$DBNAME" < schema.sql
	fi
#Local
else
	if psql ${DBNAME} -c '\q' 2>&1; then
		echo "Database ${DBNAME} exists. Dropping and recreating from dump."

		psql postgres -c "DROP DATABASE "$DBNAME""
		psql postgres -c "CREATE DATABASE "$DBNAME" WITH OWNER = ag_gen"
		psql -q "$DBNAME" < schema.sql
	else
		echo "Database ${DBNAME} does not exist. Creating and importing from dump."
	
		psql postgres -c "CREATE DATABASE "$DBNAME" WITH OWNER = ag_gen"
		psql -q "$DBNAME" < schema.sql
	fi
fi

## Running the scripts
# NOTE: Do not use 'bash -c' or 'eval' unless you fully understand shell parsing. We are building an array
# of arguments dynamically to avoid wordsplitting and wildcard expansions
CMD=()
if [ "$(dnsdomainname)" = "hammer.esg.utulsa.edu" ]; then 
    CMD+=$MPI_HAMMER_PARAMS
	CMD+=" ./ag_gen -n "
	CMD+=${NM}
	CMD+=" -x "
	CMD+=${XP}
	CMD+=" "

	if [ "$MPI_TYPE" == subgraphing ]; then
		CMD+=${AG_SUBG_PARAMS}
		${CMD}
    elif [ "$MPI_TYPE" == tasking ]; then
		CMD+=${AG_TASK_PARAMS}
		${CMD}
	elif [ "$MPI_TYPE" == testing ]; then
		CMD+=${AG_PARAMS}
		${CMD}
	else
		CMD+=${AG_PARAMS}
		${CMD}
	#Serial/OMP
	fi #Subg/Task/Serial/OMP

#Local
else
	CMD+=${MPI_PARAMS}
	CMD+=" ./ag_gen -n "
	CMD+=${NM}
	CMD+=" -x "
	CMD+=${XP}
	CMD+=" "

	if [ "$MPI_TYPE" == subgraphing ]; then 
		CMD+=${AG_SUBG_PARAMS}
		${CMD}
	elif [ "$MPI_TYPE" == tasking ]; then
		CMD+=${AG_TASK_PARAMS}
		${CMD}
	elif [ "$MPI_TYPE" == testing ]; then
		CMD+=${AG_PARAMS}
		${CMD}
	else
	#Serial/OMP
		CMD+=${AG_PARAMS}
		${CMD}	
	fi
fi #END Hammer/Non-Hammer

#Graphviz Strict graphing to avoid duplicate nodes and edges
if [ "$CREATE_SVG" = "true" ]; then
	echo -n 'strict ' | cat - DOTFILE.dot > temp && mv temp DOTFILE.dot
	#dot -Tsvg new.dot > ag.svg
	dot -Tsvg DOTFILE.dot > ag.svg
fi
