#!/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.

RETVAL=0

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"
    fi
    return ${RET}
}

stop()
{
    local RET=0
    /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}
}

# 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=$?
	;;
    *)
	echo $"Usage: ${SRV_NAME} {start|stop|restart|condrestart|try-restart|status|help}"
	RETVAL=2
esac

exit $RETVAL
