#! /bin/sh
### BEGIN INIT INFO
# Provides:          stuntman-server
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: STUN server to help clients with their NAT 
# Description: STUN server to help clients identify and overcome limitations of their NAT 
### END INIT INFO
#
# Please read /usr/share/doc/stuntman-server/README.Debian

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/stunserver
NAME=stuntman-server
DESC=stuntman-server
START_DAEMON=false
PIDFILE=/var/run/${NAME}.pid

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

# Include stun defaults if available
if [ -f /etc/default/stuntman-server ] ; then
	. /etc/default/stuntman-server
fi
if [ "$START_DAEMON" != "true" ] ; then 
	exit 0
fi

ARGS=""
if [ -z "$MODE" ];then
	echo "No mode given. Using basic"
	MODE=basic
fi
[ -z "$MODE" ]||ARGS="$ARGS --mode $MODE"
[ -z "$PRIMARY_INTERFACE" ]||ARGS="$ARGS --primaryinterface $PRIMARY_INTERFACE"
[ -z "$SECONDARY_INTERFACE" ]||ARGS="$ARGS --altinterface $SECONDARY_INTERFACE"
[ -z "$PRIMARY_PORT" ]||ARGS="$ARGS --primaryport $PRIMARY_PORT"
[ -z "$SECONDARY_PORT" ]||ARGS="$ARGS --altport $SECONDARY_PORT"
if [ -z "$DAEMON_USER" ];then
	DAEMON_USER=nobody
fi

DAEMON_OPTS="$ARGS $DAEMON_OPTS "

set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start-stop-daemon --start --quiet --background --make-pidfile \
		--pidfile "$PIDFILE" \
		--chuid "$DAEMON_USER" --exec "$DAEMON" -- $DAEMON_OPTS
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --quiet --pidfile "$PIDFILE" \
		--oknodo --chuid "$DAEMON_USER" --exec "$DAEMON"
	echo "$NAME."
	;;
  #reload)
	#
	#	If the daemon can reload its config files on the fly
	#	for example by sending it SIGHUP, do it here.
	#
	#	If the daemon responds to changes in its config file
	#	directly anyway, make this a do-nothing entry.
	#
	# echo "Reloading $DESC configuration files."
	# start-stop-daemon --stop --signal 1 --quiet --pidfile \
	#	$PIDFILE --exec $DAEMON
  #;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: "
	$0 stop
	sleep 1
	$0 start
	echo "$NAME."
	;;
  status)
       echo -n "Status of $DESC: "

       if [ ! -r "$PIDFILE" ]; then
           echo "$NAME is not running."
           exit 3
       fi

       if read pid < "$PIDFILE" && ps -p "$pid" > /dev/null 2>&1; then
           echo "$NAME is running."
           exit 0
       else
           echo "$NAME is not running but $PIDFILE exists."
           exit 1
       fi
       ;;
  *)
	N="/etc/init.d/$NAME"
	# echo "Usage: $N {start|stop|restart|status|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|status|force-reload}" >&2
	exit 1
	;;
esac

exit 0
