#!/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: hldsinst
#  Description: Half-Life Easy Installation 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
#
#########################################################################

# addcmd - this is the command used to add users to your system.
# YOU MUST UNCOMMENT ONE OF THE "addcmd" VARIABLES PRIOR TO USING THIS
# SCRIPT!

# linux version
#addcmd="adduser $1 -s /sbin/nologin"

# FreeBSD version
#addcmd="pw useradd $1 -s /bin/nologin -m"

# tarcmd - this is the command to untar the hlds comppressed file
tarcmd="tar xvfz hlds.tar.gz -C /home/$1"

#########################################################################
# !! DO NOT EDIT ANYTHING BELOW THIS LINE OR THE SCRIPT MAY NOT WORK !! #
#########################################################################

clear
if [ $# -lt 6 ]
then
   echo "exoGameCtl - Easy Half-Life Installer"
   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 - Easy Half-Life Installer"
echo "Copyright (c)2002-2003 JeffDodge.Com.  All Rights Reserved."
echo ""
echo "Installing..."

$addcmd
$tarcmd

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/hlds_l
chmod 755 /home/$1/start_hlds.sh
chmod 755 /home/$1/exo_test.sh

clear
echo "exoGameCtl - Easy Half-Life Installer"
echo "Copyright (c)2002-2003 JeffDodge.Com.  All Rights Reserved."
echo ""
echo "Installation Complete!"
echo ""
echo "Please set the users password below so that they may access"
echo "their FTP account to upload new maps and such."
echo ""
passwd $1
