#!/bin/sh
#
# start and stop the net tamagotchi server.

test -x /usr/sbin/tamad || exit 0

case "$1" in
	start)
echo -n "Starting net tamagotchi server: tamad"
		cd /var/lib/tama/ || exit 1
umask 077

		# Check if there is a process to match what's in the pid
		# file, by sending signal 0, which has no effect. This 
		# also checks to see if there is a pid file at all, BTW.
		if start-stop-daemon --quiet --stop --signal 0 \
			--pidfile /var/run/tama.pid --name tamad 2>/dev/null
		then
			echo " already running."
			exit
		fi

		su games -c '/sbin/start-stop-daemon --start --quiet --exec \
			/usr/sbin/tamad --pidfile /var/run/tama.pid' \
			>> /var/log/tama.log 2>&1 &

		echo $! > /var/run/tama.pid
		echo "."
		;;
	stop)
echo -n "Stopping net tamagotchi server: tamad"
		# Check if there is a process to match what's in the 
		# pid file, by sending signal 0, which has no effect.
		# This also checks to see if there is a pid file at 
		# all, BTW.
		if start-stop-daemon --quiet --stop --signal 0 \
			--pidfile /var/run/tama.pid --user games \
			--name tamad 2>/dev/null
		then
			start-stop-daemon --quiet --stop \
				--exec /usr/sbin/tamad \
				--pidfile /var/run/tama.pid \
				--user games \
				--name tamad
			echo "."
		else
			echo " not running."
		fi

		rm -f /var/run/tama.pid
		;;
	*)
		echo "Usage: /etc/init.d/tama {start|stop|restart|force-reload}"
		exit 1
esac

exit 0
