#!/bin/sh
### BEGIN INIT INFO
# Provides:          transmission-daemon
# Required-Start:    networking
# Required-Stop:     networking
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: Start the transmission BitTorrent daemon client.
### END INIT INFO

DIRECTORY="/media/hdd/transmission"
DOWNLOAD_DIR="${DIRECTORY}/download"
CONFIG_DIR="${DIRECTORY}/config"
WATCH_DIR="${DIRECTORY}/watch"
RPC_PORT=9091
USERNAME="root"
PASSWORD="enigma2"
ALLOWED="*.*.*.*,*:*:*:*:*:*:*:*"
[ -f /etc/default/transmission-daemon ] && . /etc/default/transmission-daemon
NAME="transmission-daemon"
DAEMON="/bin/nice -n 9 /usr/bin/transmission-daemon"
ARGS="-c $WATCH_DIR -g $CONFIG_DIR -r :: -a $ALLOWED -w $DOWNLOAD_DIR -p $RPC_PORT -t -u $USERNAME -v $PASSWORD"

[ -z "$DAEMON_USER" ] || SSDOPTIONS="--chuid $DAEMON_USER:$DAEMON_GROUP"

PATH=/usr/sbin:/usr/bin:/sbin:/bin

if [ ! -d $DOWNLOAD_DIR ] ; then mkdir -p $DOWNLOAD_DIR; fi
if [ ! -d $CONFIG_DIR ] ; then mkdir -p $CONFIG_DIR; fi
if [ ! -d $WATCH_DIR ] ; then mkdir -p $WATCH_DIR; fi


case $1 in
	start)
		echo  "Starting $NAME"
		start-stop-daemon $SSDOPTIONS -S -b -n $NAME -a $DAEMON -- $ARGS
		;;
	stop)
		echo  "Stopping $NAME"
		start-stop-daemon --stop --quiet --exec /usr/bin/transmission-daemon
		;;
	restart)
		echo -n "Stopping $NAME"
		start-stop-daemon --stop --quiet --exec /usr/bin/transmission-daemon
		for i in 1 2 3 ; do
			sleep 1
			echo -n "."
		done
		echo
		echo  "Restarting $NAME"
		start-stop-daemon $SSDOPTIONS -S -b -n $NAME -a $DAEMON -- $ARGS
		;;
        autostart | enable)
		echo
        	update-rc.d transmission-daemon defaults 60
		sleep 2
		echo
		;;
        noautostart | disable)
		echo
        	update-rc.d -f transmission-daemon remove
		sleep 2
		echo
		;;

	*)
		echo "Usage: $0 {start|restart|stop|enable|disable}"
		exit 1
esac

exit 0
