#!/bin/sh
# Do NOT "set -e"

# ----- BEGIN CONFIGURATION -----

# This is the name of the user account that will run the process.
USERNAME=root

# ----- END OF CONFIGURATION -----

MODULENAME=lora
MODULEPATH=/opt/${MODULENAME}
DEFAULTSFILE=$MODULEPATH/etc/defaults
SETTINGSFILE=$MODULEPATH/etc/settings

if [ -f /sbin/iptables ]; then
  IPTABLES=/sbin/iptables
elif [ -f /usr/sbin/iptables ]; then
  IPTABLES=/usr/sbin/iptables
else
  IPTABLES=
fi

# PATH should only include /usr/* if it runs after the mountnfs.sh script.
PATH=$MODULEPATH/sbin:$MODULEPATH/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

DESC="LoRaWAN"

NAME_0=lorawan_monitor
DAEMON_0_ARGS=
DAEMON_0=$MODULEPATH/bin/$NAME_0
PIDFILE_0=/var/run/$NAME_0.pid

DATAPATH=/var/data/$MODULENAME

SCRIPTNAME=$0

# Select the process based on the settings file.
DAEMON=$DAEMON_0
DAEMON_ARGS="$DAEMON_0_ARGS"
PIDFILE=$PIDFILE_0
NAME=$NAME_0

check_requirements_lorawan_monitor()
{
  # This daemon uses the following libraries from the router's firmware.
  REQUIRED="/lib/libc.so.6 /lib/libdl.so.2 /lib/libm.so.6 /lib/libpthread.so.0 /lib/librt.so.1"
  for FILE in $REQUIRED; do
    if [ ! -f "$FILE" ]; then
      logger -p "daemon.warning" -t "${NAME_0}[$$]" "A required file was not found: $FILE."
    fi
  done
}

do_defaults()
{
  return 0
}

# @retval 0 when running
# @retval 2 if not running
do_status()
{
  if [ -r "$PIDFILE" ]; then
    kill -0 `cat $PIDFILE` || return 2
    return 0
  fi
  return 2
}

# A function to generate the software-specific configuration file
# from the Spectre v2 user-module settings file.
#
do_make_config()
{
    # If the path to the configuration file does not exist, create the path.
    [ -d "$DATAPATH" ] || mkdir -p "$DATAPATH"

    ${MODULEPATH}/bin/create_loraserver_cfg
    ${MODULEPATH}/bin/create_lorawan_cfg
}

do_start_lorawan_monitor()
{
  # If the daemon is not an executable file, then exit.
  [ -x "$DAEMON_0" ] || return 3

  check_requirements_lorawan_monitor
  
  # Make the configuration file from the settings file.
  do_make_config

  # Test whether the daemon is running already or not. Return 1 if it is running.
  start-stop-daemon --test --start --quiet --chuid $USERNAME --make-pidfile --pidfile $PIDFILE_0 \
    --background --exec $DAEMON_0 -- $DAEMON_0_ARGS \
    || return 1

  # Attempt to start the daemon. Return 2 if it failed to start.
  start-stop-daemon --start --quiet --chuid $USERNAME --make-pidfile --pidfile $PIDFILE_0 \
    --background --exec $DAEMON_0 -- $DAEMON_0_ARGS \
    || return 2

  # Wait 1 second, then check whether the daemon is running. Return 2 if it is not running.
  sleep 1
  kill -0 `cat $PIDFILE_0` 2>/dev/null || return 2

  return 0
}

do_stop_lorawan_monitor()
{
  killall beam
  killall epmd
  killall basic_pkt_fwd
  # Attempt to stop the daemon.
  start-stop-daemon --stop --quiet --retry=TERM/10/KILL/5 --pidfile $PIDFILE_0 --exec $DAEMON_0
  [ $? -lt 2 ] || return $?

  # Wait for children to finish tool.
  start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON_0
  [ $? -lt 2 ] || return $?

  # Many daemons do not remove their pidfiles when they exit. Remove it now.
  rm -f $PIDFILE_0

  return 0
}

# Start either lorawan_monitor Prior to starting make sure the other process
# (lorawan_monitor) is stopped.
#
do_start()
{
   # Now, start lorawan_monitor
   echo "Starting $DESC ($NAME_0)..."  
   do_start_lorawan_monitor
   RETVAL=$?
   case "$RETVAL" in
     0|1) echo "   Stopping $DESC ($NAME_0) succeeded" ;;
     *)   echo "   Stopping $DESC ($NAME_0) failed" ;;
   esac
  return $RETVAL
}

# Stop lorawan_monitor
#
do_stop()
{
  # Stop lorawan_monitor
  echo "Stopping $DESC ($NAME_0)..."
  do_stop_lorawan_monitor
  RETVAL=$?
  case "$RETVAL" in
    0|1) echo "   Stopping $DESC ($NAME_0) succeeded" ;;
    *)   echo "   Stopping $DESC ($NAME_0) failed" ;;
  esac

  return $RETVAL
}

case "$1" in
  defaults)
    echo "Restoring defaults for $DESC ($NAME)..."
    do_defaults
    RETVAL=$?
    ;;
  start)
    do_start
    RETVAL=$?
    ;;
  status)
    echo "Checking status of $DESC ($NAME)..."
    do_status
    RETVAL=$?
    case "$RETVAL" in
        0) echo "   $DESC ($NAME) is running" ;;
        *) echo "   $DESC ($NAME) is not running" ;;
    esac
    ;;
  stop)
    do_stop
    RETVAL=$?
    ;;
  restart|force-reload)
    #
    # If the "reload" option is implemented then remove the
    # 'force-reload' alias
    #
    echo "Restarting $DESC ($NAME)..."
    do_stop
    RETVAL=$?
    case "$RETVAL" in
      0|1)
        do_start
        RETVAL=$?
        case "$RETVAL" in
          0|1) echo "   Restarting $DESC ($NAME) succeeded" ;;
          *)   echo "   Restarting $DESC ($NAME) failed: couldn't start $NAME" ;;
        esac
        ;;
      *)
        echo "   Restarting $DESC ($NAME) failed: couldn't stop $NAME" ;;
    esac
    ;;
  *)
    echo "Usage: $SCRIPTNAME { defaults | force-reload | restart | start | status | stop }" >&2
    RETVAL=3
    ;;
esac
exit $RETVAL

# ---- END OF FILE ----
