#!/bin/sh
#		Startup script for the Alamin GSM SMS Gateway
#
#		Written by Andres Seco <AndresSH@alamin.org> using
#		the skeleton provided by Miquel van Smoorenburg and
#		Ian Murdock in Debian GNU/Linux 2.1 (slink)
#		modified with paragraphs from diald startup script
#		in Debian GNU/Linux 2.2 (potato)
#
# Version:	@(#)alamin  1.3  10-Oct-2004  AndresSH@alamin.org
#
# Security note: until connection timeout will be implemented inside
#               daemons, you can use redir to redirect connections to the
#               real port where the service is listening anc check for
#               timeouts. Redir is needed to use ports lower than 1024 to
#               avoid using suid on daemons. See commented redir lines
#               for example. You need to change your config file port
#               options.
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMONA=/usr/sbin/gsgcmd
DAEMONB=/usr/sbin/gsgmdd
DAEMONC=/usr/sbin/gsgsmppin
DAEMOND=/usr/sbin/gsgsmppout
DAEMONE=/usr/sbin/gsgdb2sms
NAMEA=gsgcmd
NAMEB=gsgmdd
NAMEC=gsgsmppin
NAMED=gsgsmppout
NAMEE=gsgdb2sms
PIDA=gsgcmd
PIDB=gsgmdd
PIDC=gsgsmppin
PIDD=gsgsmppout
PIDE=gsgdb2sms
DESC="Alamin GSM SMS Gateway"
RUNSMPP="no"
RUNDB2SMS="no"
CONF=/etc/alamin/gsgd.conf

test -f $DAEMONA || exit 0
test -f $DAEMONB || exit 0
#test -f $DAEMONC || exit 0
#test -f $DAEMOND || exit 0
#test -f $DAEMONE || exit 0
test -f $CONF || exit 0

# Look for smpp in config file
egrep '^[[:space:]]*gsmdevice[[:space:]]+smpp' $CONF > /dev/null
if [ $? -eq 0 ] ; then RUNSMPP="yes" ; fi
# Look for mysql in config file
egrep '^[[:space:]]*gsgdb2sms[[:space:]]+yes' $CONF > /dev/null
if [ $? -eq 0 ] ; then RUNDB2SMS="yes" ; fi

set -e

case "$1" in
  start)
	echo -n "Starting $DESC:"
	start-stop-daemon --start \
		--pidfile /var/run/alamin/$PIDA.pid \
		--chuid alamin:alamin --name $NAMEA \
		--startas $DAEMONA
	echo -n " $NAMEA"
	start-stop-daemon --start \
		--pidfile /var/run/alamin/$PIDB.pid \
		--chuid alamin:alamin --name $NAMEB \
		--startas $DAEMONB
	echo -n " $NAMEB"
	if [ "$RUNSMPP" == "yes" ] ; then
	  start-stop-daemon --start \
		--pidfile /var/run/alamin/$PIDC.pid \
		--chuid alamin:alamin --name $NAMEC \
		--startas $DAEMONC
          echo -n " $NAMEC"
	  start-stop-daemon --start \
		--pidfile /var/run/alamin/$PIDD.pid \
		--chuid alamin:alamin --name $NAMED \
		--startas $DAEMOND
          echo -n " $NAMED"
	fi
	if [ "$RUNDB2SMS" == "yes" ] ; then
	  start-stop-daemon --start \
		--pidfile /var/run/alamin/$PIDE.pid \
		--chuid alamin:alamin --name $NAMEE \
		--startas $DAEMONE
          echo -n " $NAMEE"
	fi
	echo "."
	;;
  stop)
	echo -n "Stopping $DESC:"
	PIDN=`cat /var/run/alamin/$PIDA.pid` || true
	start-stop-daemon --stop --oknodo \
		--pidfile /var/run/alamin/$PIDA.pid
        sleep 1
        if test -n $PIDN && kill -0 $PIDN 2>/dev/null
        then
		echo -n " $NAMEA..."
                cnt=0
                while kill -0 $PIDN 2>/dev/null
                do
                        cnt=`expr $cnt + 1`
                        if [ $cnt -gt 60 ]
                        then
                                echo -n " Failed, killing..."
				start-stop-daemon --stop --oknodo --sign KILL \
					--pidfile /var/run/alamin/$PIDA.pid
                                break
                        fi
                        sleep 2
                        echo -n "."
                done
                echo -n "done"
        else
                echo -n " $NAMEA"
        fi
	PIDN=`cat /var/run/alamin/$PIDB.pid` || true
	start-stop-daemon --stop --oknodo \
		--pidfile /var/run/alamin/$PIDB.pid
        sleep 1
        if test -n $PIDN && kill -0 $PIDN 2>/dev/null
        then
                echo -n " $NAMEB..."
                cnt=0
                while kill -0 $PIDN 2>/dev/null
                do
                        cnt=`expr $cnt + 1`
                        if [ $cnt -gt 60 ]
                        then
                                echo -n " Failed, killing..."
				start-stop-daemon --stop --oknodo --sign KILL \
					--pidfile /var/run/alamin/$PIDB.pid
                                break
                        fi
                        sleep 2
                        echo -n "."
                done
                echo -n "done"
        else
                echo -n " $NAMEB"
        fi
