#!/bin/sh
#
#

# PROVIDE: tidepool
# REQUIRE: LOGIN
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# tidepool_enable (bool):		Set to NO by default.
#											Set it to YES to enable tidepool.
# tidepool_config (path):			Set to /usr/local/etc/seatidepool/tidepool.conf by default.
#											Path to configuration file.
# tidepool_user (str):			The user account tidepool daemon runs as
#											It uses 'tidepool' user by default.
# tidepool_group (str):			The group account tidepool daemon runs as
#											It uses 'tidepool' group by default.
# tidepool_logdir (str):			Default to "/var/log/tidepool"
#											Log file directory.
# tidepool_loglevel (str): 		Set to "5" by default.
#                                			Set it to priority to be used

. /etc/rc.subr

name=tidepool
rcvar=tidepool_enable

load_rc_config $name

: ${tidepool_enable:=NO}
: ${tidepool_config=/usr/local/etc/seatidepool/tidepool.conf}
: ${tidepool_logdir=/var/log/seatidepool}
: ${tidepool_sockdir=/var/run/seatidepool}
: ${tidepool_loglevel:="5"}
: ${tidepool_user="tidepool"}
: ${tidepool_group="tidepool"}
: ${tidepool_flags=""}

required_files=${tidepool_config}

command=/usr/local/bin/tidepoold
command_args="--daemonise --sockdir ${tidepool_sockdir} --config ${tidepool_config} --loglevel ${tidepool_loglevel} ${tidepool_flags}"
start_precmd="${name}_prestart"

pidfile=${tidepool_sockdir}/main.pid

tidepool_create_logdir()
{
	echo "Creating log directory"
	eval mkdir -p ${tidepool_logdir}
	[ $? -eq 0 ] && chown -R ${tidepool_user}:${tidepool_group} ${tidepool_logdir}
}

tidepool_prestart()
{
	if [ ! -d "${tidepool_logdir}/." ]; then
		tidepool_create_logdir || return 1
	fi

	if [ ! -d "${tidepool_sockdir}" ]; then
		install -d -m 0750 -o ${tidepool_user} -g ${tidepool_group} "${tidepool_sockdir}"
	fi
}

load_rc_config $name
run_rc_command "$1"
