#!/bin/sh
#
# BTPD daemon start script
#
# author: Richard Lapointe <richard*@laprjns.com>
# derived from a script developed by Thorsten Muehlfelder <thenktor@gmx.de>
# 

DAEMON=/usr/bin/btpd
BTPDUSER=btpd
DAEMON_OPTS=

test -x $DAEMON || exit 0

# include btpd defaults
if [ -f /etc/btpd.conf ] ; then
	. /etc/btpd.conf
fi

PID=""

btpd_start() {
	PID=`pidof -o %PPID /usr/bin/btpd`
	if [ -z "$PID" ]; then
		echo "Starting the Bittorrent Protocol Daemon..."
		su - $BTPDUSER -c "$DAEMON $DAEMON_OPTS"
		echo ""	
	else
		echo "The Bittorrent Protocol Daemon is already running with pid: $PID"
		exit 2
	fi
}

btpd_stop() {
	PID=`pidof -o %PPID /usr/sbin/btpd`
	if [ ! -z "$PID" ]; then
		echo "Stopping Starting the Bittorrent Protocol Daemon..."
		killall -q -TERM btpd
		sleep 1
		killall -9 btpd 1> /dev/null 2>&1
		rm -f /var/run/btpd.pid
		echo ""
	else
		echo "The Bittorrent Protocol Daemon is not running."
	fi
}

btpd_status() {
	PID=`pidof -o %PPID /usr/sbin/btpd`
	if [ ! -z "$PID" ]; then
		echo "The Bittorrent Protocol Daemon is running with pid: $PID"
		exit 0
	else
		echo "The Bittorrent Protocol Daemon is not running"
		exit 1
	fi
}


case "$1" in
'start')
	btpd_start
	;;

'stop')
	btpd_stop
	;;

'restart')
	btpd_stop
	sleep 1
	btpd_start
	;;

'status')
	btpd_status
	;;

*)
	echo "Usage: $0 start|stop|restart|status|"
esac
