#!/bin/sh
#
# Author: Michiel van Baak <michiel@vanbaak.eu>
#
# $FreeBSD: $
#
# PROVIDE: bazarr
# REQUIRE: LOGIN
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf to enable bazarr:
# bazarr_enable:   Set to NO by default. Set it to YES to enable it.
# bazarr_user:     The user account bazarr daemon runs as what
#                  you want it to be.
# bazarr_datadir:  Directory where bazarr user data lives.
#                  Default: /usr/local/bazarr

. /etc/rc.subr

name=bazarr
rcvar=bazarr_enable

load_rc_config ${name}

: ${bazarr_enable:=NO}
: ${bazarr_user:=%%USERS%%}
: ${bazarr_datadir:="%%PREFIX%%/bazarr"}

pidfile="${bazarr_datadir}/bazarr.pid"
procname="%%PYTHON_CMD%%"
command="/usr/sbin/daemon"
command_args="-f -p ${pidfile} ${procname} %%DATADIR%%/bin/bazarr.py --no-update -c ${bazarr_datadir}"
start_precmd=bazarr_precmd

bazarr_precmd()
{
	export PATH="/sbin:/bin:/usr/sbin:/usr/bin:%%LOCALBASE%%/sbin:%%LOCALBASE%%/bin"

	if [ ! -d ${bazarr_datadir} ]; then
		install -d -o ${bazarr_user} ${bazarr_datadir}
	fi
}

run_rc_command "$1"
