#! /bin/bash
# consult http://wiki.debian.org/LSBInitScripts about
# writing LSB init scripts

### BEGIN INIT INFO
# Provides:          rtorrent
# Required-Start:    $remote_fs $time $syslog networking
# Required-Stop:     $remote_fs $time $syslog networking
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     false
# Short-Description: ncurses bitTottent client
# Description:       rtorrent is a BitTorrent client for ncurses,
#                    using the libtorrent library. The client and
#                    library is written in C++ with emphasis on
#                    speed and efficiency, while delivering equivalent
#                    features to those found in GUI based clients
### END INIT INFO

# load some LSBInit functions
. /lib/lsb/init-functions
PATH=/sbin:/bin:/usr/sbin:/usr/bin

NAME="rtorrent"
DESC="ncurses bitTorrent client"

# user to start daemon as
USER="srv"
# where to place pidfile
# if stored in /var/run you'll need to create it
# first and give write permission to $USER
PIDFILE="/var/run/rtorrent.pid"

# name of executable
EXEC="/usr/bin/rtorrent"
# extra options passed to rtorrent
OPTIONS=""
# screen name (for access via "screen -r <name>")
SCREEN_NAME="rtor"

function check() {
	# do some self-checks before we start
	if ! sudo -u "$USER" test -x "$EXEC" ; then
   	log_failure_msg "user $USER has no permissions to execute $EXEC. check your chmod's"
	   exit 1
	fi
} # function check()

function start() {
	check
	log_daemon_msg "Starting $DESC" "$NAME"
	[ ! -s "$PIDFILE" ] && touch "$PIDFILE"
	[ `stat -c %U "$PIDFILE"` != "$USER" ] && chown "$USER":"$USER" "$PIDFILE"
	if ! status ; then
		stty stop undef && stty start undef
		sudo -u "$USER" screen -dmS "$SCREEN_NAME" start-stop-daemon -Svm -c "$USER" -x "$EXEC" -p "$PIDFILE" -- "$OPTIONS"
		exit_status="$?"
	else
		echo -ne "\n$NAME is already runnig"
		exit_status=255
	fi # if ! status...
	log_end_msg $exit_status
} # function start()

function stop() {
	log_daemon_msg "Stopping $DESC" "$NAME"
	if status ; then
		start-stop-daemon -Kp "$PIDFILE" -R5
		exit_status="$?"
	else
		echo -ne "\n$NAME is not running"
		exit_status=255
	fi # if status...
	log_end_msg $exit_status
} # function stop()

function shutdown() {
   log_daemon_msg "Stopping $DESC" "$NAME"
   if status ; then
      start-stop-daemon -Kp "$PIDFILE" -s INT
      exit_status="$?"
   else
      echo -ne "\n$NAME is not running"
      exit_status=255
   fi # if status...
   log_end_msg $exit_status
} # function shutdown()

function status() {
	if [ ! -f "$PIDFILE" ] ; then
		echo "no PID file $PIDFILE found";
		log_end_msg 255; exit 1
	fi
	PID=$(cat "$PIDFILE")
	if ps -p "$PID" &>/dev/null; then
		[ "$1" = "-v" ] && echo "$NAME is running with PID $PID" || return 0
	else
		[ "$1" = "-v" ] && echo "$NAME is not running" || return 1
	fi
} # function status()

function reload(){
   log_daemon_msg "Redrawing interface for $DESC" "$NAME"
   if status ; then
      start-stop-daemon -Kp "$PIDFILE" -s 28
      exit_status="$?"
   else
      echo -ne "\n$NAME is not running"
      exit_status=255
   fi # if status...
   log_end_msg $exit_status
} # function reload()

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
reload|force-reload)
	reload
	;;
restart)
	stop
	sleep 2
	start
	;;
status)
	status -v
	;;
shutdown)
	shutdown
	;;
*)
	echo "Usage: service $NAME {start|stop|restart|reload|force-reload|status|shutdown}" >&2
	exit 1
	;;
esac # case $1 in start,stop...
