#!/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
declare -a SERVICE_LIST
declare -a DB_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(){
  #----- Auth check ------#
  $ps $PS_OPT | $grep "$AUTH_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -ne 0 ] ; then
    startErrorFlag=`expr $startErrorFlag + 1`
  fi
  
  #----- Admin check ------#
  if [ $ISPRIM -eq 0 ] ; then
    $ps $PS_OPT | $grep "$ADMIN_CMD" | $grep -v 'grep' > /dev/null
    if [ $? -ne 0 ] ; then
      startErrorFlag=`expr $startErrorFlag + 1`
    fi
  fi
  
  #----- Radius check ------#
  if [ $RADIUS_FLAG -eq 0 ] ; then
    $ps $PS_OPT | $grep "$RADIUS_CMD" | $grep -v 'grep' > /dev/null
    if [ $? -ne 0 ] ; then
      startErrorFlag=`expr $startErrorFlag + 1`
    fi
  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 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

##### SMX check ###############################################################
# RADIUS check 0=use, 1=deny
isUseRadius
RADIUS_FLAG=$?

# Primary or Secondary Check 0=primary, 1=secondary
isPrimaryAuth
ISPRIM=$?

# Cluster check
get_auth_cluster_mode
CLUSTERIS2_FLAG=$?

##### need alive process_check ################################################
#----- Postgresql check -----#
$ps $PS_OPT | $grep "$PGSQL_CMD" | $grep -v 'grep' > /dev/null
if [ $? -ne 0 ] ; then
  FLAG=1
  DB_SERVICE_LIST[$svrCnt]="    ${DATABASE}"
  svrCnt=`expr $svrCnt + 1`
fi

#----- Cluster check --------#
if [ ${CLUSTERIS2_FLAG} -eq 2 ] ; then
  $ps $PS_OPT | $grep "$PGPOOL_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -ne 0 ] ; then
    FLAG=1
    DB_SERVICE_LIST[$svrCnt]="    ${CLUSTER}"
    svrCnt=`expr $svrCnt + 1`
  fi
fi

#### DB status check #####
if [ $svrCnt -ne 0 ] ; then
  CLEAR
  if [ ${CLUSTERIS2_FLAG} -eq 2 ] ; then
    msg_info "${SMXSERVICE_STOPPING}" \
             "[ ${STOPSERVICE} ]" \
             "${DB_SERVICE_LIST[0]}" \
             "${DB_SERVICE_LIST[1]}" \
             " " \
             "${COMMANDEXCUTE_4SELECT}" \
             "${DBPROBLEM_3SELECT}" \
             " " \
             "${COMMAND}: [ ${CLUSTER_MAN_CMD} ]" \
             " "
  else
    msg_info "${SMXSERVICE_STOPPING}" \
             "[ ${STOPSERVICE} ]" \
             "${DB_SERVICE_LIST[0]}" \
             " " \
             "${COMMANDEXCUTE_START}" \
             " " \
             "${COMMAND}: [ ${PGSQL_START} ]" \
             "        : [ ${INST_HOME}/${START_ALL_CMD} ]" \
             " "
  fi
  exit 0
fi

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

#----- Admin check ------#
if [ $ISPRIM -eq 0 ] ; then
  $ps $PS_OPT | $grep "$ADMIN_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -ne 0 ] ; then
    FLAG=1
    SERVICE_LIST[$svrCnt]="    ${ADMINSERVER}"
    SERVICE_CMD[$svrCnt]="$ADMIN_START"
    svrCnt=`expr $svrCnt + 1`
  fi
fi

#----- Radius check ------#
if [ $RADIUS_FLAG -eq 0 ] ; then
  $ps $PS_OPT | $grep "$RADIUS_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -ne 0 ] ; then
    FLAG=1
    SERVICE_LIST[$svrCnt]="    ${RADIUSSERVER}"
    SERVICE_CMD[$svrCnt]="$RADIUS_START"
    svrCnt=`expr $svrCnt + 1`
  fi
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


# result
CLEAR

if [ $svrCnt -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]}" \
            "${SERVICE_LIST[3]}" \
            "${SERVICE_LIST[4]}" \
            "${SERVICE_LIST[5]}" \
            "${SERVICE_LIST[6]}" \
            " "
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


