#!/bin/sh
# service_check
# ---------------------------------------------- #

# Installer directory
INST_HOME=/home/smxd/ver-3_3_2
export INST_HOME

# decide msg file
. ${INST_HOME}/decide_msg.sh
. ${MSG_PROP}

##### Initialize variables ######################
# param_init
FLAG=0
ISB_FLAG=0
declare -a SERVICE_LIST
declare -a SERVICE_CMD
svrCnt=0
startErrorFlag=0

##### Function ###################################
#-- Check if services are still stopped after performing a START operation --#
service_started_check(){
  #----- ISB check ------#
  $ps $PS_OPT | $grep "$ISB_CMD2" | $grep -v 'grep' > /dev/null
  if [ $? -ne 0 ] ; then
    startErrorFlag=`expr $startErrorFlag + 1`
  fi
  
  #----- EasySetup check ------#
  $ps $PS_OPT | $grep "$EZS_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -ne 0 ] ; then
    startErrorFlag=`expr $startErrorFlag + 1`
  fi
  
  #----- Cron check ------#
  $ps $PS_OPT | $grep "$CRON_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -ne 0 ] ; then
    startErrorFlag=`expr $startErrorFlag + 1`
  fi

  #----- Check if isbr exists ------#
  # Use the existing check_isbr.sh for checking
  CHECK_ISBR='/opt/cse/smx/common/bin/monitor/check_isbr.sh "/var/opt/cse/smx/isb/tmp/isbr_pid" "/opt/cse/smx/isb/bin/isbr" ""'
  # Normal = "on" Error = "off" or "ERROR"
  ISBR_STATUS=`eval $CHECK_ISBR`
  if [ "${ISBR_STATUS}" != "on" ] ; then
    startErrorFlag=`expr $startErrorFlag + 1`
  fi
}

#----- check root_user -------------------------#
check_root_local(){
  RTC=/tmp/rootchecker
  touch $RTC > /dev/null 2>&1
  chown root:root $RTC > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    $rm -f $RTC > /dev/null 2>&1
    $echo ""
    $echo "    ${WARNING}: ${ROOTUSER_EXCUTE_START}"
    $echo ""
    exit 1
  fi
  $rm -f $RTC > /dev/null 2>&1
}

##### Main routine ###############################
# Function read
. ${INST_HOME}/function.sh

# check root_user
check_root_local

# init
initialize

##### stopping process_check #################################################
#----- ISB check ------#
$ps $PS_OPT | $grep "$ISB_CMD2" | $grep -v 'grep' > /dev/null
if [ $? -ne 0 ] ; then
  FLAG=1
  ISB_FLAG=1
  SERVICE_LIST[$svrCnt]="    ${ISBSERVER}"
  SERVICE_CMD[$svrCnt]="$ISB_START"
  svrCnt=`expr $svrCnt + 1`
fi

#----- EasySetup check ------#
$ps $PS_OPT | $grep "$EZS_CMD" | $grep -v 'grep' > /dev/null
if [ $? -ne 0 ] ; then
  FLAG=1
  SERVICE_LIST[$svrCnt]="    ${EASYSETUP}"
  SERVICE_CMD[$svrCnt]="$EZS_START"
  svrCnt=`expr $svrCnt + 1`
fi

#----- Cron check ------#
$ps $PS_OPT | $grep "$CRON_CMD" | $grep -v 'grep' > /dev/null
if [ $? -ne 0 ] ; then
  FLAG=1
  SERVICE_LIST[$svrCnt]="    ${CRON}"
  SERVICE_CMD[$svrCnt]="$CRON_START"
  svrCnt=`expr $svrCnt + 1`
fi

#----- Check if isbr exists ------#
# Use the existing check_isbr.sh for checking
CHECK_ISBR='/opt/cse/smx/common/bin/monitor/check_isbr.sh "/var/opt/cse/smx/isb/tmp/isbr_pid" "/opt/cse/smx/isb/bin/isbr" ""'
# Normal = "on" Error = "off" or "ERROR"
ISBR_STATUS=`eval $CHECK_ISBR`
if [ "${ISBR_STATUS}" = "on" ] ; then
  if [ $ISB_FLAG -eq 1 ] ; then
    SERVICE_CMD[0]="$ISB_RESTART"
  fi
else
  if [ $ISB_FLAG -eq 0 ] ; then
    FLAG=1
    SERVICE_LIST[$svrCnt]="    ${ISBSERVER}"
    SERVICE_CMD[$svrCnt]="$ISB_RESTART"
    svrCnt=`expr $svrCnt + 1`
  fi
fi


# result
CLEAR

if [ $FLAG -eq 0 ] ; then
  $echo ""
  msg_dialog  "${SMXSERVICE_STARTOK}"
  
  exit 0

else
  msg_info  "${SMXSERVICE_START}" \
            " " \
            "[ ${SERVICESTART_TARGET} ]" \
            "${SERVICE_LIST[0]}" \
            "${SERVICE_LIST[1]}" \
            "${SERVICE_LIST[2]}" \
            " "
fi

echo_sp "${SERVICE_STARTING}."
for START_CMD in "${SERVICE_CMD[@]}"
do
  $START_CMD > /dev/null 2> /dev/stderr
  echo_sp "."
done

# Check whether or not the services were successfully started
service_started_check
if [ "$startErrorFlag" -ne 0 ]; then
  echo_sp "${MSG_ERROR}"
  $echo " "
  msg_dialog "${SMXSERVICE_STARTERROR}"
  exit 1
else
  echo_sp "${FINISH}"
  $echo " "
  msg_dialog "${SMXSERVICE_STARTFINISH}"
  exit 0
fi