if [ "$RUNSMPP" == "yes" ] ; then
	PIDN=`cat /var/run/alamin/$PIDC.pid` || true
        start-stop-daemon --stop --oknodo \
		--pidfile /var/run/alamin/$PIDC.pid
        sleep 1
        if test -n $PIDN && kill -0 $PIDN 2>/dev/null
        then
                echo -n " $NAMEC..."
                cnt=0
                while kill -0 $PIDN 2>/dev/null
                do
                        cnt=`expr $cnt + 1`
                        if [ $cnt -gt 60 ]
                        then
                                echo -n " Failed, killing..."
        			start-stop-daemon --stop --oknodo --sign KILL \
					--pidfile /var/run/alamin/$PIDC.pid
                                break
                        fi
                        sleep 2
                        echo -n "."
                done
                echo -n "done"
        else
                echo -n " $NAMEC"
        fi
	PIDN=`cat /var/run/alamin/$PIDD.pid` || true
        start-stop-daemon --stop --oknodo \
		--pidfile /var/run/alamin/$PIDD.pid
        sleep 1
        if test -n $PIDN && kill -0 $PIDN 2>/dev/null
        then
                echo -n " $NAMED..."
                cnt=0
                while kill -0 $PIDN 2>/dev/null
                do
                        cnt=`expr $cnt + 1`
                        if [ $cnt -gt 60 ]
                        then
                                echo -n " Failed, killing..."
        			start-stop-daemon --stop --oknodo --sign KILL \
					--pidfile /var/run/alamin/$PIDD.pid
                                break
                        fi
                        sleep 2
                        echo -n "."
                done
                echo -n "done"
        else
                echo -n " $NAMED"
        fi
fi
if [ "$RUNDB2SMS" == "yes" ] ; then
	PIDN=`cat /var/run/alamin/$PIDE.pid` || true
        start-stop-daemon --stop --oknodo \
		--pidfile /var/run/alamin/$PIDE.pid
        sleep 1
        if test -n $PIDN && kill -0 $PIDN 2>/dev/null
        then
                echo -n " $NAMEE..."
                cnt=0
                while kill -0 $PIDN 2>/dev/null
                do
                        cnt=`expr $cnt + 1`
                        if [ $cnt -gt 60 ]
                        then
                                echo -n " Failed, killing..."
        			start-stop-daemon --stop --oknodo --sign KILL \
					--pidfile /var/run/alamin/$PIDE.pid
                                break
                        fi
                        sleep 2
                        echo -n "."
                done
                echo -n "done"
        else
                echo -n " $NAMEE"
        fi
fi
	echo "."
	;;
  reload|force-reload|restart)
	$0 stop
	sleep 2
	$0 start
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
