#!/bin/bash
#
# rtorrent@user          Start up the rtorrent@user daemon
#
# chkconfig: 2345 55 25
# description: SSH is a protocol for secure remote shell access. \
#              This service starts up the OpenSSH server daemon.
#
# processname: screen rtorrent
# pidfile: /var/www/session/user/rtorrent.pid

### BEGIN INIT INFO
# Provides: rtorrent@user
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the rtorrent@user daemon
# Description:       
#                   
### END INIT INFO


USER="user"
NAME="rtorrent@user"
RTORRENT_BIN="/usr/local/rt096/bin/rtorrent"
SCREEN_OPTS="-dmS 'rtorrent@user' ${RTORRENT_BIN}"
SCREEN_BIN="/usr/bin/screen"
LOCKFILE="/var/lock/subsys/rtorrent@user"
PID_FILE="/var/www/session/user/rtorrent.pid"

# Source function library.
. /etc/init.d/functions

start() {
    echo "Starting ${NAME}"

    [ -f "${LOCKFILE}" ] && return 1

    touch "${LOCKFILE}"

    daemon --user "${USER}" --pidfile "${PID_FILE}" ${SCREEN_BIN} ${SCREEN_OPTS}
}

stop() {
    echo "Stopping ${NAME}"
    
    [ -f "${LOCKFILE}" ] || return 1

    kill $( cat ${PID_FILE} )
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
status)
    status "${NAME}"
    ;;
*)
    echo 'error'
;;
esac

exit 0

