#!/bin/sh
#
# PROVIDE: couchpotato
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /usr/local/etc/rc.d/couchpotato
# to enable this service:
#
# couchpotato_enable (bool):       Set to NO by default.
#
#                       Set it to YES to enable it.
# couchpotato_user:        The user account couchpotato daemon runs as what
#                       you want it to be. It uses 'media' user by
#                       default. Do not sets it as empty or it will run
#                       as root.
# couchpotato_group:       The group account couchpotato daemon runs as what
#                       you want it to be. It uses 'media' group by
#                       default. Do not sets it as empty or it will run
#                       as wheel.
# couchpotato_data_dir:    Directory where couchpotato configuration
#                       data is stored.
#                       Default: /var/db/couchpotato

. /etc/rc.subr
name="couchpotato"
rcvar="${name}_enable"
load_rc_config ${name}

: ${couchpotato_enable:="NO"}
: ${couchpotato_user:="nobody"}
: ${couchpotato_group:="nobody"}
: ${python2:="/usr/local/bin/python2.7"}
: ${couchpotato:="/usr/local/share/couchpotato/CouchPotatoServer/CouchPotato.py"}
: ${couchpotato_data_dir:="/var/db/couchpotato"}
: ${pidfile:="/var/run/couchpotato/couchpotato.pid"}


command="${python2}"
command_args="${couchpotato} --data_dir ${couchpotato_data_dir} --pid_file ${pidfile} --daemon --quiet"

start_precmd="couchpotato_prestart"
couchpotato_prestart() {
        if [ ! -d ${pidfile%/*} ]; then
                install -d -o ${couchpotato_user} -g ${couchpotato_group} ${pidfile%/*}
        fi

        if [ ! -d ${couchpotato_data_dir} ]; then
                install -d -o ${couchpotato_user} -g ${couchpotato_group} ${couchpotato_data_dir}
        fi
}

run_rc_command "$1"
