#!/bin/sh
#
# Start and stop the distributed.net client.

test -x /usr/bin/distributed-net || exit 0

case "$1" in
	start)
		echo -n "Starting distributed.net client: distributed-net"
		cd /var/lib/distributed-net || exit 1

		# 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/distributed-net.pid \
			--name distributed-net 2>/dev/null
		then
			echo " already running."
			exit
		fi

		# Figure out where start-stop-daemon is (it's moved between
		# bo and hamm, and it isn't in the path inside a su).
		if [ -x /sbin/start-stop-daemon ]; then
			ssd=/sbin/start-stop-daemon
		else
			ssd=/usr/sbin/start-stop-daemon
		fi

		su nobody -c "$ssd --start --quiet \
			--exec ./distributed-net \
			--pidfile /var/run/distributed-net.pid" \
			>> /var/log/distributed-net.log 2>&1 &

		echo $! > /var/run/distributed-net.pid
		echo "."
		;;
	stop)
		echo -n "Stopping distributed.net client: distributed-net"
		# 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/distributed-net.pid --user nobody \
			--name distributed-net 2>/dev/null
		then
			start-stop-daemon --quiet --stop \
				--exec /usr/bin/distributed-net \
				--pidfile /var/run/distributed-net.pid \
				--user nobody \
				--name distributed-net
			echo "."
		else
			echo " not running."
		fi

		rm -f /var/run/distributed-net.pid
		;;
	force-reload|restart)
		# -HUP has no effect on the client.
		# Check if the daemon is actually running.
		if start-stop-daemon --quiet --stop --signal 0 \
			--pidfile /var/run/distributed-net.pid \
			--name distributed-net \
			--user nobody 2>/dev/null
		then
			$0 stop
			$0 start
		fi
		;;
	*)
		echo "Usage: /etc/init.d/distributed-net {start|stop|restart|force-reload}"
		exit 1
esac

exit 0
