#!/bin/sh
#
# kannel:	This shell script takes care of starting and stopping
#		the Kannel WAP gateway
#
# Author:	Maciej Witkowiak <ytm@elysium.pl>
# chkconfig:	345 97 03
# description:	start and stop the Kannel WAP gateway used to fetch \
#		some WML content from a web server and compile it into \
#		WMLC mobile phone bytecode.
# processname:	bearerbox
# config:	/etc/sysconfig/kannel

# Sanity checks.
[ -x /usr/sbin/bearerbox ] || exit 0
[ -x /usr/sbin/wapbox ] || exit 0

# Source function library.
. /etc/rc.d/init.d/functions

# Source network configuration
. /etc/sysconfig/network

# Get service config
CONFIGFILE=/etc/kannel/kannel.conf
[ -f /etc/sysconfig/kannel ] && . /etc/sysconfig/kannel

# Check that networking is up
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status -a "$1" != init ]; then
		msg_network_down Kannel
		exit 1
	fi
else
	exit 0
fi

STARTER="/usr/sbin/run_kannel_box"

RETVAL=0
# See how we were called.
case "$1" in
  start)
	if [ ! -f /var/lock/subsys/kannel ]; then
		msg_starting "Kannel gateway (bearerbox)"
		daemon "$STARTER" /usr/sbin/bearerbox -- $CONFIGFILE
		RETVAL1=$?
		msg_starting "Kannel gateway (wapbox)"
		daemon "$STARTER" /usr/sbin/wapbox -- $CONFIGFILE
		RETVAL2=$?
		[ $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 ] && touch /var/lock/subsys/kannel ||\
		RETVAL=1
	else
		msg_already_running "Kannel gateway"
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/kannel ]; then
		msg_stopping "Kannel gateway"
		killproc "$STARTER"
		rm -f /var/lock/subsys/kannel >/dev/null 2>&1
	else
		msg_not_running "Kannel gateway"
	fi	
	;;
  status)
	status kannel
	exit $?
	;;
  restart|force-reload)
	$0 stop
	$0 start
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
