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

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

if [ "$HOST_NAME" == "TEST" ]; then
        TEST_SET_ID="9"
elif [ "$HOST_NAME" == "SUBMIT" ]; then
        TEST_SET_ID="8"
elif [ "$HOST_NAME" == "DATA" ]; then
        TEST_SET_ID="7"
elif [ "$HOST_NAME" == "TIME1" ]; then
        TEST_SET_ID="6"
elif [ "$HOST_NAME" == "MAPPING" ]; then
        TEST_SET_ID="5"
fi

ACTION=$1

for LAST; do true; done

if [ "$ACTION" == "status" ]; then
	echo "=== DB ===" | colorize white red
	echo "worlds" | colorize white blue
	psql -U postgres ${GAME_TYPE}Account -c "SELECT id, name, port, state, version, is_new, group_text, preset, time_zone FROM worlds ORDER BY id"
	echo "satellite_servers" | colorize white blue
	psql -U postgres ${GAME_TYPE}Account -c "SELECT * FROM satellite_servers ORDER BY set_id, id"
	echo "serverstatus" | colorize white blue
	psql -U postgres ${GAME_TYPE}DB1 -c "SELECT id, name, ext_address, ext_port, int_address, int_port, last_start_time FROM serverstatus ORDER BY id"
	echo "player_elfisland" | colorize white blue
	psql -U postgres ${GAME_TYPE}DB1 -c "SELECT worldserver, COUNT(*) FROM player_elfisland GROUP BY worldserver ORDER BY worldserver"
	echo "family" | colorize white blue
	psql -U postgres ${GAME_TYPE}DB1 -c "SELECT worldserver, COUNT(*) FROM family GROUP BY worldserver ORDER BY worldserver"	
	echo "node_stream" | colorize white blue
	psql -U postgres ${GAME_TYPE}DB1 -c "SELECT world_id, COUNT(*) FROM node_stream GROUP BY world_id ORDER BY world_id"	
	echo "iap" | colorize white blue
	psql -U postgres ${GAME_TYPE}Account -c "SELECT COUNT(*) FROM iap"

	echo ""
	echo "=== FILE ===" | colorize white green
        echo "GAMEDB" | colorize white cyan
        cat ~/servers1/setup.ini | egrep "GameDB="
	echo "WorldServer101" | colorize white cyan
	cat ~/servers1/WorldServer101/setup.ini | grep "WorldServerID="
	echo "WorldServer102" | colorize white cyan
	cat ~/servers1/WorldServer102/setup.ini | grep "WorldServerID="
	echo "ZoneServer101" | colorize white cyan
	cat ~/servers1/ZoneServer101/setup.ini | grep "ZoneServerID="
	echo "ZoneServer102" | colorize white cyan
	cat ~/servers1/ZoneServer102/setup.ini | grep "ZoneServerID="
	echo "MissionServer1" | colorize white cyan
	cat ~/servers1/setup.ini | grep "MissionServerID="
