#!/bin/sh
# SMX patch uninstall
# ---------------------------------------------- #

##### Function ###################################

##### 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


# START
echo_sp "${MODULE_RESTORE}."
write_log "${MODULE_RESTORESTART}"

LIST_FILE=${INST_HOME}/${LIST_FILE_NAME}


# exist backup
# Dont exist backup when error
if [ ! -f $BACKUP_PKG_FILE ] ; then
  FLAG=1
  arize_err "dmy" "${BACKUP_NOEXIST}"
  arize_err "dmy" "${CANT_RESTORE}"
fi

# comp module.tar.gz
# cause modify after patch install
if [ ! -d $MODULE_DIR ] ; then
  $tar $ZX_OPT $MODULE_PKG_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
  if [ $? -ne 0 ] ; then
    FLAG=1
    arize_err "dmy" "${MODIFY_MODULE_MISS}"
  fi
fi

# comp backup(temp)
# mkdir temp_dir
if [ -d $MODULE_DIR ] ; then
  $tar $X_OPT $BACKUP_PKG_FILE -C $MODULE_DIR 1> /dev/null 2>> ${ERR_LOG_FILE}
  if [ $? -ne 0 ] ; then
    FLAG=1
    arize_err "dmy" "${MODIFY_BACKUP_MISS}"
  fi
fi

# get add_list from backup
# delete head(/)
# comp at /
ADDED_LIST_FILE_SP=`echo "$ADDED_LIST_FILE" | $sed -e s/^'\/'/''/g`
$tar $X_OPT $BACKUP_PKG_FILE -C / $ADDED_LIST_FILE_SP 1> /dev/null 2>> ${ERR_LOG_FILE}
if [ $? -ne 0 ] ; then
  FLAG=1
  arize_err "dmy" "${ADDLIST_COMP_MISS}"
fi


ORG_IFS=$IFS
IFS='
'

# read list
# deploy from backup after delete deployed module
if [ $FLAG -eq 0 ] ; then
  if [ -s $LIST_FILE ] ; then
    
    # salvage_flag init (default=OFF, there is salvage file when flag ON)
    SALVAGE_FLAG=0
    
    for LINE in `$cat $LIST_FILE | grep -v '^#' | grep -v '^$'`
    do
      MODULE_FILE=`echo "$LINE" | $awk '{print $1}'`
      TARGET_OWNER=`echo "$LINE" | $awk '{print $2}'`
      TARGET_PERMIT=`echo "$LINE" | $awk '{print $3}'`
      TARGET_FILE=`echo "$LINE" | $awk '{print $4}'`
      
      
      # salvage modify file          ##########################################
      # modify file backup after patch_install                              ###
      # compare now module and patch_module                                 ###
      # when exist salvage_file in salvage_backup, no doing                 ###
      $cmp $MODULE_DIR/$MODULE_FILE $TARGET_FILE > /dev/null 2>&1
      if [ $? -ne 0 ] ; then
        # compare now module and patch_module -> diff -> salvage file?
        if [ -f $MODULE_DIR/$TARGET_FILE -a -f $TARGET_FILE ] ; then
          $cmp $MODULE_DIR/$TARGET_FILE $TARGET_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
          if [ $? -ne 0 ] ; then
            # compare now module and salvage_backup_module -> diff -> no double -> salvage
            $tar $U_OPT $SALVAGE_PKG_FILE $TARGET_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
            if [ $? -ne 0 ] ; then
              FLAG=1
              arize_err "dmy" "${MODIFYFILE_BACKUP_MISS}"
              arize_err "dmy" "$TARGET_FILE"
              break;
            else
              SALVAGE_FLAG=1
            fi
          fi
        fi
      fi
      
      # delete deployed module, however only new file            ##############
      $grep "$TARGET_FILE" $ADDED_LIST_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
      if [ $? -eq 0 ] ; then
        # exist file in add_list -> new file -> delete
        $rm -f $TARGET_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
        if [ $? -ne 0 ] ; then
          FLAG=1
          arize_err "dmy" #${DEPLOYED_FILE_DELETE_MISS}"
          break;
        fi
      fi
      
      echo_sp "."
      
    done
    
  else
    # No exist SMX list when error
    FLAG=1
    arize_err "dmy" "${NOEXPECT_ERROR}: ${INSTINFO_LIST_NOEXIST}"
  fi
  
fi

IFS=$ORG_IFS


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


# backup comp at /
if [ -f $BACKUP_PKG_FILE -a $FLAG -eq 0 ] ; then
  $tar $X_OPT $BACKUP_PKG_FILE -C / 1> /dev/null 2>> ${ERR_LOG_FILE}
  if [ $? -ne 0 ] ; then
    FLAG=1
    arize_err "dmy" "${BACKUP_RESTORE_MISS}"
  fi
#  cd $CRT_DIR > /dev/null
fi

# result
if [ $FLAG -eq 0 ] ; then
  $echo "${END}"
  write_log "${MODULE_RESTOREEND}"
  if [ $SALVAGE_FLAG -ne 0 ] ; then
    $echo "${INSTAFTER_MODIFY_BACKUP}"
  fi
else
  $echo "${OCCURED_ERROR}"
  $echo ""
  msg_err "${SMX_MODULE_DEPLOY_MISS}" \
          "${PROSECC_QUIT}" \
          " " \
          "<!> ${NOEXPECT_ERROR_SAPORT}"
  arize_err "dmy" "${SMX_MODULE_DEPLOY_MISS}"
fi

exit $FLAG
