#!/bin/bash

CLUSTER_NAME=DesignGalaxy
CENTRAL_SERVER_ADDRESS=swo-dev7.station.sony.com
CENTRAL_GAME_PORT=44471
CENTRAL_PLANET_PORT=44475
GAME_SCENE=naboo.trn
PLANET_NAME=$USER
LOGIN_SERVER_ADDRESS=swo-dev6.station.sony.com
EXE_DIR=~/swg/current/exe/linux

get_free_port()
{
	PORT=(RANDOM + 1024)
	((PORT=$PORT+1))
	AVAIL=`netstat -aut|grep $PORT`
	while [ -n "$AVAIL" ];
	do
	{
		((PORT=RANDOM))
		AVAIL=`netstat -aut|grep $PORT`
	};
	done
	echo $PORT
}

run_gameserver()
{
    cd $EXE_DIR

    echo "** Starting GameServer at " $(date +"%Y%m%d-%k.%M.%S") 
	./SwgGameServer_d -- @servercommon.cfg @combat.cfg \
		-s GameServer sceneID=$PLANET_NAME \
		centralServerAddress=$CENTRAL_SERVER_ADDRESS \
	        centralServerPort=$CENTRAL_GAME_PORT \
		groundScene=terrain/${GAME_SCENE} \
		clusterName=$CLUSTER_NAME \
		useTemplates=1 \
		regenerationRate=0.00532 \
		scriptPath=../../data/sku.0/sys.server/compiled/game \
		useRemoteDebugJava=0 \
		javaDebugPort=0 \
		javaConsoleDebugMessages=1\
		-s SharedFoundation \
		frameRateLimit=4 \
		warningCallStackDepth=0 \
		-s SharedDebug \
		lookupCallStack=1 &
}

run_planetserver()
{
    GAME_SERVICE_PORT=(get_free_port);
    cd $EXE_DIR
    echo "** Starting PlanetServer at " $(date +"%Y%m%d-%k.%M.%S") " gameserviceport=${GAME_SERVICE_PORT}" 
	./PlanetServer_d -- \
	-s PlanetServer sceneID=$PLANET_NAME \
	clusterName=$CLUSTER_NAME \
	centralServerAddress=$CENTRAL_SERVER_ADDRESS \
	centralServerPort=$CENTRAL_PLANET_PORT \
	gameServicePort=0 &
}

fatality()
{
	CRASHDIR="/swo/swg/CRASH-"`date +"%Y%m%d-%k.%M.%S"`
	echo ""
	echo "**********************************************************************"
	echo "**      !!FATAL WARNING FATAL WARNING FATAL WARNING FATAL WARNING!!"     
	echo "**"
	echo "** $1 "
	echo "**"
	echo "** Server is no longer running. It failed to properly start."
	echo "** If this is an unknown bug, advise the server development team."
	echo "**"
	echo "** A crash directory called $CRASHDIR"
	echo "** is being created, logs and the core file will be placed in "
	echo "** $CRASHDIR"
	echo "**"
	echo "**********************************************************************"
	echo ""
	echo -n "Killing servers ..."
	mkdir $CRASHDIR
	cp /swo/swg/*.log $CRASHDIR
	cp /swo/swg/current/exe/linux/core $CRASHDIR
	kill_servers
	echo "done."
	
}

#-----------------------------------------
#-- parse options
#-----------------------------------------

show_usage ()
{

    echo "Options:"
    echo "         -h            help"
    echo "         -s <scene>    scenefile (default: $GAME_SCENE)"
    echo "         -x <exe dir>  executable dir name (default: $EXE_DIR)"
    echo "         -n <nbname>   nbname dir (default: $NBNAME)"
    echo "         -p <planetname> planet name (default: $PLANET_NAME)"
}

while getopts "hs:x:n:p:" arg
do
    case $arg in
	"s")
	    GAME_SCENE=$OPTARG
	    echo "Setting Game Scene to: $GAME_SCENE"
	    ;;
	"x")
	    EXE_DIR=$OPTARG
	    echo "Setting Executable Dir to: $EXE_DIR"
	    ;;
	"p")
	    PLANET_NAME=$OPTARG
	    echo "Setting Planet Name to: $PLANET_NAME"
	    ;;
	"h")
	    show_usage;
	    ;;
	*)
	    show_usage;
	    ;;
    esac
done

#-----------------------------------------
#-----------------------------------------

echo "Port selection:"

echo -n "Starting planet server planet=${PLANET_NAME}...                    "
run_planetserver
echo "done."
sleep 2
echo -n "Starting game server planet=${PLANET_NAME} scene=${GAME_SCENE} ... "
run_gameserver

echo "done"
echo ""
echo "**********************************************************************"
echo "You may now connect to your game cluster [ $CLUSTER_NAME ]."
echo "Your client configuration should have an entry "
echo "loginServerAddress=$LOGIN_SERVER_ADDRESS "
echo ""
echo "If you are connecting to a different login server"
echo "ensure that servercommon.cfg in your depot reflects the"
echo "alternate server address, and that your client config"
echo "matches."
echo ""
echo "Contact Justin Randall or Chris Mayer if you are having trouble"
echo "**********************************************************************"


