#!/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)
	echo "NOW DATETIME: ${NOW_DATETIME}"
	pl
else
	TestCtrl stop
	
	if [ "$ACTION" == "restore" ]; then
		sudo /usr/sbin/ntpdate -u $NTP_SERVER
		
	elif [ "$ACTION" == "change" ]; then
		sudo date -s "$DATETIME"
	fi

	PG_VERSION=$(psql --version | awk -F" " '{print $3}' | awk -F"." '{print $1}')
	
	sudo systemctl restart postgresql@${PG_VERSION}-main.service
	
	find ~/servers1/*Server* -type f -name "*log*" -exec rm -f {} \;
	
	TestCtrl start
fi
