#!/bin/sh

# PROVIDE: %%PORTNAME%%
# REQUIRE: LOGIN
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable %%PORTNAME%%:
#
#%%PORTNAME%%_enable="YES"
#

. /etc/rc.subr

name=%%PORTNAME%%
start_precmd="%%PORTNAME%%_start_precmd"
stop_postcmd="%%PORTNAME%%_stop_postcmd"
rcvar=%%PORTNAME%%_enable

command=%%PREFIX%%/%%SLIMDIR%%/slimserver.pl
command_interpreter=%%PERL%%
pidfile=/var/run/${name}/${name}.pid
logdir=/var/log/${name}
statedir=%%SLIMDBDIR%%
cachedir=${statedir}/cache
prefsdir=${statedir}/prefs
playlistdir=${statedir}/playlists
u=%%SLIMUSER%%
g=%%SLIMGROUP%%
command_args="--daemon --pidfile=${pidfile} --user=${u} --group=${g}"
%%PORTNAME%%_user=${u}
%%PORTNAME%%_group=${g}

%%PORTNAME%%_start_precmd()
{
	# This is stuff is here and not in pkg-install because
	# /var/run may be destroyed at any time and we've had issues
	# with permissions on the various directories under /var getting
 	# screwed up in the past.

	mkdir -p /var/run/${name}
	chown -RH ${u}:${g} /var/run/${name}

	mkdir -p ${logdir}
	chown -RH ${u}:${g} ${logdir}

	mkdir -p ${statedir}
	mkdir -p ${cachedir}
	mkdir -p ${prefsdir}
	mkdir -p ${playlistdir}
	chown -RH ${u}:${g} ${statedir}
}

%%PORTNAME%%_stop_postcmd()
{
	pids=`pgrep -u $u`
	if [ -n "${pids}" ]; then
		sleep 1
		kill $pids > /dev/null 2>&1
	fi
	pids=`pgrep -u $u`
	if [ -n "${pids}" ]; then
		wait_for_pids $pids
	fi
}

load_rc_config ${name}

%%PORTNAME%%_enable=${%%PORTNAME%%_enable:-"NO"}
%%PORTNAME%%_flags=${%%PORTNAME%%_flags:-""}

run_rc_command "$1"
