#!/bin/sh
# service stop
# ---------------------------------------------- #

# 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 SERVICE_CMD
svrCnt=0
stopErrorFlag=0

##### Function ###################################
#--- Check if services are still running after performing a STOP operation ---#
service_stopped_check(){
  $ps $PS_OPT | $grep "$AUTH_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -eq 0 ] ; then
    stopErrorFlag=`expr $stopErrorFlag + 1`
  fi

  #----- Admin check ------#
  $ps $PS_OPT | $grep "$ADMIN_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -eq 0 ] ; then
    stopErrorFlag=`expr $stopErrorFlag + 1`
  fi

  #----- Radius check ------#
  $ps $PS_OPT | $grep "$RADIUS_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -eq 0 ] ; then
    stopErrorFlag=`expr $stopErrorFlag + 1`
  fi

  #----- EasySetup check ------#
  $ps $PS_OPT | $grep "$EZS_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -eq 0 ] ; then
    stopErrorFlag=`expr $stopErrorFlag + 1`
  fi

  #----- Cron check ------#
  $ps $PS_OPT | $grep "$CRON_CMD" | $grep -v 'grep' > /dev/null
  if [ $? -eq 0 ] ; then
    stopErrorFlag=`expr $stopErrorFlag + 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_STOP}"
    $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 ###########################################################
#----- Auth check ------#
$ps $PS_OPT | $grep "$AUTH_CMD" | $grep -v 'grep' > /dev/null
if [ $? -eq 0 ] ; then
  FLAG=1
  SERVICE_LIST[$svrCnt]="    ${AUTHSERVER}"
  SERVICE_CMD[$svrCnt]="$AUTH_STOP"
  svrCnt=`expr $svrCnt + 1`
fi

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

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

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

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

# result
CLEAR

if [ $svrCnt -eq 0 ] ; then
  $echo ""
  msg_dialog  "${SMXSERVICE_STOPOK}"
  
  exit 0

else
  msg_info  "${SMXSERVICE_STOP}" \
            " " \
            "[ ${SERVICESTOP_TARGET} ]" \
            "${SERVICE_LIST[0]}" \
            "${SERVICE_LIST[1]}" \
            "${SERVICE_LIST[2]}" \
            "${SERVICE_LIST[3]}" \
            "${SERVICE_LIST[4]}" \
            "${SERVICE_LIST[5]}" \
            "${SERVICE_LIST[6]}" \
            " " \
            "${STOPQUESTION}"

  response="hoge"
  while [ "$response" != y  -a "$response" != n ]
  do
    echo_sp "${STOP_ANS} (y) / ${NOSTOP_ANS} (n) : "
    read response
  done
fi

if [ "$response" = y ]; then
  echo_sp "${SERVICE_STOPPING}."
  for STOP_CMD in "${SERVICE_CMD[@]}"
  do
    $STOP_CMD > /dev/null 2> /dev/stderr
    echo_sp "."
  done
  # Check whether or not the services were successfully stopped
  service_stopped_check
  if [ "$stopErrorFlag" -ne 0 ]; then
    echo_sp "${MSG_ERROR}"
    $echo " "
    msg_dialog "${SMXSERVICE_STOPERROR}"
    exit 1
  else
    echo_sp "${FINISH}"
    $echo " "
    msg_dialog "${SMXSERVICE_STOPFINISH}"
    exit 0
  fi
else
  msg_dialog "${SMXSERVICE_STOPQUIT}"
  exit 0
fi