#!/usr/bin/env bash

#Optional Arguments for running batches of tests
NUM_SERV=${1:-foo}
TYPE=${2:-foo2}
CARS=${3:-foo3}
DBNAME=${4:-foo4}

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

if [ "$HOSTNAME" = "login" ]; then 
	#Database stored on node with port
	if psql -p 5240 -h login ${DBNAME} -c '\q' 2>&1; then
		echo "Database ${DBNAME} exists. Dropping and recreating from dump."
	   
		psql -p 5240 -h login postgres -c "DROP DATABASE "$DBNAME""
		psql -p 5240 -h login postgres -c "CREATE DATABASE "$DBNAME" WITH OWNER = ag_gen"
		psql -q -p 5240 -h login "$DBNAME" < schema.sql
	else
		echo "Database ${DBNAME} does not exist. Creating and importing from dump."
	   
 		psql -p 5240 -h login postgres -c "CREATE DATABASE "$DBNAME" WITH OWNER = ag_gen"
		psql -q -p 5240 -h login "$DBNAME" < schema.sql
	fi
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

#./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
	#./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
	./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"
fi

if [ "$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
	mpirun --mca btl tcp,self --mca btl_tcp_if_include eth0 ./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" -m 2
fi

#dot -Tsvg new.dot > ag.svg

#dot -Tsvg DOTFILE.dot > ag.svg
