#!/bin/bash
#
# onapp         This script manages OnApp background processes
#
# chkconfig:    - 86 14
# description:  OnApp background processes include SSH Agent, VNC Proxy, \
# Delayed Jobs and OnApp Daemon.

# Source onapp functions
. /onapp/interface/script/init/onapp_functions.sh

SCRIPTPATH=${APPPATH}/script/init
. ${SCRIPTPATH}/onappd.sh
. ${SCRIPTPATH}/onapp_vnc_proxy.sh
. ${SCRIPTPATH}/onapp_ssh_agent.sh

# Idempotent between reboots
cleanup_pids() {
    touchfile='/tmp/onapp-cleanup'

    modified_after_reboot $touchfile
    res=$?
    touch $touchfile
    if [ $res -eq 1 ]; then return 1; fi
    rm -f $APPPATH/tmp/pids/*
}

usage()
{
    echo "Usage: $0 {start|stop|soft-stop|force-stop|restart|force-restart|status}"
}

start(){
    cleanup_pids

    change_dir $APPPATH $ERRORLOGFILE

    if [ ! -d $PIDPATH ]; then
        mkdir -p $PIDPATH

        if [ $? -ne 0 ]; then
            print_failure
            echo "Failed to create $PIDPATH" | add_timestamp >> $ERRORLOGFILE 2>&1
            exit 1
        fi
    fi

    chown ${USER}:${USER} $PIDPATH

    start_ssh_agent
    start_daemon
    start_vnc_proxy

    cd - >/dev/null
}

stop(){
    change_dir $APPPATH $ERRORLOGFILE

    stop_daemon
    stop_vnc_proxy
    stop_ssh_agent

    cd - >/dev/null
# TODO check whether this is still relevant
    # echo -n "Waiting while all Runners will stop "
    # RETRY=1
    # pgrep -f 'servers:stop' >/dev/null 2>&1
    # while [ $? -eq 0 -a $RETRY -lt $MAX_RETRIES ]
    # do
    #     echo -n "."
    #     RETRY=$((${RETRY}+1))
    #     sleep $SLEEP
    #     pgrep -f 'servers:stop' >/dev/null 2>&1
    # done
    # if [ $RETRY -lt $MAX_RETRIES ]; then
    #     echo " done"
    # else
    #     echo " give up"
    # fi
}

soft-stop() {
    change_dir $APPPATH $ERRORLOGFILE

    stop_daemon_instance
    stop_vnc_proxy
    stop_ssh_agent

    cd - >/dev/null
}

force-stop(){
    force_stop_daemon
    stop_vnc_proxy
    stop_ssh_agent
    cd - >/dev/null
}

restart(){
    stop
    printf "\n\tAll OnApp subsystems are stopped\n\n"
    sleep $SLEEP && start
}

force-restart(){
    force-stop
    printf "\n\tAll OnApp subsystems are stopped\n\n"
    sleep $SLEEP && start
}

status(){
    for task in onapp_engine ssh_agent vnc_proxy ; do
      is_running "$PIDPATH/${task}.pid"
        case $? in
          0)
            if [ -z "$1" ]; then
                echo "$task is stopped"
            else
                echo "$task,stopped"
            fi
            ;;
          1)
            if [ -z "$1" ]; then
                echo "$task is stopped but pid file exists"
            else
                echo "$task,stopped"
            fi
            ;;
          2)
            if [ -z "$1" ]; then
                echo "$task (pid `get_pid_from_file "$PIDPATH/${task}.pid"`) is running..."
            else
                echo "$task,running"
            fi
            ;;
        esac
    done
    if [ -z "$1" ]; then
        get_zombie "onapp_daemon"
    fi
}

case "$1" in
  status)
        status "$2"
        ;;
  start)
        start
        ;;
  stop)
        stop
        ;;
  force-stop)
        force-stop
        ;;
  restart)
        restart
        ;;
  force-restart)
        force-restart
        ;;
  soft-stop)
        soft-stop
        ;;
  *)

        usage
        ;;
esac
