#!/bin/sh
#
# $FreeBSD: net/boinc-client/files/boinc-client.in 306088 2012-10-18 19:22:09Z pav $
#
# PROVIDE: boinc-client
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable the BOINC client:
#
# boinc_client_enable (boolean)		Set to "YES" to enable boinc_client
#					(default is "NO").
# boinc_client_flags (string)		Additional flags for boinc_client.

. /etc/rc.subr

name="boinc_client"
rcvar=boinc_client_enable

load_rc_config ${name}
: ${boinc_client_enable="NO"}

command="/usr/local/bin/boinc_client"
pidfile="/var/run/${name}.pid"

boinc_client_user=boinc
command_args="--daemon --dir \"/var/db/boinc\""

start_cmd=boinc_client_start
stop_postcmd=boinc_client_poststop

boinc_client_start()
{
	local pid

	pid=$(check_pidfile "${pidfile}" "${command}")
	if [ -n "${pid}" ]; then
		echo 1>&2 "${name} already running? (pid=${pid})."
		return 1
	fi
	echo "Starting ${name}."
	idprio 31 su - ${boinc_client_user} -c \
	    "\"${command}\" ${boinc_client_flags} ${command_args}" || return 1
	pgrep -U ${boinc_client_user} -n -t - '^boinc_client$' \
	    > "${pidfile}" || return 1
}

boinc_client_poststop()
{
	rm -f "${pidfile}"
}

run_rc_command "$1"