elif [ "$ACTION" == "restore" ]; then
	echo "worlds" | colorize white blue
	psql -U postgres ${GAME_TYPE}Account -c "UPDATE worlds SET id = ${TEST_SET_ID}10 WHERE port = 5567;"
	psql -U postgres ${GAME_TYPE}Account -c "UPDATE worlds SET id = ${TEST_SET_ID}20 WHERE port = 5569;"
	echo "satellite_servers" | colorize white blue
	psql -U postgres ${GAME_TYPE}Account -c "UPDATE satellite_servers SET set_id = 0;"

	echo "iap" | colorize white blue
	ssh TEST "pg_dump -U postgres ${GAME_TYPE}Account -t iap > /tmp/iap_test_sync.sql"
	rsync -avz TEST:/tmp/iap_test_sync.sql /tmp/
	psql -U postgres ${GAME_TYPE}Account -c "DROP TABLE iap"
	psql -U postgres ${GAME_TYPE}Account -c "\i /tmp/iap_test_sync.sql"

	echo "serverstatus" | colorize white blue
	psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${TEST_SET_ID}10 WHERE ext_port = 5567;"
	psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${TEST_SET_ID}11 WHERE ext_port = 6112;"
	psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${TEST_SET_ID}20 WHERE ext_port = 5569;"
	psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${TEST_SET_ID}21 WHERE ext_port = 6114;"
	echo "player_elfisland" | colorize white blue
	psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE player_elfisland SET worldserver = ${TEST_SET_ID}20;"
	echo "family" | colorize white blue
	psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE family SET worldserver = ${TEST_SET_ID}20;"
	echo "node_stream" | colorize white blue
	psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE node_stream SET world_id = '${TEST_SET_ID}20';"

	echo "WorldServer101" | colorize white cyan
	sed -i -r "s/WorldServerID=([0-9]+)$/WorldServerID=${TEST_SET_ID}10/" ~/servers1/WorldServer101/setup.ini
	echo "WorldServer102" | colorize white cyan
	sed -i -r "s/WorldServerID=([0-9]+)$/WorldServerID=${TEST_SET_ID}20/" ~/servers1/WorldServer102/setup.ini
	echo "ZoneServer101" | colorize white cyan
	sed -i -r "s/ZoneServerID=([0-9]+)$/ZoneServerID=${TEST_SET_ID}11/" ~/servers1/ZoneServer101/setup.ini
	echo "ZoneServer102" | colorize white cyan
	sed -i -r "s/ZoneServerID=([0-9]+)$/ZoneServerID=${TEST_SET_ID}21/" ~/servers1/ZoneServer102/setup.ini
	echo "MissionServer1" | colorize white cyan
	sed -i -r "s/MissionServerID=([0-9]+)$/MissionServerID=1/" ~/servers1/setup.ini	
	
	if [ "$LAST" != "norestart" ]; then
		yes | TestCtrl restart
	fi
