#!/bin/sh
# version check
# ---------------------------------------------- #


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

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

##### TRAP #######################################
# Deny Ctrl+C
trap 'echo "Deny Ctrl+C"' 1 2 3 15

# init
initialize

# result init
FLAG=0

# GET Version_info
nowVersion=`/opt/cse/smx/common/bin/information.sh | $grep Version | $awk '{print $NF}'`
verNum=`$echo ${nowVersion} | $sed -e s/'\.'/''/g`    # 3.3.*ab [.] delete, [*ab] left

# Now Version Check
targetVerFlag=0
alreadyFlag=0

toVerNum=`$echo ${TO_VER} | $sed -e s/'\.'/''/g`
if [ "${verNum}" = "${toVerNum}" ] ; then
  alreadyFlag=1
else
  for TAR_VER in `$echo ${FROM_VER} | $sed -e s/'\.'/''/g -e s/','/' '/g`
  do
    if [ "${verNum}" = "${TAR_VER}" ] ; then
      targetVerFlag=1
      break
    fi
  done
fi

# install or uninstall
if [ "${UN}" = "" ] ; then
  # already check
  # target check
  if [ ${targetVerFlag} -ne 1 ] ; then
    $echo "${SMX_VERSION_MISS}"
    $echo " "
    if [ ${alreadyFlag} -eq 1 ] ; then
      # arize_err "dmy" "${ALREADY_INSTALL}"
      msg_err "ERR" \
              " " \
              "<!> ${ALREADY_INSTALL} ( ${nowVersion} )" \
              "    ${DOUBLE_INSTALL_TABOO}" \
              "    ${PROSECC_QUIT}" \
              ""
      FLAG=1
    else
      # arize_err "dmy" "${NOT_INSTALL_TARGET}"
      msg_err "ERR" \
              " " \
              "<!> ${NOT_TARGER_SMX} ( ${nowVersion} )" \
              "    ${TARGET_VERSION2}" \
              "    ${PROSECC_QUIT}" \
              ""
      FLAG=1
    fi
  else
      # ${targetVerFlag} -eq 1
      # Write the current version num to install.ini (overwrite)
      $echo "OLD_VERSION=${nowVersion}" > ${INSTALL_INI}
  fi
elif [ "${UN}" = "${UN_MSG}" ] ; then
  # now version is uninstall allow check
  if [ ${alreadyFlag} -ne 1 ] ; then
    $echo "${SMX_VERSION_MISS}"
    $echo " "
    msg_err "ERR" \
            "" \
            "<!> ${NOT_TARGER_SMX} ( ${nowVersion} )" \
            "    ${TARGET_VERSION}" \
            "    ${PROSECC_QUIT}" \
            ""
    FLAG=1
  fi
else
  $echo "${OCCURED_ERROR}"
  $echo
  msg_err "${PROSECC_QUIT}" " " "<!> ${NOEXPECT_ERROR_SAPORT}"
  FLAG=1
fi

exit ${FLAG}
