#! /bin/sh
### BEGIN INIT INFO
# Provides:          teamspeak3-Server
# Required-Start:    networking
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: TeamSpeak3 Server Daemon
# Description:       Starts/Stops/Restarts the TeamSpeak3 Server Daemon
### END INIT INFO
#
#
##############################################################
# Version History / Change Log:
##############################################################
#
# 2010-01-07	Axel Werner	CHANGE: updated script for
#					all new TS3 Server.
#				ADD:	TS3 Server seems to need
#					--background option for
#					start-stop-deamon
#
# 2010-01-21	Axel Werner	FIX: --background option is not allowed 
#					in STOP section. removed it.
#
# 2010-02-06	Axel Werner	FIX: Since server_linux-amd64-3.0.0-beta17
#					the Daemon requires the environment
#					variable $LD_LIBRARY_PATH to be set and
#					must include the teamspeak basedirectory
#
#
#
#
##############################################################

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="TeamSpeak3 Server Linux AMD64"
NAME=teamspeak
USER=teamspeak
DIR=/opt/teamspeak
DAEMON=$DIR/ts3server_linux_amd64
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

#fix 2010-02-06	Axel Werner
export LD_LIBRARY_PATH="$DIR:$LD_LIBRARY_PATH"

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

d_start() {
        start-stop-daemon --start --quiet \
                --background \
		--chuid $USER \
                --chdir $DIR \
                --exec $DAEMON \
                > /dev/null \
                || echo -n " already running"
}

d_stop() {
        start-stop-daemon --stop --quiet \
                --chuid $USER \
                --chdir $DIR \
                --exec $DAEMON \
                || echo -n " not running"
}

case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        d_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        d_stop
        echo "."
        ;;
  restart|force-reload)
        echo -n "Restarting $DESC: $NAME"
        d_stop
        sleep 15
        d_start
        echo "."
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac

exit 0