#!/bin/sh
#
# kannel	This script takes care of starting and stopping the kannel \
#		WAP gateway services (bearer/wap/smsbox).
# chkconfig: - 97 03
# description:  The Kannel WAP and SMS gateway services
# config: /etc/kannel.conf

### BEGIN INIT INFO
# Provides: kannel
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: start and stop Kannel WAP gateway services
# Description: Kannel is a set of daemons implementing WAP gateway services.
### END INIT INFO

# Use start-stop-kannel
prog="/usr/sbin/start-stop-daemon"
args="--start --background --chuid kannel:kannel --exec "
#args="--start --chuid kannel:kannel --exec "
config="/etc/kannel.conf"

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

[ -f $config ] || exit 1

RETVAL=0
RETVAL_BEARER=0
RETVAL_WAP=0
RETVAL_SMS=0

BEARERBOX_PIDFILE="/run/kannel/bearerbox.pid"
SMSBOX_PIDFILE="/run/kannel/smsbox.pid"

_start_args() {
	pidfile=$1
	echo "--start --background --chuid kannel:kannel --pidfile $pidfile --make-pidfile --exec"
}

start() {
	# Create runtime directory for pid files
	mkdir -p /run/kannel && chown kannel:kannel /run/kannel
	rm $BEARERBOX_PIDFILE || true
	args="$(_start_args $BEARERBOX_PIDFILE)"

        # Start daemons.
        echo -n "Starting kannel bearer box: "
	daemon $prog $args /usr/sbin/bearerbox $config &>/dev/null
	RETVAL_BEARER=$?

	# It seems like the bearerbox may need to settle before accepting
	# connections from wapbox and smsbox
	#
	sleep 2

	# Bearerbox start may fail, however start-stop-daemon with --background 
	# option will always report success. Without --background option, start-stop-daemon
	# would correctly report failures but hang in foreground and service startup will never end.
	#
	# To work around this, we instructed start-stop-daemon to save a pidfile of started process
	# and manually check /proc/ if it actually exists.
	for i in $(seq 1 20); do
		RETVAL_BEARER=1
		sleep 0.2
		if ! [ -s $BEARERBOX_PIDFILE ]; then
			continue
		fi
		PID=$(cat "$BEARERBOX_PIDFILE" 2>/dev/null)
		if [ -d "/proc/${PID}" ]; then
			RETVAL_BEARER=0
			success; echo
			break
		else
			failure; echo
			RETVAL=1
			return
			#break
		fi
	done

	# Starting wap and sms only makes sense if bearerbox is running
	if [ $RETVAL_BEARER -eq 0 ]; then
	  if grep "^group = wapbox" $config &>/dev/null; then
	    echo -n "Starting kannel wap box: "
	    daemon $prog $args /usr/sbin/wapbox $config
	    RETVAL_WAP=$?
	    echo
	  fi
	  if grep "^group = smsbox" $config &>/dev/null; then
	    echo -n "Starting kannel sms box: "

	    args=$(_start_args $SMSBOX_PIDFILE)
	    daemon $prog $args /usr/sbin/smsbox $config &>/dev/null
	    RETVAL_SMS=$?

	    for i in $(seq 1 20); do
	    	RETVAL_SMS=1
	    	sleep 0.2
	    	if ! [ -s $SMSBOX_PIDFILE ]; then
	    		continue
	    	fi
	    	PID=$(cat "$SMSBOX_PIDFILE" 2>/dev/null)
	    	if [ -d "/proc/${PID}" ]; then
	    		RETVAL_SMS=0
	    		success; echo
	    		break
	    	else
	    		failure; echo
	    		RETVAL_SMS=1
	    		RETVAL=1
	    		return
	    		#break
	    	fi
	    done
	  fi
	fi
 	[ $RETVAL_BEARER -eq 0 -a $RETVAL_WAP -eq 0 -a $RETVAL_SMS -eq 0 ] \
	  && touch /var/lock/subsys/kannel || RETVAL=1
}

stop() {
        # Stop daemons.
	if grep "^group = smsbox" $config &>/dev/null; then
	  echo -n "Shutting down kannel sms box: "
	  killproc /usr/sbin/smsbox
	  RETVAL_SMS=$?
	  echo
	fi
	if grep "^group = wapbox" $config &>/dev/null; then
	  echo -n "Shutting down kannel wap box: "
	  killproc /usr/sbin/wapbox
	  RETVAL_WAP=$?
	  echo
	fi
	echo -n "Shutting down kannel bearer box: "
        killproc /usr/sbin/bearerbox
	RETVAL_BEARER=$?
	echo
	[ $RETVAL_BEARER -eq 0 -a $RETVAL_WAP -eq 0 -a $RETVAL_SMS -eq 0 ] \
	  || RETVAL=1
	rm -f /var/lock/subsys/kannel
}

# See how we were called.
case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  status)
	status /usr/sbin/bearerbox
	RETVAL_BEARER=$?
	if grep "^group = wapbox" $config &>/dev/null; then
	  status /usr/sbin/wapbox
	  RETVAL_WAP=$?
	fi
	if grep "^group = smsbox" $config &>/dev/null; then
	  status /usr/sbin/smsbox
	  RETVAL_SMS=$?
	fi
	[ $RETVAL_BEARER -eq 0 -a $RETVAL_WAP -eq 0 -a $RETVAL_SMS -eq 0 ] \
	  || RETVAL=1
	;;
  restart)
	stop
	start
	;;  
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=1
esac

exit $RETVAL

