#!/bin/bash
# Log Asterisk Concurrent Calls to MySQL
# Author: Nathan Thomas
# 12/23/2014
#
### BEGIN INIT INFO
# Provides:          astactivecalls
# Required-Start:    $network $syslog $named $local_fs $remote_fs
# Required-Stop:     $network $syslog $named $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Start/stop astactivecalls daemon
### END INIT INFO

PROG=astactivecalls
DESC="Logs Asterisk Concurrent Calls to MySQL"
HOME_DIR=/usr/local/bin
DAEMON=$HOME_DIR/$PROG.sh
PIDFILE=/var/run/$PROG.pid

case "$1" in
        'start')
                # Remove old PIDFILE if it exists
                if [ -s $PIDFILE ] ; then
                        rm -f $PIDFILE
                fi
                # Check for existing process
                ps_check=`ps aux | grep $PROG.sh | grep -v "grep $PROG.sh" | wc -l`
                if [ "$ps_check" -ge "1" ] ; then  # if count of proc >= 1
                        echo "Cannot start $PROG, service is already running."
                        pids=`pgrep $PROG.sh`
                        echo "Try killing these processes first: $pids"
                        exit 1
                else
                        # Start the service
                        echo "Starting $PROG service..."
                        if start-stop-daemon --start --chuid root --pidfile $PIDFILE --chdir $HOME_DIR --background --make-pidfile --exec $DAEMON; then
                                echo "OK"
                        else
                                echo "Error: Could not start $PROG service."
                                exit 3
                        fi
                fi
                ;;
        'stop')
                echo "Stopping $PROG service..."
                if start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE; then
                        rm -f $PIDFILE
                        echo "OK"
                else
                        ps_check=`ps aux | grep $PROG.sh | grep -v "grep $PROG.sh" | wc -l`
                        if [ "$ps_check" -eq "0" ] ; then
                                echo "Error: Could not find an active $PROG process to kill."
                                exit 1
                        else
                                pids=`pgrep $PROG.sh`
                                echo "Error: Could not stop $PROG service. Try killing these processes manually: $pids"
                                exit 1
                        fi
                fi
                ;;
        'restart')
                /etc/init.d/$PROG stop
                sleep 2
                /etc/init.d/$PROG start
                ;;
        'status')
                ps_check=`ps aux | grep $PROG.sh | grep -v "grep $PROG.sh" | wc -l`
                echo "The $PROG service is..."
                if [ -s $PIDFILE -a "$ps_check" -ge "1" ] ; then  # if pidfile exists and size > 0 and # of procs >= 1
                        echo "Running"
                        exit 0
                else
                        echo "Stopped"
                        exit 1
                fi
                ;;
        *)
                echo "Usage:  {start|stop|restart|status}"
                exit 1
                ;;
esac
exit $?

