#! /bin/bash

# ts2perlmod_startscript - TS2PerlMod startscript for Linux
# This script is a modified version of the TeamSpeak 2 server startscript written by pwk.linuxfan.
# Modified 2006 by Sven Paulsen

# ~~ BEGIN CONFIGURATION ~~~~~~~~~

CFG=default.ini
PID=ts2perlmod.pid

# ~~~END CONFIGURATION ~~~~~~~~~~~

case "$1" in
	start)
		if [ -e $PID ]; then
			echo "The file $PID already exists. Is the script already started?";
			echo "If you are sure the script is not running, delete the file $PID."
			exit 1
		else
			if [ -e ts2perlmod.pl ]; then
				if [ ! -x ts2perlmod.pl ]; then
					echo "The file ts2perlmod.pl is not executable. Trying to set it..."
					chmod u+x ts2perlmod.pl
				fi
				if [ -x ts2perlmod.pl ]; then
					perl ./ts2perlmod.pl -config=$CFG -pid=$PID -daemon
				else
					echo "The file ts2perlmod.pl is not executable. Fix this!"
					exit 4
				fi
			else
				echo "Could not find the file ts2perlmod.pl."
				exit 5
			fi
		fi
	;;
    stop)
		if [ -e $PID ]; then
			echo -n "Stopping the TS2PerlMod... "
			kill -KILL `cat $PID`
			rm $PID
			sleep 5
			echo "done"
		else
			echo "The file $PID is missing. Is the script started?"
			exit 7
		fi
	;;
	restart)
		$0 stop && $0 start || exit 1
	;;
	status)
		if [ -e $PID ]; then
			echo "The TS2PerlMod seems to be running."
			exit 0
		else
			echo "The TS2PerlMod seems to be stopped."
			exit 3
		fi
	;;
	*)
		echo "Usage: $0 {start | stop | restart | status}"
		exit 2
esac
exit 0
