#!/bin/bash

. /etc/rc.d/init.d/functions
. /etc/rc.d/init.d/init_functions
. /etc/profile.d/SetNNEnvVar.sh

PROCESSCONTROLLOG=/var/log/processcontrol.log


# detaillist()
# for every component process of interest, determine its status
# Note: Some components may not be monitored, ignore for now.
detaillist() {
    cd /nn/etc/scd/monitcfg
    ls -1 M???B_* M???T_* M???F_* > /tmp/startup/processcontrol_expected.txt
    ls -l /proc/*/exe  2>/dev/null > /tmp/startup/processcontrol_active.txt
    while read processList; do
        processName=${processList##M????_}

        grep -w "$processName" /tmp/startup/processcontrol_active.txt > /dev/null
        if [ $? -ne 0 ]; then
            echo "$processName  Automatic       Stopped"
        else
            echo "$processName  Automatic       Running"
        fi
    done < /tmp/startup/processcontrol_expected.txt
}

# status()
# for specified component process of interest, determine its status
# Note: Some components may not be monitored, ignore for now.
status() {
    cd /nn/etc/scd/monitcfg

    if [ -e /var/run/$1.pid ] &&
       [ -d /proc/`cat /var/run/$1.pid` ]; then
        echo "$1  Automatic       Running"
    else
        ls -l /proc/*/exe  2>/dev/null > /tmp/startup/processcontrol_active.txt

        grep -w "$1" /tmp/startup/processcontrol_active.txt > /dev/null
        if [ $? -ne 0 ]; then
            echo "$1  Automatic       Stopped"
        else
            echo "$1  Automatic       Running"
        fi
    fi
}

start() {
        # NOTE: Time is imprecise as pidof takes long and variable period of time.
        # Assume pidof takes between 0.25s and 0.60s.
        # NOTE #2: pidof has been replaced with use of .pid files and /proc fs.

        # Desired timeout = 90s
        START_TIMEOUT=180

        /opt/sbin/monit start $1

        count=0
        result=1
        while [ "$count" -lt $START_TIMEOUT ];do
            if [ -e /var/run/$1.pid ] &&
               [ -d /proc/`cat /var/run/$1.pid` ]; then
                result=0
                count=$START_TIMEOUT
            else
                usleep 500000
                let "count += 1"
            fi
           # echo `date` count= $count
        done

        if [ $result -eq 0 ];then 
            echo "Success";
        else
            echo "Failure";
        fi

        return $result
}

stop() {
        # NOTE: Time is imprecise as pidof takes long and variable period of time.
        # Assume pidof takes between 0.25s and 0.60s.
        # NOTE #2: pidof has been replaced with use of .pid files and /proc fs.

        # Desired timeout = 90s
        SHUTDOWN_TIMEOUT=180

        /opt/sbin/monit stop $1

        count=0
        result=1
        while [ "$count" -lt $SHUTDOWN_TIMEOUT ];do
            if [ -e /var/run/$1.pid ] &&
               [ -d /proc/`cat /var/run/$1.pid` ]; then
                usleep 500000
                let "count += 1"
            else
                result=0
                count=$SHUTDOWN_TIMEOUT
            fi
           # echo `date` count= $count
        done

        if [ $result -eq 0 ];then
            echo "Success";
        else
            echo "Failure";
        fi

        return $result
}

restart() {
        stop $1 > /dev/null
        sleep 1
        start $1 > /dev/null

        result=$?
        if [ $result -eq 0 ];then
            echo "Success";
        else
            echo "Failure";
        fi

        return $result
}

case "$1" in
  detaillist)
        echo "`date` CmdLine: $0 $1" >> $PROCESSCONTROLLOG
        detaillist
        ;;
  status)
        echo "`date` CmdLine: $0 $1" >> $PROCESSCONTROLLOG
        status $2
        ;;
  start)
        echo "`date` CmdLine: $0 $1 $2" >> $PROCESSCONTROLLOG
        start $2
        ;;
  stop)
        echo "`date` CmdLine: $0 $1 $2" >> $PROCESSCONTROLLOG
        stop $2
        ;;
  restart)
        echo "`date` CmdLine: $0 $1 $2" >> $PROCESSCONTROLLOG
        restart $2
        ;;
  *)
        echo "`date` CmdLine: $0 $1 $2" >> $PROCESSCONTROLLOG
        echo $"Usage: $0 {detaillist|start <component>|stop <component>|restart <component>}"
        exit 1
esac

exit $?
