#! /bin/bash

# Source Dedicated Server (SRCDS) Linux Startscript 
# Version: 1.2
 
# Author:
# Andreas Glaser aka JaZz
# www.andreas-glaser.com

# Function:
# Start your source dedicated with a set of parameters

# Requirements:
# srcds :: http://www.srcds.com
# screen :: http://www.google.com/linux?q=screen
# awk :: http://www.google.com/linux?q=awk
# nice :: http://www.google.com/linux?q=nice
# whoami :: http://www.google.com/linux?q=whoami
# grep :: http://www.google.com/linux?q=grep

############################*
# Configuration (Required):
#

# |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
# |~~ 1. SYSTEM PARAMETERS ~~~~~~~~~~|
# |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|

# ~~ Description: Set the user who owns the CounterStrike Server (root is not recommended)
# ~~ Params: Existing user on your system
SYS_USER="css-001";

# ~~ Description: Path to the root directory of your server
# ~~ Params: System path
SYS_DIR="/path/to/your/server/";

# ~~ Description: Server daemon
# ~~ Params: srcds_run - srcds_amd - srcds_i486 - srcds_i686
# ~~ Default Value: srcds_run
SYS_DAEMON="srcds_run";

# ~~ Descripton: Set the priority (niceness) of the server. Highest priority: -20 Lowest priority: 19
# ~~ Default Value: 0
SYS_NICE="0";

# ~~ Name: Name of your server
SYS_NAME="CSS-001";

# ~~ Description: Description of the server
SYS_DESC="My first SRCDS Server";

# |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
# |~~ 2. SERVER START PARAMETER ~~~~~|
# |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|

# ~~ Description: Define which kind of srcds game should be started
# ~~ Params: Counter-Strike Source - ageofchicalry - diprip - dods - dystopia - esmod - garrysmod - hl2mp - insurgency
# ~~         l4d_full - left4dead - synergy - tf - zps - cstrike - cstrike_beta - czero - dmc - dod - gearbox - ricochet
# ~~         tfc - valve - darkmessiah - diprip - marenosturm - redorchestra - ship - sin - tshb
# ~~ Default Value: Counter-Strike Source
SYS_GAME="Counter-Strike Source";

# ~~ Description: IPv4 Address of your server
# ~~ Params: XXX.XXX.XXX.XXX
START_PARAMS_IP="127.0.0.1";

# ~~ Description: Port of your server
# ~~ Params: Between 0 and 65535
# ~~ Default Value: 27015
START_PARAMS_PORT="27015";

# ~~ Description: Defines the default map
# ~~ Params: Requires an installed map on your server
START_PARAMS_MAP="de_dust2";

# ~~ Description: Defines the amount of player slots
# ~~ Params: Between 1 and 32
START_PARAMS_MAXPLAYERS="32";

# ~~ Description: Defines whether VAC (Valve Anti Cheat) is activated
# ~~ Params: secure / insecure
# ~~ Default Value: secure
START_PARAMS_SECURE="secure";

# ~~ Description: Pingboost
# ~~ Params: 0 / 1 / 2 / 3
# ~~ Default Value: 0
START_PARAMS_PINGBOOST="0";

# ~~ Description: Defines the tickrate
# ~~ Params: 33 / 66 / 100
# ~~ Default Value: 33
START_PARAMS_TICKRATE="33";

#
# CONFIGURATION END
############################*



# ~~~ DON'T EDIT ANYTHING BELOW THIS LINE - ACCEPT YOU KNOW WHAT YOU ARE DOING ~~~


############################*
# STARTSCRIPT CORE START
# 

echo "| INFORMATION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|";
echo "| Script:         Source Dedicated Server Startscript (Linux)     |";
echo "| Version:        1.2                                             |";
echo "| Author:         Andreas Glaser                                  |";
echo "| Website:        www.andreas-glaser.com                          |";   
echo "| Creation Date:  2008-09-05                                      |";
echo "| Last Update:    2009-02-17                                      |";               
echo "|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|";

# ~~ Assemble start parameters
PARAMS="-game $SYS_GAME -pingboost $START_PARAMS_PINGBOOST +map $START_PARAMS_MAP +maxplayers $START_PARAMS_MAXPLAYERS -$START_PARAMS_SECURE -tickrate $START_PARAMS_TICKRATE -autoupdate +ip $START_PARAMS_IP -port $START_PARAMS_PORT";

# ~~ Check awk package
if [ ! -x /usr/bin/awk ]; then 
echo "~~> Error    >> Startscript cannot be used";
echo "~~> Reason   >> AWK seems to be not installed";
echo "~~> Solution >> Install the Linux package 'screen'";
exit 1;
fi;

# ~~ Check screen package
if [ ! -x /usr/bin/screen ]; then 
echo "~~> Error    >> Startscript cannot be used";
echo "~~> Reason   >> Screen seems to be not installed";
echo "~~> Solution >> Install the Linux package 'screen'";
exit 1;
fi;

