#! /bin/bash

# This script determines if the ExternalDataManager MSU Data Loading instance
# is running or not.  If the instance is not running, a check is made to 
# determine if the msu_pnp variable exists.  If the msu_pnp variable does not 
# exist, the ExternalDataManager instance is not started.  If the msu_pnp
# variable exists and the value is FALSE, the ExternalDataManager instance is
# not restarted.  If the msu_pnp variable exists and the value is TRUE, the
# ExternalDataManager is started. This script file is going
# to run as a cron job within the /etc/cron.hourly directory at a test cell 
# to ensure the ExternalDataManager is always running for MSU Data Loading if
# the msu_pnp variable = TRUE
#
# Usage: /cyflex/cmds/check_msu_loading
#
# Author: Jennifer Schuck
# 
# History
#   05/15/2020 - Jennifer Schuck - CCS-1077 - Initial release.

#  This file is to:
#     - determine if the ExternalDataManager instance for MSU Loading
#       is running
#       - if is running
#         - do nothing
#       - if is not running 
#          - determine if the msu_pnp variable exists
#            - if does not exist, do nothing
#            - if does exist and value = FALSE, do nothing
#            - if does exist and value = TRUE
#             - start instance

# Determine if ExternalDataManager Instance for MSU Loading is Running

PROG=com.cybermetrix.cyflex.extdatman.ExternalDataManager
FILE=/specs/extdatman.cummins.msu

# Find the PID for the MSU data loading process.
PID=$(ps -ef | grep $PROG | grep $FILE | grep -v grep | tr -s ' ' | cut -f2 -d\ )

if [ "$PID" = "" ]; then

   # MSU data loading isn't running, determine if it should be restarted by
   # looking at the msu_pnp variable.

   msu_pnp_var=`gval msu_pnp`

   # the ExternalDataManager MSU Data Loading instance will only be started
   # if the msu_pnp variable exists and is TRUE
   
   if test "$msu_pnp_var" = "1"
   then
      # start MSU Data Loading
   
      DATE=`date +"%y%m%d"`
      su $LOGNAME -c /cyflex/cmds/extdatman.cummins.msu >> /data/errors/extdatman.msu.$DATE 2>>/data/errors/extdatman.msu.errs.$DATE &
      chown $LOGNAME:users /data/errors/extdatman.msu.$DATE
      chown $LOGNAME:users /data/errors/extdatman.msu.errs.$DATE
   fi
fi
