#!/bin/sh
#########################################################################
#
# exoGameCtl - Half-Life Server Control Panel
# Copyright (c)2002-2003 JeffDodge.Com.  All Rights Reserved.
#
#########################################################################
#
#       Author: Jeffrey J. Dodge
#        Email: info@exodus.tk
#          Web: http://www.exodus.tk
#     Filename: remstart
#  Description: Remote Restart Easy Setup Script
#      Release: 2.0.3
#
#########################################################################
#
# Please direct bug reports, suggestions, or feedback to the exodus.tk
# programming forums.  http://forums.exodus.tk
#
#########################################################################
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# Please see the LICENSE file for the full End User License Agreement
#
#########################################################################

clear
if [ $# -lt 6 ]
then
   echo "exoGameCtl - Remote Restart Easy Setup"
   echo "Copyright (c)2002-2003 JeffDodge.Com.  All Rights Reserved."
   echo ""
   echo "usage: $0 <login> <ip> <port> <mod_name> <max_players> <start_map>"
   echo ""
   exit 1
fi

echo "exoGameCtl - Remote Restart Easy Setup"
echo "Copyright (c)2002-2003 JeffDodge.Com.  All Rights Reserved."
echo ""
echo "Installing Remote Restart..."

echo "!#/bin/sh" > /home/$1/start_hlds.sh
echo "# exoGameCtl - Half-Life Start Shell Script" >> /home/$1/start_hlds.sh
echo "# Copyright (c)2002-2003 JeffDodge.Com.  All Rights Reserved." >> /home/$1/start_hlds.sh
echo "" >> /home/$1/start_hlds.sh
echo "killall -9 hlds_run" >> /home/$1/start_hlds.sh
echo "killall -9 hlds" >> /home/$1/start_hlds.sh
echo "" >> /home/$1/start_hlds.sh
echo "export LD_LIBRARY_PATH=/home/$1/hlds_l:\$LD_LIBRARY_PATH" >> /home/$1/start_hlds.sh
echo "cd /home/$1/hlds_l" >> /home/$1/start_hlds.sh
echo "./hlds_run -game $4 +ip $2 +port $3 +maxplayers $5 +map $6 > /dev/null &" >> /home/$1/start_hlds.sh

echo "#!/bin/sh" > /home/$1/exo_test.sh
echo "# exoGameCtl - Half-Life Remote Start Shell Script" >> /home/$1/exo_test.sh
echo "# Copyright (c)2002-2003 JeffDodge.Com.  All Rights Reserved." >> /home/$1/exo_test.sh
echo "" >> /home/$1/exo_test.sh
echo "user_home=\"/home/$1\"" >> /home/$1/exo_test.sh
echo "" >> /home/$1/exo_test.sh
echo "hlds_kpid=\`ps x | grep \"sh ./hlds_run\" | awk '{print \$1}' | head -1\`" >> /home/$1/exo_test.sh
echo "" >> /home/$1/exo_test.sh
echo "if [ -f \$user_home/start.hlds ]" >> /home/$1/exo_test.sh
echo "then" >> /home/$1/exo_test.sh
echo "   rm -f \$user_home/start.hlds" >> /home/$1/exo_test.sh
echo "   \$user_home/start_hlds.sh" >> /home/$1/exo_test.sh
echo "fi" >> /home/$1/exo_test.sh
echo "" >> /home/$1/exo_test.sh
echo "if [ -f \$user_home/kill.hlds ]" >> /home/$1/exo_test.sh
echo "then" >> /home/$1/exo_test.sh
echo "   rm -f \$user_home/kill.hlds" >> /home/$1/exo_test.sh
echo "   kill -9 \$hlds_kpid" >> /home/$1/exo_test.sh
echo "   killall -9 hlds" >> /home/$1/exo_test.sh
echo "fi" >> /home/$1/exo_test.sh

touch /home/$1/start.hlds
echo "1-59 * * * * /home/$1/exo_test.sh > /dev/null 2>&1" > cronjob
crontab -u $1 cronjob
rm -f cronjob

chown -R $1:$1 /home/$1
chmod 755 /home/$1/start_hlds.sh
chmod 755 /home/$1/exo_test.sh

clear
echo "exoGameCtl - Remote Restart Easy Setup"
echo "Copyright (c)2002-2003 JeffDodge.Com.  All Rights Reserved."
echo ""
echo "Remote Restart Installation Complete!"
echo ""