# ~~ Check nice package
if [ ! -x /usr/bin/nice ]; then
echo "~~> Error    >> Startscript cannot be used";
echo "~~> Reason   >> Nice seems to be not installed";
echo "~~> Solution >> Install the Linux package 'nice'";
exit 1;
fi;

# ~~ Get mode (Root/Nonroot)
if [ `whoami` = root ]; then 
  usagetype=root; 
else 
  usagetype=nonroot; 
fi;

if [ "$useagetype" != "root" -a $SYS_NICE -lt 0 ]; then
  SYS_NICE="0";
fi;

if [ $SYS_NICE -lt -20 ]; then 
  SYS_NICE="-20";
fi;

if [ $SYS_NICE -gt 19 ]; then 
  SYS_NICE="19";
fi;

# ~~ Start function
start() 
{
if [ ! -d $SYS_DIR ]; then 
  echo "~~> Error    >> Server could not be started";
  echo "~~> Reason   >> $SYS_DIR does not exists";
  echo "~~> Solution >> Please check the startscript configuration";
  exit 1; 
fi;

if [ ! -x $SYS_DIR/$SYS_DAEMON ]; then 
  echo "~~> Error    >> Deamon could not be executed";
  echo "~~> Reason   >> Either the deamon is not executable or the startscript configuration is incorrect";
  echo "~~> Solution >> 1. chmod u + x $SYS_DIR$SYS_DAEMON 2. Check the startscript configuration";
  exit 1;
fi;

if status; then 
  echo "~~> Error    >> Server is already started";
  echo "~~> Reason   >> Already started";
  echo "~~> Solution >> Stop the server";
  exit 1;
fi;

case "$usagetype" in
root)
  su - $SYS_USER -c "cd $SYS_DIR ; screen -AmdS $SYS_NAME nice -$SYS_NICE ./$SYS_DAEMON $PARAMS";
;;
nonroot)
  cd $SYS_DIR; 
  screen -AmdS $SYS_NAME nice -$SYS_NICE ./$SYS_DAEMON $PARAMS;
;;
esac
}

# ~~ Stop function
stop () 
{
if ! status; then
  echo "~~> Error    >> Server could not be found";
  echo "~~> Reason   >> The daemon is not started";
  echo "~~> Solution >> Start the server";
  exit 1;
fi;

case "$usagetype" in
root)
  tmp=$(su - $SYS_USER -c "screen -ls" | awk -F . "\$2 ~ /^$SYS_NAME[[:space:]].*/ {print \$1}" | awk '{print $1}');
  su - $SYS_USER -c "screen -r $tmp -X quit"M
;;
nonroot)
  screen -r $(screen -ls | awk -F . "\$2 ~ /^$SYS_NAME[[:space:]].*/ {print \$1}" | awk '{print $1}') -X quit;
;;
esac;
}

# ~~ Status function
status () 
{
case "$usagetype" in
root)
  su - $SYS_USER -c "screen -ls" | grep [.]$SYS_NAME[[:space:]] > /dev/null
;;
nonroot)
  screen -ls | grep [.]$SYS_NAME[[:space:]] > /dev/null
;;
esac
}

# ~~ Watch function
watch () {
if ! status; then 
  echo "$SYS_DESC $SYS_NAME could not be found. Probably not running."; 
  exit 1; 
fi

case "$usagetype" in
root)
  tmp=$(su - $SYS_USER -c "screen -ls" | awk -F . "\$2 ~ /^$SYS_NAME[[:space:]].*/ {print \$1}" | awk '{print $1}')
  su - $SYS_USER -c "screen -r $tmp"
;;
nonroot)
  screen -r $(screen -ls | awk -F . "\$2 ~ /^$SYS_NAME[[:space:]].*/ {print \$1}" | awk '{print $1}')
;;
esac
}

case "$1" in
start)
  echo "~~> Action   >> Starting $SYS_NAME ($SYS_DESC)";
  start
  echo "~~> Success  >> Server is started";
;;

stop)
  echo "~~> Action   >> Stopping $SYS_NAME ($SYS_DESC)";
  stop
  echo "~~> Success  >> Server is stopped";
;;

restart)
  echo "~~> Action   >> Restrating $SYS_NAME ($SYS_DESC)";
  status && stop
  start
  echo "~~> Success  >> Server is restarted";
;;

status)
if status; then 
  echo "~~> Info   >> The server is online : $SYS_NAME ($SYS_DESC)";
  else 
  echo "~~> Info   >> The server is offline : $SYS_NAME ($SYS_DESC)";
fi;
;;
watch)
watch
;;

*)
echo "~~> Usage   >> $0 {start|stop|status|restart|watch}";
echo "            >> While watching press ctrl-a, ctrl-d to stop watching without stopping the server.";
exit 1;
;;

esac
#
# STARTSCRIPT CORE END
############################*

exit 0
