#!/bin/sh
# RPM update
# ---------------------------------------------- #


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

# import function.sh 
. ${INST_HOME}/function.sh

##### TRAP #######################################
# Deny Ctrl+C 1-Hangup 2-Interrupt 3-Quit 
#             15-Software Termination Signal
trap 'echo "Deny Ctrl+C"' 1 2 3 15

# init  
# Determine LF char & current srv version/type
initialize

# result init
FLAG=0

# RPM Update START
write_log "${MSG_RPM_UPD_START_LOG}"
echo_sp "${MSG_RPM_UPD_START}."

##### Main routine ###############################
# Confirm distribution
#get sys info (e.g. NOW_SMX_DIST=RHELES40 , NOW_SMX_SVR=Auth)
getSMXINFO
if [ "${NOW_SMX_DIST}" = "${UNKNOWN_WORD}" -o "${NOW_SMX_SVR}" = "${UNKNOWN_WORD}" ] ; then
  FLAG=1
  arize_err "dmy" "${MSG_DIST_GET_ERR}"
fi

# Extract the updater archived module
# Extract the RPM files from module_xxx.tar.gz
if [ ! -d $MODULE_DIR ] ; then
  $tar $ZX_OPT $MODULE_PKG_FILE > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    FLAG=1
    arize_err "dmy" "${MSG_EXTR_RPM_MOD_ERR}"
  fi
fi

# Get rpm_list
RPM_LIST=${INST_HOME}/${RPM_LIST_FILE_NAME}

if [ ${FLAG} -eq 0 ] ; then
  ORG_IFS=$IFS
  IFS='
'

  # refer to rpm_list.txt and update if needed
  # refer to rpm_list.txt and output only the curent distribution
  for LINE in `$cat ${RPM_LIST} | $grep -w ${NOW_SMX_DIST}`
  do
    # Get the values from the read LINE
    # APPLY_VER : DISTRIBUTION_MINI from securematrix_infomartion.txt
    # TYPE      : i=install, U=upgrade
    # NEW_RPM   : RPM file (new RPM version)
    # NEW_PKG   : RPM package name (output by 'rpm -qa')
    # OLD_RPM   : RPM file (old RPM version)
    # OLD_PKG   : RPM package name (output by 'rpm -qa', old version)
    APPLY_VER=`echo "$LINE" | $awk -F ',' '{print $2}'`
    TYPE=`echo "$LINE" | $awk -F ',' '{print $3}'`
    NEW_RPM=`echo "$LINE" | $awk -F ',' '{print $4}'`
    NEW_PKG=`echo "$LINE" | $awk -F ',' '{print $5}'`
    OLD_RPM=`echo "$LINE" | $awk -F ',' '{print $6}'`
    OLD_PKG=`echo "$LINE" | $awk -F ',' '{print $7}'`
  
    # Get the current version from install.ini (OLD_VERSION=*.*.*)
    # and confirm whether the version needs to be updated
    eval `$cat ${INSTALL_INI} | $grep "OLD_VERSION"`
    echo "${APPLY_VER}" | $grep -w "${OLD_VERSION}" > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
      # Confirm whether the new pkg version is already installed
      $rpm -qa | $grep "$NEW_PKG" > /dev/null 2>&1
      if [ $? -eq 0 ] ; then
        # if the new version is already installed
        write_log "${MSG_RPM_ALRDY_INST} [ $NEW_PKG ]"
      else
        # if not yet installed
        # check the installation type (U = update)
        if [ "${TYPE}" = "U" ] ; then
          # check if the old target version is in the
          # list of all installed packages
          $rpm -qa | $grep -w "${OLD_PKG}" > /dev/null 2>&1
          if [ $? -ne 0 ] ; then
            # if old package does not exists
            arize_err "dmy" "${MSG_UPD_TARG_NOT_INST}"
            FLAG=1
            break;
          fi
        fi
      
        # Update the the old RPM by using the new RPM package
        if [ -f "${MODULE_DIR}/${NEW_RPM}" ] ; then
          # rpm update command
          $rpm -${TYPE} $MODULE_DIR/${NEW_RPM} 1> /dev/null 2>&1
          if [ $? -ne 0 ] ; then
            # if update operation failed
            arize_err "dmy" "${MSG_RPM_UPD_FAILED} [ ${NEW_RPM} ]"
            FLAG=1
          fi
        else
            # if new RPM file does not exists
            arize_err "dmy" "${MSG_NEW_RPM_NOT_EXST}"
            FLAG=1
            break;
        fi
      fi
    fi
  done
  
  IFS=$ORG_IFS
fi

# delete comp module_dir
if [ -d $MODULE_DIR ] ; then
  $rm -rf $MODULE_DIR 1> /dev/null 2>> ${ERR_LOG_FILE}
fi

# result
if [ $FLAG -eq 0 ] ; then
  # process completion msg and log output
  echo "${MSG_DONE}"
  write_log "${MSG_MIDDLEWARE_SUCCESS}"
else
  # process failed msg and error log output
  echo "${OCCURED_ERROR}"
  echo ""
  msg_err "${MSG_MIDDLEWARE_FAILED}" \
          "${PROSECC_QUIT}"
  arize_err "dmy" "${MSG_MIDDLEWARE_FAILED_S}"
fi

exit $FLAG