#!/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="skf_client"
DESC="skf_client"
DAEMON="$APP_DIR/skf_client.py"
PID="/var/run/skfclient.pid"

# Set custom environment variables for the application execution
function SetEnv {
# Environment Variables
#    CONFIG_DIR
#    APP_DIR
#    APP_ID

    echo "skfc SetEnv"

    echo "skfc CONFIG_DIR: $CONFIG_DIR"
    logger -t  skfc "CONFIG_DIR: $CONFIG_DIR"
    echo "skfc APP_DIR: $APP_DIR"
    logger -t skfc "APP_DIR: $APP_DIR"
    echo "skfc APP_ID: $APP_ID"
    logger -t skfc "APP_ID: $APP_ID"
    echo "skfc PID: $PID"
    logger -t skfc "PID: $PID"
}

# This function can be used to chmod files and implement any security initialization
function CreateAccess {
    echo "CreateAccess:"
}

# Intended to be a hook allowing the application to be executed as a non-root user.
function ChangeUser {
    echo "ChangeUser:"
}

function ExecuteStart {
    echo "ExecuteStart:"
    /usr/sbin/start-stop-daemon --start --background --pidfile "$PID" --make-pidfile --startas "$DAEMON" --chdir $APP_DIR
}

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

# Stop the application running process(es)
function Stop {
    echo "Stop:"

    # Older version uses daemon. If pidfile
    # from older version exists then stop it
    # using the same daemon it was used to start it.
    if [ -f "${PID}" ]; then
        /usr/sbin/start-stop-daemon --stop --pidfile "$PID" --retry 60
    fi
}

# 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, and
    # during config install
    start)
        echo -n "Starting $DESC: "
        Start
        ;;
    # stop is invoked by app-manager before shutdown, and during config install
    stop)
        echo -n "Stopping $DESC: "
        Stop
        ;;
    # restart is invoked by app-manager when the app is explicitly restarted
    restart)
        echo -n "Restarting $DESC: "
        Restart
        ;;
    # reload is invoked by app-manager after a new config has been installed
    reload)
        echo -n "Restarting $DESC: "
        Reload
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload}" >&2
        exit 1
        ;;
esac

exit 0
