#!/bin/sh
#  $Id: $
# -----------------------------------------------------------------------------
#  Wrapper for "cam_patrol" engine control
# -----------------------------------------------------------------------------
#  Author: Andrey Fomenko
#  Edited by: 
#  QA by: Alexey Tsibulnik
#  Copyright: videoNEXT Federal Inc, 2015
# -----------------------------------------------------------------------------

# must have an environment set or refuse to execute
[ -z "$APL" ] && echo Variable APL is not set && exit 1

# configuration
USR=apl
PID=$APL_VAR/pids/cam_patrol.pid
RUN_TIMEOUT=100


# =============================================================================
#   UTILITIES / HELPERS
# =============================================================================

Log () { # ---------------------------------------------------------- Log -----
    #echo "$*" >> /tmp/debug
    logger -t cam_patrol_ctl "$*"
}

Kill_apl () { # ------------------------------------------------ Kill_apl -----
 SIG=$1
 ps -au "$USR" -o pid,user,args | grep '/opt/sarch' | grep "$USR" | 
      grep -v aplbase| grep -v '/opt/sarch/sm/'| grep -v 'master_restore'| grep -v 'smxctl'|
      cut -c 1-6 | xargs kill $SIG >/dev/null 2>&1  
}

Pid_age () { # -------------------------------------------------- Pid_age -----
    # returns the age of the pid file, if absent then return 9999
    now=`date +%s`
    if [ -f  $PID ]; then
        mtime=`date +%s -r $PID`
        age=$[ $now - $mtime ]
        echo $age
    else 
        echo 9999
    fi
}

StartPatrol () {
    Log StartPatrol: prep stage
    # kill all relevant engines, just to be on safe side
    Kill_apl -9
    # make sure that all locally-cached conf-files will be re-replicated upon start
    export NODE_ID=`grep OBJID $APL_CONF/node/conf|cut -d '=' -f 2`
    echo "update _objs set rtime=null where node_id=$NODE_ID and obj>99;" | psql -h s_master apl apl
    # 
    Log StartPatrol: start patrolling engine
    su "$USR" -c '$APL/cam/bin/cam_patrol >/dev/null 2>/dev/null &'
    Log StartPatrol: finished
}

StopPatrol () {
    Log StopPatrol: stop-nicely-stage
    Kill_apl
    sleep 2
    Log StopPatrol: stop-hard-stage
    Kill_apl -9
    sleep 1     # it was observed that sometimes mretr processes remained active
                # likely patrol was starting it was it was killed?
                # let's kill again to safeguard!
    Kill_apl -9
    if Check_cam_patrol_running; then
        Log StopPatrol: failed to stop cam_patrol, it is running
    else
        Log StopPatrol: deleting PID file
        rm $PID
    fi
    Log StopPatrol: finished
}

check_internal_cam_patrol_running () {
    # return 0 if cam_patrol running
    [ -f  $PID ]                 || return 1
    pid=`cat $PID`
    [ "$pid" -gt 1 2>/dev/null ] || return 2
    proc=`ps -p $pid -o comm=`
    [ "$proc" != 'cam_patrol' ]  && return 3
    return 0
}

Check_cam_patrol_running () { # ---------------- Check_cam_patrol_running -----
    # return 0 if cam_patrol running
    check_internal_cam_patrol_running && return 0
    # pid has to be checked twice since pid may be replaced at the exact  
    # time of checking. The second check guarantees correct status
    sleep 1                 # recheck after 1 sec
    check_internal_cam_patrol_running
} 

Pid_age () { # -------------------------------------------------- Pid_age -----
    # returns the age of the pid file, if absent then retrun 9999
    now=`date +%s`
    if [ -f  $PID ]; then
    mtime=`date +%s -r $PID`
    age=$[ $now - $mtime ]
    echo $age
    else 
    echo 9999
    fi
}

# =============================================================================
#  MAIN
# =============================================================================

case "$1" in
    start)
        Log cam_patrol_ctl: patrol start requested
        if Check_cam_patrol_running; then
            Log WARNING: cam_patrol is already running
        else
            StartPatrol
        fi
        ;;
    stop)
        Log cam_patrol_ctl: patrol stop requested
        if ! Check_cam_patrol_running; then
            Log WARNING: cam_patrol is already stopped
        else
            StopPatrol
        fi
        ;;
    restart)
        Log cam_patrol_ctl: patrol restart requested
        if Check_cam_patrol_running; then
            StopPatrol
        else
            Log warning: cam_patrol is not running while restart was requested
        fi
        StartPatrol
        ;;
    status)
        Log "cam_patrol_ctl: patrol status requested"
        if Check_cam_patrol_running; then
            pid_age=`Pid_age`;
            if [ $pid_age -le $RUN_TIMEOUT ]; then
                echo running
            else
                Log FAILED: cam_patrol is frozen for $pid_age seconds
                echo failed
            fi
        else
            echo stopped
        fi
        ;;
    *)
        echo Usage: $0 '[start|stop|restart|status]'
        exit 3
esac

exit 0