elif [ "$ACTION" == "change" ]; then
	if [ "$2" -ne 0 ]; then
		ORIGINAL_SET_ID=$2
		NEW_SET_ID=$3
	else
		NEW_SET_ID=$3
	fi

	if [ -z "$ORIGINAL_SET_ID" ]; then
		echo "worlds" | colorize white blue
		psql -U postgres ${GAME_TYPE}Account -c "UPDATE worlds SET id = ${NEW_SET_ID}010 WHERE id = ${TEST_SET_ID}10;"
		psql -U postgres ${GAME_TYPE}Account -c "UPDATE worlds SET id = ${NEW_SET_ID}020 WHERE id = ${TEST_SET_ID}20;"
		echo "satellite_servers" | colorize white blue
		psql -U postgres ${GAME_TYPE}Account -c "UPDATE satellite_servers SET set_id = ${NEW_SET_ID} WHERE set_id = 0;"
		echo "iap" | colorize white blue
		psql -U postgres ${GAME_TYPE}Account -c "TRUNCATE iap;"
		echo "serverstatus" | colorize white blue
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${NEW_SET_ID}010 WHERE id = ${TEST_SET_ID}10;"
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${NEW_SET_ID}011 WHERE id = ${TEST_SET_ID}11;"
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${NEW_SET_ID}020 WHERE id = ${TEST_SET_ID}20;"
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${NEW_SET_ID}021 WHERE id = ${TEST_SET_ID}21;"
		echo "player_elfisland" | colorize white blue
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE player_elfisland SET worldserver = ${NEW_SET_ID}020;"
		echo "family" | colorize white blue
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE family SET worldserver = ${NEW_SET_ID}020;"
		echo "node_stream" | colorize white blue
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE node_stream SET world_id = '${NEW_SET_ID}020';"
		
		echo "WorldServer101" | colorize white cyan
		sed -i "s/WorldServerID=${TEST_SET_ID}10/WorldServerID=${NEW_SET_ID}010/" ~/servers1/WorldServer101/setup.ini 
		echo "WorldServer102" | colorize white cyan
		sed -i "s/WorldServerID=${TEST_SET_ID}20/WorldServerID=${NEW_SET_ID}020/" ~/servers1/WorldServer102/setup.ini
		echo "ZoneServer101" | colorize white cyan
		sed -i "s/ZoneServerID=${TEST_SET_ID}11/ZoneServerID=${NEW_SET_ID}011/" ~/servers1/ZoneServer101/setup.ini
		echo "ZoneServer102" | colorize white cyan
		sed -i "s/ZoneServerID=${TEST_SET_ID}21/ZoneServerID=${NEW_SET_ID}021/" ~/servers1/ZoneServer102/setup.ini
		echo "MissionServer1" | colorize white cyan
		sed -i "s/MissionServerID=1/MissionServerID=${NEW_SET_ID}/" ~/servers1/setup.ini
	else
		echo "worlds" | colorize white blue
		psql -U postgres ${GAME_TYPE}Account -c "UPDATE worlds SET id = ${NEW_SET_ID}010 WHERE id = ${ORIGINAL_SET_ID}010;"
		psql -U postgres ${GAME_TYPE}Account -c "UPDATE worlds SET id = ${NEW_SET_ID}020 WHERE id = ${ORIGINAL_SET_ID}020;"
		echo "satellite_servers" | colorize white blue
		psql -U postgres ${GAME_TYPE}Account -c "UPDATE satellite_servers SET set_id = ${NEW_SET_ID} WHERE set_id = ${ORIGINAL_SET_ID};"
		echo "iap" | colorize white blue
		psql -U postgres ${GAME_TYPE}Account -c "TRUNCATE iap;"
		echo "serverstatus" | colorize white blue
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${NEW_SET_ID}010 WHERE id = ${ORIGINAL_SET_ID}010;"
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${NEW_SET_ID}011 WHERE id = ${ORIGINAL_SET_ID}011;"
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${NEW_SET_ID}020 WHERE id = ${ORIGINAL_SET_ID}020;"
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE serverstatus SET id = ${NEW_SET_ID}021 WHERE id = ${ORIGINAL_SET_ID}021;"
		echo "player_elfisland" | colorize white blue
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE player_elfisland SET worldserver = ${NEW_SET_ID}020;"
		echo "family" | colorize white blue
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE family SET worldserver = ${NEW_SET_ID}020;"
		echo "node_stream" | colorize white blue
		psql -U postgres ${GAME_TYPE}DB1 -c "UPDATE node_stream SET world_id = '${NEW_SET_ID}020';"
		
		echo "WorldServer101" | colorize white cyan
		sed -i "s/WorldServerID=${ORIGINAL_SET_ID}010/WorldServerID=${NEW_SET_ID}010/" ~/servers1/WorldServer101/setup.ini 
		echo "WorldServer102" | colorize white cyan
		sed -i "s/WorldServerID=${ORIGINAL_SET_ID}020/WorldServerID=${NEW_SET_ID}020/" ~/servers1/WorldServer102/setup.ini
		echo "ZoneServer101" | colorize white cyan
		sed -i "s/ZoneServerID=${ORIGINAL_SET_ID}011/ZoneServerID=${NEW_SET_ID}011/" ~/servers1/ZoneServer101/setup.ini
		echo "ZoneServer102" | colorize white cyan
		sed -i "s/ZoneServerID=${ORIGINAL_SET_ID}021/ZoneServerID=${NEW_SET_ID}021/" ~/servers1/ZoneServer102/setup.ini
		echo "MissionServer1" | colorize white cyan
		sed -i "s/MissionServerID=${ORIGINAL_SET_ID}/MissionServerID=${NEW_SET_ID}/" ~/servers1/setup.ini
	fi

	if [ "$LAST" != "norestart" ]; then
		yes | TestCtrl restart
	fi
else
	exec echo "Usage: $0 <status|restore|change> <orginal_set_id> <new_set_id> <norestart>"
fi
