#!/usr/bin/env bash

#Optional Arguments for running batches of tests
NUM_SERV=${1:-1}
TYPE=${2:-sync}
CARS=${3:-2}
DBNAME=${4:-tmp}
NUM_THREADS=${5:-1}
NODES=${6:-2}

ITFC="eth0"

strval1="sync"
strval2="non-sync"

#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

	ITFC="wlp2s0"
fi

#./ag_gen -n ../car_examples/generic_timeline_maintenance.nm -x ../car_examples/tmp.xp -g DOTFILE.dot -t 1 -q 1
#./ag_gen -n ../car_examples/generic_maintenance.nm -x ../car_examples/generic_maintenance.xp -g DOTFILE.dot -t 1 -q 1
#./ag_gen -n ../fpga_examples/Network_5/car_network5.nm -x ../fpga_examples/Network_5/car_network5.xp -g DOTFILE.dot -t 1 -q 1

if [ "$TYPE" == "$strval1" ]; then
	#Old
	#./ag_gen -n ../Feb_2021/generic_timeline_maintenance.nm -x ../Feb_2021/Sync/"$NUM_SERV"_Serv/sync_timeline_maintenance.xp -g DOTFILE.dot -t 1 -q 1 -p -a 0.9

	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 -n ../Oct_2021/nm_files/"$CARS"_car_timeline_maintenance.nm -x ../Oct_2021/Sync/10_Exploits/"$NUM_SERV"_Serv/sync_timeline_maintenance.xp -t "$NUM_THREADS" -q 1 -p -a 0.6 -z "$DBNAME"

	# 4 Exploit	
	#mpiexec -np "$NODES" --bind-to numa --map-by numa ./ag_gen -n ../Oct_2021/nm_files/"$CARS"_car_timeline_maintenance.nm -x ../Oct_2021/Sync/4_Exploits/"$NUM_SERV"_Serv/sync_timeline_maintenance.xp -t 1 -q 1 -p -a 0.6 -z "$DBNAME"


elif [ "$TYPE" == "$strval2" ]; then
	#./ag_gen -n ../Feb_2021/generic_timeline_maintenance.nm -x ../Feb_2021/Non_Sync/"$NUM_SERV"_Serv/generic_timeline_maintenance.xp -g DOTFILE.dot -t 1 -q 1 -p -a 0.9
	
	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 -n ../Oct_2021/nm_files/"$CARS"_car_timeline_maintenance.nm -x ../Oct_2021/Non_Sync/10_Exploits/"$NUM_SERV"_Serv/generic_timeline_maintenance.xp -t "$NUM_THREADS" -q 1 -p -a 0.6 -z "$DBNAME"

else
	echo "Running default."
	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 --timeout 129600 ./ag_gen -n ../Oct_2021/nm_files/1_car_timeline_maintenance.nm -x ../Oct_2021/Sync/10_Exploits/1_Serv/sync_timeline_maintenance.xp -t "$NUM_THREADS" -q 1 -p -a 0.6 -z "$DBNAME"

fi


#dot -Tsvg new.dot > ag.svg

#dot -Tsvg DOTFILE.dot > ag.svg
