#! /bin/sh
#
# AA Server Cron Job (originally TeamSpeak Cron Job)
#
# Usage: launch-server <configfile>
# EG:    launch-server ctf.cfg
#
# Author: Chris Childers
# E-Mail: Chris@darkstarllc.com
# Modified by: Fafa Paku
# Modified by: Tony Jackson
#
# Set the port number in the .cfg if running multiple servers.
# Script checks the existance of the cfg file before launching.

# Uncomment this line if you want to get core dumps
#ulimit -c unlimited

### Set your default AA Root Directory
aadir="/home/aa/alienarena2007"

### Set your AA Binary Name or command string ($1 is the argument
### passed to the script)
aabin="crded +set game arena +exec $1"

########## you probably don't need to change anything below here 
##########


if [ $1 ]
then
if test -r $aadir/arena/$1
then
echo "Found server config $aadir/arena/$1..."
else
echo "Unable to find server config $aadir/arena/$1."
exit 0
fi
else
echo "Usage: launch-server <configfile>"
exit 0
fi

aapid="$1.pid"

cd $aadir

# is there a pid file?
if test -r $aapid
then
# there is a pid file -- is it current?
pid=`cat $aapid`
if `kill -CHLD $pid >/dev/null 2>&1`
then
echo "Alien Arena is currently running...."
exit 0
fi
echo ""
echo "Stale $aapid file, erasing..."
echo "Attempting to Restart Alien Arena"
rm -f $aapid
./$aabin &
ps aux | grep "$aabin" | grep -v grep | awk '{print $2}' > $aapid
else
echo "$aapid appears to be missing. Attempting to Restart Alien Arena"
./$aabin &
ps aux | grep "$aabin" | grep -v grep | awk '{print $2}' > $aapid
fi
