#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/tkkassed
NAME=tkkasse-server
DESC=tkkasse-server
LOGFILE=/var/log/tkkassed
RUNDIR=/var/run/tkkassed
PIDFILE=$RUNDIR/tkkassed.pid
USER=tkkassed
GROUP=nogroup
NICELEVEL=0

test -x $DAEMON  || exit 0

# Include tkkasse-server defaults if available
if [ -f /etc/default/tkkasse-server ] ; then
	. /etc/default/tkkasse-server
fi

set -e

start_daemon()
{
	# generate+chown logfile
	[ -f $LOGFILE ] || touch $LOGFILE
	chown $USER:$GROUP $LOGFILE
	chmod 644 $LOGFILE
        # generate+chown rundir
        test -d $RUNDIR  || mkdir -p $RUNDIR 
        chown $USER:$GROUP $RUNDIR
	chmod 755 $RUNDIR
	
	umask 022
	start-stop-daemon --start --pidfile $PIDFILE \
	    --chuid $USER:$GROUP --make-pidfile \
	    --nicelevel $NICELEVEL --background \
	    --exec $DAEMON -- $LOGFILE $NAME $DAEMON_OPTS
}

stop_daemon()
{
	start-stop-daemon --stop --quiet --pidfile $PIDFILE \
		--user $USER --group $GROUP
}

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start_daemon
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	stop_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 \
	#	/var/run/$NAME.pid --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: "
	stop_daemon
	sleep 1
	start_daemon
	echo "$NAME."
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
