#!/bin/bash
# Copyright (C) 2016-2022 eGloo Incorporated
#
# This is free software, licensed under the GNU General Public License v3.

### BEGIN INIT INFO
# Provides: netifyd
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the Netify Agent
# Description: Netify Agent
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/usr/sbin/netifyd"
NAME="netifyd"
PIDFILE="/var/run/netifyd/netifyd.pid"
DESC="Netify Agent"

unset TMPDIR

# Exit if the package is not installed
test -x $DAEMON || exit 0

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

start() {
    # Load defaults.
    if [ -f /usr/share/netifyd/functions.sh ]; then
        source /usr/share/netifyd/functions.sh
        load_modules
    fi

    log_daemon_msg "Starting $DESC" "$NAME"

    NETIFYD_OPTS=$(auto_detect_options)

    start-stop-daemon --start --quiet --oknodo --pidfile "$PIDFILE" --exec $DAEMON -- $NETIFYD_OPTS
    status=$?
    log_end_msg $status
    return $status
}   

stop() {
    log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --quiet --retry 5 --oknodo --pidfile "$PIDFILE" --name $NAME
    status=$?
    log_end_msg $status
    return $status
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    status)
        status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
    restart)
        stop
        start
    ;;
    *)
    echo "Usage: $NAME {start|stop|status|restart}"
    exit 3
    ;;
esac
exit $RETVAL

# vi: expandtab shiftwidth=4 softtabstop=4 tabstop=4 syntax=sh
