#!/bin/sh
#
# snetaid    /etc/init.d initscript for snetaid
#

### BEGIN INIT INFO
# Provides:          snetaid
# Required-Start:    ubus $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start and stop snetaid
# Description:       
### END INIT INFO

# Exit inmediatelly in case of failure
# http://www.gnu.org/software/bash/manual/bashref.html#The-Set-Builtin
set -e

# Import LSB funcions:
. /lib/lsb/init-functions

NAME=snetaid
PROG=@SBINDIR@/$NAME
CONFIGFILE=@CONFDIR@/$NAME.conf
PIDFILE=/run/snetaid/$NAME.pid
PIPE="/var/log/netaid_events.pipe"
UBUS_PID="/var/run/snetaid_ubus.pid"

# load an ini file as a set of environment variables, echoing them to stdout
# $1 is the path to the file
source_ini_file() {

    local FILE_PATH line KEY VALUE
   
    FILE_PATH=$1
   
    /bin/cat $FILE_PATH | /bin/sed "s/.*\[.*\].*//g" | \
    while read line; do 
   
        KEY=$(echo $line | /bin/sed "s/\(^.*\)=.*/\1/g");
        VALUE=$(echo $line | /bin/sed "s/^.*=\(.*\)$/\1/g");
      
        if [ -n "$KEY" ]; then 
      
            echo "${KEY}=${VALUE}"
        fi
    done
}

if [ -f "$CONFIGFILE" ]; then
    eval $(source_ini_file "$CONFIGFILE")
fi

WIRED_IF=${wired_interface:-${WIRED_IF:-eth0}}
WIRELESS_IF=${wireless_interface:-${WIRELESS_IF:-wlan0}}
PIDFILE=${pidfile:-/run/snetaid/snetaid.pid}

##proguser=root
if [ `id -u` != "0" ] && [ "$1" = "start" -o "$1" = "stop" ] ; then
  echo "You must be root to start, stop or restart snetaid."
  exit 1
fi

# -b: background, -a: config, -p: pid, -e/w: interfaces
DAEMON_OPTS="-b -a $CONFIGFILE -p $PIDFILE -e $WIRED_IF -w $WIRELESS_IF"

case "$1" in
    start)
        log_action_begin_msg "Starting $NAME"
        
        # Prepare directories and pipe
        [ -p "$PIPE" ] || mkfifo "$PIPE"
        
        # Start snetaid
        start-stop-daemon --start --quiet --oknodo --exec "$PROG" -- $DAEMON_OPTS
        
        # Start ubus monitor (We use --startas to avoid conflicts)
        start-stop-daemon --start --quiet --background \
            --make-pidfile --pidfile "$UBUS_PID" \
            --startas /bin/sh -- -c "ubus listen network.log > $PIPE"
            
        log_action_end_msg 0
        ;;

    stop)
        log_daemon_msg "Stopping $NAME"
        
        # Stop ubus moniotr
        start-stop-daemon --stop --quiet --pidfile "$UBUS_PID" --remove-pidfile --retry 5
        
        # Stop snetaid
        start-stop-daemon --stop --quiet --exec "$PROG" --oknodo --retry 5
        
        log_end_msg $?
        ;;

    status)
        $PROG --check -p $PIDFILE
		;;

    restart)
        log_action_begin_msg "Restarting $NAME and refreshing network state"
        $0 stop || true
        if ifquery --version >/dev/null 2>&1; then
            ifreload -a
        fi
        sleep 1
        $0 start
        log_action_end_msg $?
        ;;

    reload|force-reload) 
        log_daemon_msg "Reloading UBUS services:" "$NAME"
        start-stop-daemon --stop --signal 1 --exec "$PROG"
        log_end_msg $?
        ;;

    *)
        echo "Usage:  $0 {start|stop|status|restart|reload|force-reload}"
        exit 2
        ;;
esac
