#!/bin/bash
source $HOME/.gamerc

[  "$1" == "" ] && exec echo "Usage: $0 <status|restore|change> <datetime>"

ACTION=$1
DATETIME=$2

if [ "$ACTION" != "status" -a "$ACTION" != "restore" -a "$ACTION" != "change" ]; then
	exec echo "Usage: $0 <status|restore|change> <datetime>"
fi

if [ "$ACTION" == "status" ]; then
	NOW_DATETIME=$(date +"%Y-%m-%d %H:%M:%S")
	echo "NOW DATETIME: ${NOW_DATETIME}"
	pl
	echo
else
	echo "Stop Game Servers..."
	TestCtrl stop
	echo
	
	if [ "$ACTION" == "restore" ]; then
		echo "Restore DateTime to Now"
		sudo /usr/sbin/ntpdate -u $NTP_SERVER
		echo
		
	elif [ "$ACTION" == "change" ]; then
		echo "Change DateTime to ${DATETIME}"
		sudo date -s "$DATETIME"
		echo
	fi

	PG_VERSION=$(psql --version | awk -F" " '{print $3}' | awk -F"." '{print $1}')
	
	echo "Restart Postgresql Database Server ${PG_VERSION} ..."
	sudo systemctl restart postgresql@${PG_VERSION}-main.service
	echo
	
	echo "Remove the logs on all servers current directory"
	find ~/servers1/*Server* -type f -name "*log*" -exec rm -f {} \;
	find ~/servers10/*Server* -type f -name "*log*" -exec rm -f {} \;
	find ~/servers20/*Server* -type f -name "*log*" -exec rm -f {} \;
	find ~/servers30/*Server* -type f -name "*log*" -exec rm -f {} \;
	echo
	
	echo "Start Game Servers now..."
	TestCtrl start
	echo
fi
