#!/bin/sh

prefix="/opt"
export EVENT_NOEPOLL=1
PATH=${prefix}/bin:${prefix}/sbin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=transmission-daemon
CONFIGDIR=/root/.config/transmission-daemon
DAEMON=${prefix}/bin/${NAME}

test -x $DAEMON || exit 0

if [ -z "$1" ] ; then
    case `echo "$0" | sed 's:^.*/\(.*\):\1:g'` in
        S??*) rc="start" ;;
        K??*) rc="stop" ;;
        *) rc="usage" ;;
    esac
else
    rc="$1"
fi

case "$rc" in
    start)
        echo "Starting Torrent client: $NAME"
        nice $DAEMON -g ${CONFIGDIR}
        ;;
    stop)
        #if [ -n "`pidof $NAME`" ]; then
            echo "Stopping Torrent client: $NAME"
            killall $NAME 2> /dev/null
        #fi
        ;;
    restart)
        "$0" stop
        sleep 1
        "$0" start
        ;;
    *)
        echo "Usage: $0 (start|stop|restart|usage)"
        ;;
esac

exit 0