#!/bin/sh
#
# PROVIDE: sickbeard
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# sickbeard_enable (bool):	Set to NO by default.
#			Set it to YES to enable it.
# sickbeard_user:	The user account SickRage daemon runs as what
#			you want it to be. It uses '_sabnzbd' user by
#			default. Do not sets it as empty or it will run
#			as root.
# sickbeard_group:	The group account SickRage daemon runs as what
#			you want it to be. It uses '_sabnzbd' group by
#			default. Do not sets it as empty or it will run
#			as wheel.
# sickbeard_dir:	Directory where SickRage lives.
#			Default: /usr/local/sickbeard
# sickbeard_datadir:	Data directory for Sick Beard (DB, Logs, config)
#			Default is same as sickbeard_dir

. /etc/rc.subr

name="sickbeard"
rcvar=${name}_enable

load_rc_config ${name}

: ${sickbeard_enable:="NO"}
: ${sickbeard_user:="_sabnzbd"}
: ${sickbeard_group:="_sabnzbd"}
: ${sickbeard_dir:="/usr/local/sickbeard"}
: ${sickbeard_datadir:="${sickbeard_dir}"}

pidfile="/var/run/sickbeard/sickbeard.pid"
command="/usr/local/bin/python2.7"
command_args="${sickbeard_dir}/SickBeard.py --datadir ${sickbeard_datadir} -d --pidfile ${pidfile} --quiet --nolaunch"

start_precmd="sickbeard_prestart"
sickbeard_prestart() {
	if [ -f ${pidfile} ]; then
		rm -f ${pidfile}
		echo "Removing stale pidfile."
	elif [ ! -d ${pidfile%/*} ]; then
		install -d -o ${sickbeard_user} -g ${sickbeard_group} ${pidfile%/*}
	fi

	if [ ! -d ${sickbeard_datadir} ]; then
		install -d -o ${sickbeard_user} -g ${sickbeard_group} ${sickbeard_datadir}
	fi
}

run_rc_command "$1"
