#!/bin/bash
#
# chkconfig: - 16 84
# description: Start up tracker for BitTorrennt 
#
# processname: bttrack
# config: /etc/sysconfig/bttrack

# source function library
. /etc/rc.d/init.d/functions

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

# defaults
PORT=6969
STATEFILE=/srv/bittorrent/state/bttrack
OPTIONS=
LOGFILE=/var/log/bittorrent/bttrack.log
# directory of torrents that the tracker is allowed to serve
DIR="/srv/bittorrent/data"

# source the config
. /etc/sysconfig/bttrack

RETVAL=0
prog="/usr/bin/bttrack"
btuser="torrent"

case "$1" in
  start)
	echo -n $"Starting BitTorrent tracker: "
	runuser -s /bin/sh -c "$prog --port $PORT --dfile $STATEFILE --logfile $LOGFILE \
		$OPTIONS --allowed_dir $DIR" $btuser &> /dev/null &
	disown -ar
	usleep 500000
	status bttrack &> /dev/null && echo_success || echo_failure
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/bttrack
	echo
	;;
  stop)
	echo -n $"Shutting down BitTorrent tracker: "
	killproc $prog
	#killproc "/usr/bin/python $prog"
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/bttrack
	echo
	;;
  restart|reload)
        $0 stop
        $0 start
	RETVAL=$?
        ;;
  condrestart)
        if [ -f /var/lock/subsys/bttrack ]; then
                $0 stop
		$0 start
        fi
	RETVAL=$?
        ;;
  status)
        status bttrack
	RETVAL=$?
        ;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
	exit 1
esac

exit $RETVAL
