#!/bin/bash

# This Start script is used by the app-manager program to start and stop
# applications. app-manager invokes this script with the 'start' option on boot
# and when the app-manager start command is run. app-manager invokes this
# script with the 'stop' option on shutdown and when the app-manager stop
# command is run.
#
# app-manager requires this script to accept the following command line
# arguments:
#
#   start - start the application process(es)
#   stop  - stop the application process(es)
#   restart - restart the application process(es)
#   reload - reload the application configuration (new config downloaded)
#
#
# This script is a fully cusomizable template that can be used to initialize
# the environment for executing the application and starting all processes.
# This script is the supported framework to start and stop an application.

NAME="node-red-custom-app"

NODE_RED_INIT="/etc/init.d/node-red"
NODE_RED_STUNNEL_INIT="/etc/init.d/node-red-stunnel"

# Run script with specified arguments in a clean environment.
function run_cleanenv()                                             
{                                                                   
    local SCRIPT                                                    
    SCRIPT="$1"                                                     
    shift                                                              
                                                                                          
    # [use clean env] [set whitelisted env values] [script to call] [arguments for script]
    # LANG variable is not set as it's not initialized in the current mPower firmware
    env -i "PATH=$PATH" "TERM=$TERM" "$SCRIPT" "$@"                 
    return $?                                                 
}

# The nuts and bolts of starting the application process.
function ExecuteStart {
    echo "ExecuteStart:"
    run_cleanenv "$NODE_RED_STUNNEL_INIT" start >&2
    run_cleanenv "$NODE_RED_INIT" start >&2
}

# Start the application running process(es)
function Start {
    #SetEnv
    #CreateAccess
    #ChangeUser
    ExecuteStart
}

# Stop the application running process(es)
function Stop {
    echo "Stop:"
    run_cleanenv "$NODE_RED_INIT" stop >&2
    run_cleanenv "$NODE_RED_STUNNEL_INIT" stop >&2
}

# Effectively stop and start the application again.
function Restart {
    echo "Restart:"
    Stop
    sleep 1
    Start
}

# Notify the application process that new config files are available
function Reload {
    echo "Reload:"
    Restart
    # Could SIGHUP your process if it supports it instead of restarting
}

#Gather options from command line
# Reset in case getopts has been used previously in the shell.
OPTIND=1

case "$1" in
  # start is invoked by app-manager after install, on boot, during restart, and
  # during config install
  start)
      echo -n "Starting $NAME: "
      Start
      ;;
  # stop is invoked by app-manager before shutdown, during restart, and
  # during config install
  stop)
      echo -n "Stopping $NAME: "
      Stop
      ;;
  # restart is never invoked by app-manager
  restart)
      echo -n "Restarting $NAME: "
      Restart
      ;;
  # reload is invoked by app-manager after a new config has been installed
  reload)
      echo -n "Restarting $NAME: "
      Reload
      ;;
  *)
      echo "Usage: $0 {start|stop|restart|reload}" >&2
      exit 1
      ;;
esac

exit 0
