#!/bin/bash 

# otree sys conf
source "$HOME/.oTree/venv/bin/activate"
source "$HOME/otree_environ_config"

################
# DESKTOP link
################
function xdisplay {
killall otree
killall daphne

# checking if otree is installed and usable
if ! hash otree &>/dev/null;
then
zenity --question --title="No oTree" --text="oTree does not appear to be installed in the current\nvirtual environment.\nDo you want me to try and install oTree for you?\n"
case $? in
0) xterm -e "pip3 install -U psycopg2 otree-core"
;;
1) zenity --info --title="Check installation" --text="Please install oTree manually before continuing with this script."
exit 1
;;
esac
fi

if ! hash otree &>/dev/null;
then
zenity --error --text="Couldn't complete oTree installation.\nPlease check manually!"
exit 1
fi

zenity --question --title="collectstatic" --text="Are you running this experiment for the first time in this environment\nor did you add any statics\n\n  - images, stylesheets, scripts, etc.- \n\nsince you last ran it here?" --cancel-label="Do not collect" --ok-label="Collect"
case $? in
0) (cd "$HOME/.oTree";otree collectstatic --noinput)
;;
esac

tstamp=$(date +%Y%m%d-%T)
zenity --title="Reset Database" --question --text="Do you require a <b>database</b> reset?" --cancel-label="Do not reset" --ok-label="Reset"
case $? in
0)      echo -e "Creating backup of current database!"
	pg_dump "$DB_NAME" | 7za a -si "$HOME/DB_BackUps/pg_$tstamp.7z"
	(cd "$HOME/.oTree";otree resetdb --noinput)
;;
esac

zenity --title="Run(Prod)Server" --question --text="Do you want to start the\nprogramming server (runserver) or the \nproduction server (runprodserver)?" --ok-label="Production" --cancel-label="Programming"
case $? in
1) (cd "$HOME/.oTree";otree runserver "$DAPHNE_PORT")
;;
0) (cd "$HOME/.oTree";otree runprodserver --port="$DAPHNE_PORT")
;;
*) exit 1
;;
esac
}

###############################
# CONSOLE
###############################
function ttydisplay () {
if [ ! "$1" = "-h" ]
then
	killall otree
	killall daphne
fi

# check if otree is installed
if ! hash otree &>/dev/null;
then
printf "oTree does not appear to be installed in the current virtual environment."
read -p "Do you want me to try and install oTree for you? [Y/n]: " INSTALL
case $INSTALL in
[nN]* ) printf "Please install oTree manually before continuing with this script."
;;
* ) pip3 install -U psycopg2 otree-core
;;
esac
fi


if [ $# -eq 0 ]
then
	read -p "Do you require 'collectstatic' to run? [y/N]: " YN
	case $YN in
	[yY]* ) (cd "$HOME/.oTree";otree collectstatic --noinput)
	;;
	esac

	tstamp=$(date +%Y%m%d-%T)
	read -p "Do you require a DATABASE RESET ? [y/N]: " YN
	case $YN in
	[yY]* )      echo -e "Creating backup of current database!"
		pg_dump "$DB_NAME" | 7za a -si "$HOME/DB_BackUps/pg_$tstamp.7z"
		(cd "$HOME/.oTree";otree resetdb --noinput)
	;;
	esac
	read -p "Launch otree in detached screen? [y/N] " YN
	case $YN in
	[yY]* ) SCREEN="1"
	;;
	esac
	read -p "Send email if otree stops? [y/N] " YN
	case $YN in
	[yY]* ) MAIL="1"
	;;
	esac
	read -p "Start local server (programming)? [y/N] " YN
	case $YN in
	[yY]* ) LOCAL="1"
	;;
	esac
fi
for arg in "$@"
do
	case $arg in
	"-c" )  (cd "$HOME/.oTree";otree collectstatic --noinput)
	;;
	"-h" )	HELP="1"
	;;
	"-l" )  LOCAL="1"
	;;
	"-m" )  MAIL="1"
	;;
	"-r" )  echo -e "Creating backup of current database!"
		pg_dump "$DB_NAME" | 7za a -si "$HOME/DB_BackUps/pg_$tstamp.7z"
		(cd "$HOME/.oTree";otree resetdb --noinput)
	;;
	"-s" )  SCREEN="1"
	;;
	esac
done


if [ "$HELP" = "1" ]
 then
 printf "
	otree_restart [OPTIONS]

	-c	collectstatic
	-m	send mails on crash to account defined in otree_environ_config
	-l	run programming server only (runserver)
	-p      run only prodserver (no collectstatic / resetdb)
	-r	resetdb
	-s	run in detached screen named 'otree'
	-h	help
\n
"
elif [ "$LOCAL" = "1" ]
then

(cd "$HOME/.oTree";otree runserver "$DAPHNE_PORT")

else
	if [ "$SCREEN" = "1" ]
	then
		if [ "$MAIL" = "1" ]
		then
			printf "Will send email on otree stop."
			(cd "$HOME/.oTree";screen -S otree -d -m otree runprodserver --port="$DAPHNE_PORT") && 
			echo "otree has stopped."
			mail -s "server has stopped" "$USER_EMAIL" <<< "Warning!!! The experiment on server port $DAPHNE_PORT has stopped."
		else
			
			(cd "$HOME/.oTree";screen -S otree -d -m otree runprodserver --port="$DAPHNE_PORT")
		fi
	else
		if [ "$MAIL" = "1" ]
		then
			printf "Will send email on otree stop."
			(cd "$HOME/.oTree";otree runprodserver --port="$DAPHNE_PORT") && 
			echo "otree has stopped."
			mail -s "server has stopped" "$USER_EMAIL" <<< "Warning!!! The experiment on server port $DAPHNE_PORT has stopped."
		else
			
			(cd "$HOME/.oTree";otree runprodserver --port="$DAPHNE_PORT")
		fi

	fi
fi
}




case $1 in
"-x" ) xdisplay;;
* ) ttydisplay "$@";;
esac


