#! /bin/sh
# This script can be used to start/stop smsd 
# as a daemon in Linux and Solaris.

# Maximum time to stop smsd, after that it gets killed hardly
maxwait=30

case "$1" in
    start)
	find /var/spool/sms -name '*.LOCK' -exec rm {} \;
        /usr/local/bin/smsd &
	;;
    stop)
        kill `ps -e | grep smsd | awk '{print $1}'`
        sleep 1
        if ps -e | grep smsd >/dev/null; then
          echo "Allowing smsd to terminate gracefully within $maxwait seconds"
          seconds=0
          while ps -e | grep smsd >/dev/null; do
            seconds=`expr $seconds + 1`
            if [ "$seconds" -ge $maxwait ]; then
              echo "Timeout occured, killing smsd hardly."
              kill -9 `ps -e | grep smsd | awk '{print $1}'`
              seconds=0
            fi
            sleep 1
          done
        fi 
        ;;
    restart|reload)
	$0 stop
        $0 start
	;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

