#!/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

# 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=AppServer
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

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
}

do_start_application_server()
{
  # If the daemon is not an executable file, then exit.
  [ -x "$DAEMON_0" ] || return 3
  
  # 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_application_server()
{
  # 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)..."  
   
   LORA_APPSER_ENABLE=`cat "$SETTINGSFILE" | awk 'BEGIN{FS="=";found=""}{if($1=="LORA_APPSER_ENABLE")if(NF>=2)if(length($2)>0)found=substr($0,index($0,$2))}END{printf("%s",found)}'`
   if [ -z "$LORA_APPSER_ENABLE" ] || [ "$LORA_APPSER_ENABLE" == "0" ]; then
	exit 0
   fi
   
   do_start_application_server
   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_application_server
  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 ----
