#!/bin/sh
### BEGIN INIT INFO
# Provides:          distributed-net
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Should-Start:      $network
# Should-Stop:       $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and Shutdown the distributed.net client
### END INIT INFO
#
# Notes:
#   - distributed.net likes to do startup and shutdown its own way and 
#     doesn't use pid files.  Running as user daemon makes it difficult
#     to generate pid files.  It appears that dnetc starts a seperate 
#     process to do the crunching, which makes any pid returned by the 
#     shell invalid.
#   - When instructed to shutdown dnetc takes a few seconds to clean up.
#     We should wait for it to finish, however since the stop is normally
#     executed as part of a system shutdown, we can't wait forever.  So 
#     we'll wait for up to 30 seconds.
#
# This script was originally written by: Noel@debian.org, 2001-12-16.
# James Stark <jstark@ieee.org>, 2006-10-22

DAEMON="/usr/bin/dnetc"
INIFILE="/etc/distributed-net.conf"
LOGFILE="/var/log/distributed-net.log"

dnetc_opts=""

if [ -r /etc/default/distributed-net ]; then
	. /etc/default/distributed-net
fi

OPTIONS="-quiet -ini $INIFILE -l $LOGFILE -inbase /var/lib/distributed-net/buff-in -outbase /var/lib/distributed-net/buff-out $dnetc_opts"

test -x $DAEMON || exit 0

export PATH="${PATH:+"$PATH:"}/usr/sbin:/sbin"

case "$1" in
	start)
		echo -n "Starting distributed.net client: distributed-net"
		cd /var/lib/distributed-net || exit 1
		
		# only start the deamon if it's not already running.  
		# start-stop-daemon doesn't support switching users, so we'll only
		# use it to check for a running process.
		if start-stop-daemon --quiet --stop --signal 0 --user daemon --name dnetc 2>/dev/null; then
			echo " already running."
		else
			su daemon -s /bin/sh -c "chrt -b 0 $DAEMON $OPTIONS"
			echo "."
		fi
		;;
	stop)
		echo -n "Stopping distributed.net client: distributed-net"
		cd /var/lib/distributed-net || exit 1

		# only stop the deamon if it's running.  
		# dnetc wants to use its own shutdown command, so start-stop-deamon
		# is  only used to ckeck for the running process.
		if start-stop-daemon --quiet --stop --signal 0 --user daemon --name dnetc 2>/dev/null; then
			su daemon -s /bin/sh -c "$DAEMON $OPTIONS -shutdown" > /dev/null 2>&1

			# wait for upto 10s for dnetc to stop.
			WAIT=0
			while start-stop-daemon --quiet --stop --signal 0 --user daemon --name dnetc 2>/dev/null && [ $WAIT -lt 30 ]; do
				sleep 1
				WAIT=`expr $WAIT + 1`
			done

			# If we timeout of the wait loop send any remaining dnetc 
			# processes SIGKILL to make sure that they terminate.
			if [ $WAIT -eq 30 ]; then
				start-stop-daemon --quiet --stop --signal 9 --user daemon --name dnetc
			fi

			echo "."
		else
			echo " not running."
		fi
		;;
	restart)
		$0 stop
		$0 start
		;;
	reload)
		# if dnetc is running have it HUP itself, otherwise do nothing.
		if start-stop-daemon --quiet --stop --signal 0 --user daemon --name dnetc 2>/dev/null; then
			su daemon -s /bin/sh -c "$DAEMON $OPTIONS -restart" 2> /dev/null
		fi
		;;
	force-reload)
		# if dnetc is running have it HUP itself, otherwise start it.
		if start-stop-daemon --quiet --stop --signal 0 --user daemon --name dnetc 2>/dev/null; then
			su daemon -s /bin/sh -c "$DAEMON $OPTIONS -restart" 2> /dev/null
		else
			$0 start
		fi
		;;
	fetch)
		echo -n "distributed.net: Fetching blocks"
		cd /var/lib/distributed-net || exit 1
		su daemon -s /bin/sh -c "$DAEMON $OPTIONS -fetch"
		;;
	flush)
		echo -n "distributed.net: Flushing blocks"
		cd /var/lib/distributed-net || exit 1
		su daemon -s /bin/sh -c "$DAEMON $OPTIONS -flush"
		;;
	update)
		echo -n "distributed.net: Updating blocks"
		cd /var/lib/distributed-net || exit 1
		su daemon -s /bin/sh -c "$DAEMON $OPTIONS -update"
		;;
	*)
		echo "Usage: /etc/init.d/distributed-net {start|stop|restart|force-reload|fetch|flush|update}"
		exit 1
esac

exit 0
