#!/usr/bin/env bash

#Optional Arguments for running batches of tests
NUM_SERV=${1:-1}
TYPE=${2:-sync}
CARS=${3:-2}
DBNAME=${4:-tmp}
NODES=${5:-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
	#Curr
	#mpirun --mca btl tcp,self --mca btl_tcp_if_include "$ITFC" --mca opal_warn_on_missing_libcuda 0 ./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"
	#Test
	mpirun --mca btl_openib_allow_ib 1 --mca opal_warn_on_missing_libcuda 0 --mca btl ^uct -np "$NODES" ./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
	#Test
	#mpirun --mca btl tcp,self --mca btl_tcp_if_include "$ITFC" --mca opal_warn_on_missing_libcuda 0 ./ag_gen -n ../Oct_2021/nm_files/"$CARS"_car_timeline_maintenance.nm -x ../Oct_2021/Non_Sync/4_Exploits/"$NUM_SERV"_Serv/generic_timeline_maintenance.xp -t 1 -q 1 -p -a 0.6 -z "$DBNAME"
	
	mpirun --mca btl_openib_allow_ib 1 --mca opal_warn_on_missing_libcuda 0 -np "$NODES" ./ag_gen -n ../Oct_2021/nm_files/"$CARS"_car_timeline_maintenance.nm -x ../Oct_2021/Non_Sync/4_Exploits/"$NUM_SERV"_Serv/generic_timeline_maintenance.xp -t 1 -q 1 -p -a 0.6 -z "$DBNAME"

else
	echo "Running default."
	mpirun --mca btl_openib_allow_ib 1 --mca opal_warn_on_missing_libcuda 0 ./ag_gen -n ../Oct_2021/nm_files/1_car_timeline_maintenance.nm -x ../Oct_2021/Sync/4_Exploits/1_Serv/sync_timeline_maintenance.xp -t 1 -q 1 -p -a 0.6 -z "$DBNAME"

fi


#dot -Tsvg new.dot > ag.svg

#dot -Tsvg DOTFILE.dot > ag.svg
