#!/bin/bash
wd_c=0
start()
{
        while [ 1 ]
        do
                HTTPD_STAT="`ps -e|grep speaker-cgi|grep -v grep`"
                if [ -z "${HTTPD_STAT}" ];then
                        killall speaker-cgi > /dev/null 2>&1
                        sleep 1
                        /etc/scripts/httpd.sh start
                        wd_c=`expr $wd_c + 1`
                fi

                APIAGENT_STAT="`ps|grep api_agent|grep -v grep`"
                if [ -z "${APIAGENT_STAT}" ];then
                        /etc/scripts/api_agent &
                fi

                REDIS_STAT="`ps -e|grep 'redis-server'|grep -v grep`"
                if [ -z "${REDIS_STAT}" ];then
                        /usr/sbin/redis-server /etc/redis.conf > /dev/null 2>&1 &
                        wd_c=`expr $wd_c + 1`
                fi

                OUTPUTCTRL_STAT="`ps|grep output_control|grep -v grep`"
                if [ -z "${OUTPUTCTRL_STAT}" ];then
                        /etc/scripts/output_control &
                fi

                PROCESS_STAT="`ps|grep service_process|grep -v grep`"
                if [ -z "${PROCESS_STAT}" ];then
                        /etc/scripts/service_process &
                fi

                SIPPHONE_STAT="`ps -e|grep baresip|grep -v grep`"
                SIPSEV_STAT="`ps|grep 'bareservice'|grep -v grep`"
                if [ -z "${SIPPHONE_STAT}" -o -z "${SIPSEV_STAT}" ];then
                        killall baresip > /dev/null 2>&1
                        killall bareservice > /dev/null 2>&1
                        # sleep 1
                        /etc/scripts/sipphone.sh start
                        wd_c=`expr $wd_c + 1`
                fi

                ISPEAKER_STAT="`ps -e|grep -w "ispeaker" |grep -v grep`"
                if [ -z "${ISPEAKER_STAT}" ];then
                        MULTICAST_ENABLE="`sysconf /etc/speaker.conf get multicast_player enable`"
                        if [ "foo${MULTICAST_ENABLE}" = "fooyes" ];then
                                killall ispeaker > /dev/null 2>&1
                                /etc/scripts/ispeaker start
                                wd_c=`expr $wd_c + 1`
                        fi
                fi

                ONVIFSERVER_STAT="`ps|grep -w onvifserver|grep -v grep`"
                RTSPSERVER_STAT="`ps|grep -w rtspserver|grep -v grep`"
                if [ -z "${ONVIFSERVER_STAT}" -o -z "${RTSPSERVER_STAT}" ];then
                        ONVIF_ENABLE="`sysconf /etc/speaker.conf get onvif enable`"
                        if [ "foo${ONVIF_ENABLE}" = "fooyes" ];then
                                /etc/scripts/shell_action.sh set_onvif
                        fi
                fi

                ONVIF_STAT="`ps -e|grep 'onvif-discover'|grep -v grep`"
                if [ -z "${ONVIF_STAT}" ];then
                        /etc/scripts/onvif-discover > /dev/null 2>&1 &
                fi

                SPEAKER_CONF_ERROR_STAT="`grep "\['" /oem/etc/speaker.conf`"
                if [ ! -z "${SPEAKER_CONF_ERROR_STAT}" ];then
                        sed -i "s/\['//g" /oem/etc/speaker.conf
                        sed -i "s/'\]//g" /oem/etc/speaker.conf
                        sed -i "s/', '//g" /oem/etc/speaker.conf
                        sync
                fi

                VOLUME_CONF_ERROR_STAT="`grep "\['" /oem/etc/volctrl.conf`"
                if [ ! -z "${VOLUME_CONF_ERROR_STAT}" ];then
                        sed -i "s/\['//g" /oem/etc/volctrl.conf
                        sed -i "s/'\]//g" /oem/etc/volctrl.conf
                        sed -i "s/', '//g" /oem/etc/volctrl.conf
                        sync
                fi

                [ $wd_c -gt 10 ] && /sbin/reboot

                sleep 15
        done
}

stop()
{
        # stop sipphone
        /etc/scripts/sipphone.sh stop
        # stop ispeaker
        killall ispeaker > /dev/null 2>&1
        killall service_process > /dev/null 2>&1
        killall api_agent > /dev/null 2>&1
        killall ptpd2 > /dev/null 2>&1
        killall onvifserver > /dev/null 2>&1
        killall rtspserver > /dev/null 2>&1
}

restart()
{
        stop
        sleep 2
        start&
}

reload()
{
        stop
        sleep 2
        start&
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        restart
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?
