#!/bin/sh
#
# The script used to have '-e' at the top to signal it to abort if any command's exit code is not 0.
# In 5.9, it was decided to remove this option to allow the execution to continue 
# and perhaps recover on its own to some manageable state.
# You are encouraged to 'set -e' on commands you wish to exit the script immediately in case of failure,
# remember to disable this "abort of failure" after the command execution using 'set +e'
#
### BEGIN INIT INFO
# Provides:          edvrserver
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: exacqVision Server
# Description:       exacqVision Server NVR software is part
#                    of Exacq Technologies exacqVisionPro and
#                    exacqVisionIP products.
#
### END INIT INFO
#
# Author:	Exacq Support <support@exacq.com>.
#
# Version:	1.2-0ubuntu1
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="exacqVision Server"
NAME=exacqd
DAEMON=/usr/local/exacq/server/$NAME
PIDFILE=/var/run/edvrserver.pid
SCRIPTNAME=/etc/init.d/edvrserver

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

# Get LSB functions
. /lib/lsb/init-functions


case "$1" in
    start)
        pid=$(cat "$PIDFILE" 2> /dev/null)
        if ps -p "$pid" > /dev/null 2>&1; then
            echo "$NAME is already running."
            exit 1
        fi

        if [ -x /etc/init.d/s6stretch ]; then
            /etc/init.d/s6stretch start
        fi
        if [ -x /etc/init.d/stretch_dvr ]; then
            /etc/init.d/stretch_dvr start
        fi

        # Starting with 16.04 (i386 and amd64) where some traditionally-packaged executables
        # are no longer packaged and only used system-wide, symlink them as necessary for
        # where server may not be dynamic enough to find system-wide executables.
        # Some systems (dynacolor) do not have lsb_relase.
        if [ -e /etc/os-release ]; then
            eval $(grep ^VERSION_ID= /etc/os-release)
        else
            VERSION_ID=$(lsb_release -rs)
        fi
        if dpkg --compare-versions "$VERSION_ID" ge "16.04" ; then
            # Avahi configures Upstart to be kept always running, so you have to disable before stopping or no effect
            if [ -e /etc/init/avahi-daemon.conf ]; then
                log_begin_msg "Stopping Avahi in order to allow IP camera detection."
                sudo systemctl disable avahi-daemon
                sudo systemctl stop avahi-daemon
                log_end_msg $?
            fi
        fi

        # Starting with 20.06, we always package smartctl for x86 & x64 for NVMe support & JSON output.
        # But since SysmgmtPI expects to call ./smartctl, for dynacolor we have to symlink.
        if [ -h /usr/local/exacq/server/smartctl ]; then
            echo "Verified symlink to smartctl."
        elif [ -x /usr/local/exacq/server/smartctl ]; then
            echo "Verified packaged smartctl."
        elif [ -n "`which smartctl`" ]; then
            log_begin_msg "Symlinking smartctl."
            ln -s `which smartctl` /usr/local/exacq/server/smartctl
            log_end_msg $?
        else
            echo "smartctl totally unavailable."
        fi

        # Now start edvrserver daemon.
        log_begin_msg "Starting $DESC: $NAME"
        which udevadm >/dev/null 2>&1 && udevadm settle --timeout=10
        $DAEMON -b -p $PIDFILE
        log_end_msg $?
        ;;
    stop)
        log_begin_msg "Stopping $DESC: $NAME"
        start-stop-daemon --stop -s TERM --quiet --oknodo --retry 60 --pidfile $PIDFILE --name $NAME
        # if core hangs on shutdown, the above line will only kill exacqd
        start-stop-daemon --stop -s TERM --quiet --oknodo --retry 60 --name core
        log_end_msg $?
        dhcp_pids=$(pidof opendhcpd)
        if [ -n "$dhcp_pids" ]; then
            log_begin_msg "Stopping DHCP service for cameras."
            kill -1 $dhcp_pids
            log_end_msg $?
        fi
        mdnsd_pids=$(pidof mdnsd)
        if [ -n "$mdnsd_pids" ]; then
            log_begin_msg "Stopping mDNS service."
            kill $mdnsd_pids
            log_end_msg $?
        fi
        if [ $? = 0 ] && [ -e ${PIDFILE} ] ; then
            rm -f ${PIDFILE}
        fi
        if [ -x /etc/init.d/stretch_dvr ]; then
            /etc/init.d/stretch_dvr stop
        fi
        if [ -x /etc/init.d/s6stretch ]; then
            /etc/init.d/s6stretch stop
        fi
        ;;
    restart|force-reload)
        $0 stop
        $0 start
        ;;
    status)
        echo -n "Status of $DESC: "
        if [ ! -r "$PIDFILE" ]; then
            echo "$NAME is not running."
            exit 3
        fi
        if read pid < "$PIDFILE" && ps -p "$pid" > /dev/null 2>&1; then
            echo "$NAME is running."
            exit 0
        else
            echo "$NAME is not running but $PIDFILE exists."
            exit 1
        fi
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
        exit 3
        ;;
esac

exit 0
