#!/bin/sh
# Start/stop the Kannel boxes: One bearer box and one WAP box.

# This is the default init.d script for Kannel.  Its configuration is
# appropriate for a small site running Kannel on one machine.

# Make sure that the Kannel binaries can be found in $BOXPATH or somewhere
# else along $PATH.  run_kannel_box has to be in $BOXPATH.

BOXPATH=/usr/sbin
PIDFILES=/var/run/kannel
CONF=/etc/kannel/kannel.conf

PATH=$BOXPATH:$PATH

# On Debian, the most likely reason for the bearerbox not being available
# is that the package is in the "removed" or "unconfigured" state, and the
# init.d script is still around because it's a conffile.  This is normal,
# so don't generate any output.
test -x $BOXPATH/bearerbox || exit 0

test -r /etc/default/kannel && . /etc/default/kannel

case "$1" in
  start)
    echo -n "Starting WAP gateway:"
    echo -n " bearerbox"
    start-stop-daemon --start --quiet \
	--pidfile $PIDFILES/kannel_bearerbox.pid \
	--chuid kannel \
	--exec $BOXPATH/bearerbox -- -v 4 --parachute --daemonize \
		--pid-file $PIDFILES/kannel_bearerbox.pid  $CONF 
    sleep 1 # Wait for bearerbox
    test ! -z $START_WAPBOX && (
    echo -n " wapbox"
	start-stop-daemon --start --quiet \
    	    --pidfile $PIDFILES/kannel_wapbox.pid \
	    --chuid kannel \
	    --exec $BOXPATH/wapbox -- -v 4 --parachute --daemonize \
		--pid-file $PIDFILES/kannel_wapbox.pid $CONF 
    )
    test ! -z $START_SMSBOX && (
    echo -n " smsbox"
	start-stop-daemon --start --quiet \
    	    --pidfile $PIDFILES/kannel_smsbox.pid \
	    --chuid kannel \
	    --exec $BOXPATH/smsbox -- -v 4 --parachute --daemonize \
		--pid-file $PIDFILES/kannel_smsbox.pid $CONF
    )
    echo "."
    ;;

  stop)
    echo -n "Stopping WAP gateway:"
    test ! -z $START_SMSBOX && (
	echo -n " smsbox"
	start-stop-daemon --stop --retry 5 --quiet \
    	    --pidfile $PIDFILES/kannel_smsbox.pid \
	    --name smsbox
    )
    test ! -z $START_WAPBOX && (
	echo -n " wapbox"
	start-stop-daemon --stop --retry 5 --quiet \
    	    --pidfile $PIDFILES/kannel_wapbox.pid \
	    --name wapbox
    )
    echo -n " bearerbox"
    start-stop-daemon --stop --retry 5 --quiet \
    	--pidfile $PIDFILES/kannel_bearerbox.pid \
	--name bearerbox
    echo "."
    ;;

  reload)
    # We don't have support for this yet.
    exit 1
    ;;

  restart|force-reload)
    $0 stop
    sleep 1
    $0 start
    ;;

  *)
    echo "Usage: $0 {start|stop|restart|force-reload}"
    exit 1

esac

exit 0
