#!/bin/sh
#
# yate:	Starts the Yet Another Telephony Engine
#
# chkconfig: 345 95 10
# description: Starts and stops YATE used as Telephony Server
#
# processname: yate
# pidfile: /var/run/yate/yate.pid
#

### BEGIN INIT INFO
# Provides:          yate
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Yate server
# Description:       Yet another telephony engine
### END INIT INFO



DAEMON=/usr/bin/yate
PIDFILE=/var/run/yate/yate.pid
LOG=/var/log/yate

# Extra Yate command line options
OPTS="-rs -vvv -l $LOG"

# Include yate defaults if available
if [ -f /etc/default/yate ] ; then
	. /etc/default/yate
fi

set -e


# Check if the process is running looking at /proc
# (works for all users)
running()
{
    # No pidfile, probably no daemon present
    [ ! -f "$PIDFILE" ] && return 1
    # Obtain the pid and check it against the binary name
    pid=`head -n 1 $PIDFILE`
    [ -z "$pid" ] && return 1
    [ ! -d /proc/$pid ] &&  return 1
    cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
    # Is this the expected child?
    [ "$cmd" != "$DAEMON" ] &&  return 1
    return 0
}


start() {
	test -x $DAEMON || exit 0
	echo -n $"Starting YATE: "
	unset DISPLAY
	touch $LOG
	chown yate:yate $LOG
	start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid yate:yate --exec $DAEMON -- -d -p $PIDFILE $OPTS
	if running ; then
		echo "yate."
	else
		echo " ERROR."
	fi
}

stop() {
	echo -n $"Stopping YATE: "
	start-stop-daemon --stop --oknodo --quiet --retry 5 --pidfile $PIDFILE --exec $DAEMON
	echo "yate."
}

reload() {
	echo -n $"Reloading YATE configuration: "
	start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDFILE --exec $DAEMON
	echo "done."
}



# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    reload)
  	reload
        ;;
    status)
	echo -n "$LABEL is "
	if running ;  then
		echo "running"
	else
		echo " not running."
		exit 1
	fi
	;;
    restart)
	stop
	start
	;;
    condrestart)
	if running; then
	    stop
	    start
	fi
	;;
    *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
	;;
esac

exit 0
# vim: noexpandtab shiftwidth=8


