#!/bin/bash
#
# eyesdn  Startup script for the EyeSDN services
#
# chkconfig: 345 96 4
# description: This script is a wrapper to start the EyeSDN Recording Service,
#              the EyeSDN Indexing Service and the EyeSDN Audio Processing Service,
#              which are used for telephone call recordings.
# config: /etc/EyeSDN-USB4/registry.xml
#
### BEGIN INIT INFO
# Provides: eyesdn
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Startup script for the EyeSDN services
# Description: This script is a wrapper to start the EyeSDN Recording Service,
#              the EyeSDN Indexing Service and the EyeSDN Audio Processing Service,
#              which are used for telephone call recordings.
### END INIT INFO

# Path to the service binary.

PID_FILE=/var/run/eyesdn-monitor.pid


RETVAL=0

stop_monitor()
{
    if test -r "${PID_FILE}"; then
        kill -9 $(cat ${PID_FILE}) 2>/dev/null
    fi
}

start_monitor()
{
    stop_monitor
    nohup $0 monitor >/dev/null 2>&1 &
}

start()
{
    local RET=0
    if ! /opt/innoventif/EyeSDN-USB4/eyesdn-recs start; then
        logger -s -t eyesdn "could not start EyeSDN recS"
        RET=1
    elif ! /opt/innoventif/EyeSDN-USB4/eyesdn-icis start; then
        logger -s -t eyesdn "could not start EyeSDN iciS"
        RET=1
    elif ! /opt/innoventif/EyeSDN-USB4/eyesdn-aps start; then
        logger -s -t eyesdn "could not start EyeSDN apS"
        RET=1
    elif ! /opt/innoventif/EyeSDN-USB4/eyesdn-sysconfigd start; then
        logger -s -t eyesdn "could not start EyeSDN sysconfigd"
        RET=1
    else
        logger -s -t eyesdn "EyeSDN services started"

        start_monitor
    fi
    return ${RET}
}

stop()
{
    local RET=0

    stop_monitor

    /opt/innoventif/EyeSDN-USB4/eyesdn-sysconfigd stop || RET=1
    /opt/innoventif/EyeSDN-USB4/eyesdn-recs stop || RET=1
    /opt/innoventif/EyeSDN-USB4/eyesdn-icis stop || RET=1
    /opt/innoventif/EyeSDN-USB4/eyesdn-aps stop || RET=1
    if [ ${RET} -eq 0 ]; then
        logger -s -t eyesdn "EyeSDN services stopped"
    else
        logger -s -t eyesdn "Could not stop all EyeSDN services"
    fi
    return ${RET}
}

broadcast()
{
    local RET=0
    local CMD=$1
    /opt/innoventif/EyeSDN-USB4/eyesdn-recs ${CMD} || RET=1
    /opt/innoventif/EyeSDN-USB4/eyesdn-icis ${CMD} || RET=1
    /opt/innoventif/EyeSDN-USB4/eyesdn-aps ${CMD} || RET=1
    /opt/innoventif/EyeSDN-USB4/eyesdn-sysconfigd ${CMD} || RET=1
    return ${RET}
}

monitor()
{
    echo $$ >${PID_FILE}

    local RUNNING=1
    while true; do
        if test ${RUNNING} -eq 1; then
            for i in recs icis aps sysconfigd; do
                if ! /opt/innoventif/EyeSDN-USB4/eyesdn-$i status; then
                    logger -s -t eyesdn "EyeSDN $i is no longer running"
                    RUNNING=0
                fi
            done
        fi
        if test ${RUNNING} -eq 0; then
            RUNNING=1
            for i in recs icis aps sysconfigd; do
                if ! /etc/init.d/eyesdn-$i status; then
                    if ! /opt/innoventif/EyeSDN-USB4/eyesdn-$i start; then
                        logger -s -t eyesdn "could not restart EyeSDN $i"
                        RUNNING=0
                    fi
                fi
            done
        fi

        sleep 10
    done
}

# See how we were called.
case "$1" in
    start)
	    start
        RETVAL=$?
	    ;;
    stop)
	    stop
        RETVAL=$?
	    ;;
    status|condrestart|try-restart)
        broadcast "$1"
        RETVAL=$?
	    ;;
    restart)
	    stop
	    start
        RETVAL=$?
	    ;;
    monitor)
        monitor
        RETVAL=$?
        ;;
    *)
	    echo $"Usage: ${SRV_NAME} {start|stop|restart|condrestart|try-restart|status|help}"
	    RETVAL=2
        ;;
esac

exit $RETVAL
