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

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

##### Grobal Parameter ###########################
# decide MSG
. ${INST_HOME}/decide_msg.sh
eval `$grep "UN_MSG=" ${MSG_PROP}`

UN="${UN_MSG}"
export UN
MODE=""
export MODE

# error flag
FLAG=0

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

##### Function ###################################
#----- Version Check -----------------------#
version_check(){
  ${INST_HOME}/version_check.sh
  VER_RET=$?
  if [ ${VER_RET} -ne 0 ] ; then
    # quit
    $echo "${SMX_INST_QUIT}"
    exit 1
  fi
}

#----- Service Check ---------------------#
service_check(){
  ${INST_HOME}/service_check.sh
  if [ $? -ne 0 ] ; then
    # quit
    $echo "${SMX_INST_QUIT}"
    exit 1
  fi
}

#----- restore module ---------------#
file_restore(){
  ${INST_HOME}/file_restore.sh
  if [ $? -ne 0 ] ; then
    # quit
    quit
  fi
}

#----- ISB Server check -------------------------#
server_check(){
  ${INST_HOME}/server_check.sh "ISB"
  if [ $? -ne 0 ] ; then
    # quit
    $echo "${SMX_INST_QUIT}"
    exit 1
  fi
}

#----- reverse parse_param ----------------------#
reverse_parse_param(){
  ${INST_HOME}/reverse_parse_param.sh "replace2.xml" "insert2.xml"
  if [ $? -ne 0 ] ; then
    # quit
    quit
  fi
}

#----- Delete JSP generated files --------------#
jsp_gen_files_delete(){
  write_log "${MSG_JSP_FILES_DEL_START}"
  # delete isb folder
  $rm -rf ${JSP_OUT_ISB_DIR_DEL}/* > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    FLAG=1
  fi
  # delete EZS folder
  $rm -rf ${JSP_OUT_EZS_DIR_DEL}/* > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    FLAG=1
  fi
  # error check
  if [ ${FLAG} -ne 0 ] ; then
    $echo " "
    msg_err "ERR" \
            "" \
            "<!> ${MSG_JSP_FILES_DEL_ERROR}" \
            "    ${NOEXPECT_ERROR_SAPORT}"
    arize_err "dmy" "${MSG_JSP_FILES_DEL_ERROR}"
    
    # quit
    quit
  fi
  write_log "${MSG_JSP_FILES_DEL_END}"
}

#----- final -------------------------#
finalize(){
  ${INST_HOME}/finalize.sh
  if [ $? -ne 0 ] ; then
    # quit
    quit
  fi
}

##### Main routine ###############################

# init
initialize

# check root_user
check_root

# current dir check
current_dir_check

# server type check
server_check

# version check
version_check

# check SMX process
service_check

# Patch_uninstall START
write_log "--- ${SECUREMATRIX} Patch UnInstall START [ to Ver-$TO_VER ] ---"
$echo "${SMX_UNINST_START}"

# Reverse Param Setting
reverse_parse_param

# restore module
file_restore

# Delete JSP generated files
jsp_gen_files_delete

# final
finalize

# patch install end
write_log "---- ${SECUREMATRIX} Patch UnInstall END [ to Ver-$TO_VER ] ----"
$echo  "${SMX_UNINST_END}"
$echo  " "
msg_dialog  "${START_COMMAND_MSG}" \
            " ( ${ROOTUSER_COMMAND} )" \
            " " \
            "${START_COMMAND}: [ ${INST_HOME}/${START_ALL_CMD} ]" \
            " "

exit 0
