#!/bin/sh

# PROVIDE: wapbox
# REQUIRE: LOGIN bearerbox
# KEYWORD: shutdown
#
# Add these lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# kannel_wapbox_enable (bool):	Set to NO by default.
#				Set it to YES to enable kannel_wapbox.
# kannel_wapbox_config (path):	Set to /usr/local/etc/kannel.conf
#				by default.

. /etc/rc.subr

name=kannel_wapbox
rcvar=${name}_enable
kannel_piddir="/var/run/kannel"

load_rc_config $name

: ${kannel_wapbox_enable:="NO"}
: ${kannel_wapbox_config="/usr/local/etc/kannel.conf"}
: ${kannel_wapbox_user:="kannel"}
: ${kannel_wapbox_group:="kannel"}

command="/usr/local/sbin/run_kannel_box"
start_precmd="${name}_prestart"
extra_commands="reload"
sig_reload=SIGUSR1

pidfile="${kannel_piddir}/${name}.pid"
bearerbox_pidfile="${kannel_piddir}/kannel_bearerbox.pid"

command_args="--pidfile ${pidfile} /usr/local/sbin/wapbox ${kannel_wapbox_flags} ${kannel_wapbox_config}"

kannel_wapbox_prestart()
{
	if [ ! -d "${kannel_piddir}" ]; then
		install -d -o $kannel_wapbox_user -g $kannel_wapbox_group $kannel_piddir
	fi
	kannel_wapbox_checkconfig
	kannel_bearerbox_checkpid
}

kannel_wapbox_checkconfig() {
	# Config file is required
	if [ ! -r ${kannel_wapbox_config} ]; then
		warn "${kannel_wapbox_config} is not readable."
		exit 1
	fi
}

kannel_bearerbox_checkpid() {
	# Check if kannel bearerbox is running
	if [ ! -e "${bearerbox_pidfile}" ]; then
		warn "In order to run ${name} you need to run first kannel_bearerbox."
		exit 1
	fi
}

run_rc_command "$1"
