#!/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
    result=$?
    touch $touchfile
    if [ $result -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; result=$?
    start_vnc_proxy

    cd - >/dev/null

    # Remove startfile, because we don't need the delayed start of onapp
    if [ $result -eq 0 ]; then rm -f "$ONAPPD_START" &>/dev/null; fi

    return $result
}

stop(){
    change_dir $APPPATH $ERRORLOGFILE

    stop_daemon; result=$?
    stop_vnc_proxy
    stop_ssh_agent

    cd - >/dev/null

    return $result
}

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
    if [ $? -eq $ONAPPD_TIMEOUT_ERROR ]; then
      touch $ONAPPD_START
      echo 'Finishing running transactions...'
      echo 'OnApp Engine is scheduled for restart'
      return 1
    fi
    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(){
    daemon_status
    for task in 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
}

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
