#!/bin/bash
#
# Starting rtorrent in a screen session as daemon
#
# chkconfig: 2345 50 01
#
# description: start rtorrent detached
# processname: rtorrent

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

# Source networking configuration.
[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network

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


if [ -f /etc/sysconfig/rtorrent ]; then
        . /etc/sysconfig/rtorrent
fi

if [ -z $RTORRENT_USER ]; then
        exit 0
fi

RETVAL=0

start() {
        echo -n "Starting rtorrent: "
        daemon --user $RTORRENT_USER /usr/bin/screen -dmS rtorrent /usr/bin/rtorrent $OPTIONS
        RETVAL=$?
        echo
}

stop() {
        echo -n "Stopping rtorrent: "
        killproc rtorrent -INT
        RETVAL=$?
        echo
}

restart() {
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  status)
        status rtorrent
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
esac

exit $RETVAL
