#!/bin/sh
#
# kunde: START_TICKETGEN=yes: Ticket-Generator via TCP

# chkconfig: 2345 90 10
# description: Starte diverse Datenbankinterfacesachen
# config: /etc/pop.conf

# Source settings
. ${POPHOME:-@POPHOME@}/lib/pop_conf_sh
[ -n "$GROUP"     ] || exit 0
[ -n "$OWNER"     ] || exit 0
[ -n "$POPHOME"   ] || exit 0
[ -n "$RUNDIR"    ] || exit 0
[ -n "$TCPSERVER" ] || exit 0

beschreibung=Ticket-Generator
dienst=ticketgen
pidfile="$RUNDIR/$dienst.pid"

echo_success() {
	echo "    OK."
}
echo_failure() {
	echo "    FAILED"
}
status() {
	echo -n "$1 ... "
	if /sbin/start-stop-daemon --stop --pidfile "$pidfile" -q --signal 0 ; then
		echo running
	else
		echo NOT running
	fi
}

if test "$(whoami)" != "$OWNER" ; then
	if test -n "$_LOOP"; then
		echo -n "setuid to $OWNER"
		echo_failure 
		exit 1
	fi
	mkdir -m 775 -p /var/lock/kunde "$RUNDIR"
	chown $OWNER:$GROUP /var/lock/kunde "$RUNDIR"
	exec su "$OWNER" -c "env _LOOP=1 $0 $*"
fi

case "$1" in
	start)
		if [ "$START_TICKETGEN" = "yes" ] ; then
			echo -n "Start: $beschreibung"
			if test -s "$pidfile" && kill -0 $(cat "$pidfile") 2>/dev/null; then
				echo "...running"
			else
				$TCPSERVER -u$OWNER -g$GROUP -q -c20 -H -R -b10 0 "$dienst" "$POPHOME/bin/$dienst" &
				echo $! >"$pidfile"
				sleep 1
				kill -0 $(cat "$pidfile") 2>/dev/null && echo_success || echo_failure 
				echo ""
			fi
		fi
		;;
	stop)
		if [ -n "$START_TICKETGEN" -a "$START_TICKETGEN" != no ] ; then
			echo -n "Stop: $beschreibung ";
			test -s "$pidfile" && kill $(cat "$pidfile") && echo_success || echo_failure
			rm -f "$pidfile"
			echo ""
		fi
		;;
	status)
		status "$dienst"
		;;
	reload)
		$0 stop;
		$0 start;
		;;
	restart)
		$0 stop;
		$0 start;
		;;
	*)
		echo "Usage: $0 {start|stop}"
		exit 1
		;;
esac

exit 0
