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

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

##### Grobal Parameter ###########################
# decide MSG
. ${INST_HOME}/decide_msg.sh

UN=""
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
}

#----- module deploy -------------------#
deploy(){
  ${INST_HOME}/deploy.sh
  if [ $? -ne 0 ] ; then
    write_log "${INST_BACK_START}"
    arize_err "dmy" "${INST_BACK_START}"
    # back module
    back_module
    # restore previous RPM settings
    ${INST_HOME}/rpm_install4uninst.sh
    # restore the temporarily saved file
    swap_restore "${CLIENTS_CONF}"
    # quit
    quit
  fi
}

#----- save file to a temp location -------------------#
temp_save_file(){
  swap_file "${CLIENTS_CONF}"
  if [ $? -ne 0 ] ; then
    write_log "${INST_BACK_START}"
    arize_err "dmy" "${INST_BACK_START}"
    # back module
    # back_module
    # quit
    quit
  fi
}

#----- Update RPM -------------------#
update_RPM(){
  ${INST_HOME}/rpm_install.sh
  if [ $? -ne 0 ] ; then
    write_log "${INST_BACK_START}"
    arize_err "dmy" "${INST_BACK_START}"
    # restore previous RPM settings
    ${INST_HOME}/rpm_install4uninst.sh
    # restore the temporarily saved file (retry)
    swap_restore "${CLIENTS_CONF}"
    # back module
    # back_module
    # quit
    quit
  fi
}

#----- restore the saved file from the temp location -----#
restore_saved_file(){
  swap_restore "${CLIENTS_CONF}"
  if [ $? -ne 0 ] ; then
    write_log "${INST_BACK_START}"
    arize_err "dmy" "${INST_BACK_START}"
    # restore previous RPM settings
    ${INST_HOME}/rpm_install4uninst.sh
    # restore the temporarily saved file (retry)
    swap_restore "${CLIENTS_CONF}"
    # back module
    # back_module
    # quit
    quit
  fi
}

#----- param setting ------------------------------------#
parse_param(){
  ${INST_HOME}/parse_param.sh "replace.xml" "insert.xml" "insert2.xml"
  if [ $? -ne 0 ] ; then
    write_log "${INST_BACK_START}"
    arize_err "dmy" "${INST_BACK_START}"
    # back param_setting
    ${INST_HOME}/reverse_parse_param.sh "replace.xml" "insert.xml" "insert2.xml"
    # back module
    back_module
    # restore previous RPM settings
    ${INST_HOME}/rpm_install4uninst.sh
    # restore the temporarily saved file
    swap_restore "${CLIENTS_CONF}"
    # quit
    quit
  fi
}

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

#----- WinLogon deploy ---------------#
new_win_module(){
  ${INST_HOME}/new_win_module.sh
  if [ $? -ne 0 ] ; then
    write_log "${INST_BACK_START}"
    arize_err "dmy" "${INST_BACK_START}"
    # new win module back
    ${INST_HOME}/win_restore.sh
    # back param_setting
    ${INST_HOME}/reverse_parse_param.sh "replace.xml" "insert.xml" "insert2.xml"
    # back module
    back_module
    # restore previous RPM settings
    ${INST_HOME}/rpm_install4uninst.sh
    # restore the temporarily saved file
    swap_restore "${CLIENTS_CONF}"
    # quit
    quit
  fi
}

#----- Delete JSP generated files --------------#
jsp_gen_files_delete(){
  write_log "${MSG_JSP_FILES_DEL_START}"
  # delete admin folder
  $rm -rf ${JSP_OUT_ADMIN_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}"
    
    write_log "${INST_BACK_START}"
    arize_err "dmy" "${INST_BACK_START}"
    # new win module back
    ${INST_HOME}/win_restore.sh
    # back param_setting
    ${INST_HOME}/reverse_parse_param.sh "replace.xml" "insert.xml" "insert2.xml"
    # back module
    back_module
    # restore previous RPM settings
    ${INST_HOME}/rpm_install4uninst.sh
    # restore the temporarily saved file
    swap_restore "${CLIENTS_CONF}"
    # quit
    quit
  fi
  write_log "${MSG_JSP_FILES_DEL_END}"
}

#----- final -------------------------#
finalize(){
  ${INST_HOME}/finalize.sh
  if [ $? -ne 0 ] ; then
    write_log "${INST_BACK_START}"
    arize_err "dmy" "${INST_BACK_START}"
    # new win module back
    ${INST_HOME}/win_restore.sh
    # back param_setting
    ${INST_HOME}/reverse_parse_param.sh "replace.xml" "insert.xml" "insert2.xml"
    # back module
    back_module
    # restore previous RPM settings
    ${INST_HOME}/rpm_install4uninst.sh
    # restore the temporarily saved file
    swap_restore "${CLIENTS_CONF}"
    # 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_install START
write_log "--- ${SECUREMATRIX} Patch Install START [ to Ver-$TO_VER ] ---"
$echo "${SMX_INST_START}"

# Temporarily save the clients.conf file to a temp location
temp_save_file

# Update RPM
update_RPM

# Restore the clients.conf file that was saved in the
# temp location before the update
restore_saved_file

# module deploy
deploy

# Param Setting
parse_param

# WinLogon deploy
new_win_module

# Delete JSP generated files
jsp_gen_files_delete

# final
finalize

# patch install end
write_log "---- ${SECUREMATRIX} Patch Install END [ to Ver-$TO_VER ] ----"
$echo  "${SMX_INST_END}"
$echo  " "
msg_dialog  "${START_COMMAND_MSG}" \
            " ( ${ROOTUSER_COMMAND} )" \
            " " \
            "${START_COMMAND}: [ ${INST_HOME}/${START_ALL_CMD} ]" \
            " " \
            "${DUMMYSMX_SET_MSG}" \
            "  ${DUMMY_SET}" \
            " " \
            "${SETCHANGE_COMMAND_MSG_HEAD}" \
            "${SETCHANGE_COMMAND_MSG_TAIL}" \
            " ( ${ROOTUSER_COMMAND} )" \
            " " \
            "${SET_COMMAND}: [ ${FIXEDDMYPATH} ]" \
            " "

exit 0