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

##### 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_DEPLOY}."
write_log "${MODULE_DEPLOYSTART}"

# Get deploy_list
LIST_FILE=${INST_HOME}/${LIST_FILE_NAME}

# Only backup file_list
#ESCAPE_LIST_FILE=${INST_HOME}/${ESCAPE_LIST_FILE_NAME}

# backup_dir create
if [ ! -d $BACKUP_DIR ] ; then
  $mkdir $BACKUP_DIR 1> /dev/null 2>> ${ERR_LOG_FILE}
  if [ $? -ne 0 ] ; then
    FLAG=1
    arize_err "dmy" "${BACKUP_DIR_MISS}"
  fi
fi

# module_archive comp
# module.tar.gz comp
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" "${MODULE_ARCHIVE_COMPMISS}"
  fi
fi


ORG_IFS=$IFS
IFS='
'

# read list
if [ $FLAG -eq 0 ] ; then
  if [ -s $LIST_FILE ] ; then
    
    # init
    DEPLOY_ERR_FLAG=0                         # deploy_miss flag
    $rm -f $DEPLOY_ERR_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}  # deploy_miss temp init
    echo "" > $ADDED_LIST_FILE                # new_addlist init
    
    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}'`
      
      # old file backup        ################################################
      # when backup, compare old and new                                    ###
      # when old same new, no backup                                        ###
      $cmp $MODULE_DIR/$MODULE_FILE $TARGET_FILE > /dev/null 2>&1
      if [ $? -ne 0 ] ; then
        # compare old and new -> diff -> judge old -> backup
        # when old in backup, no doing
        # when compare old in backup and now old module, diff, no doing
        # when dont exist old in backup, now old backup
        ADD_FLAG=0    # add_flag (default=0)
        if [ -f $BACKUP_PKG_FILE ] ; then
          # TARGET_FILE delete head(/)
          TARGET_FILE_SP=`echo "$TARGET_FILE" | $sed -e s/^'\/'/''/g`
          $tar $T_OPT $BACKUP_PKG_FILE | $grep "$TARGET_FILE_SP" 1> /dev/null 2>> ${ERR_LOG_FILE}
          if [ $? -ne 0 ] ; then
            # Dont exist backup -> new backup
            ADD_FLAG=1
          fi
        else
          # Dont exist backup -> new backup
          ADD_FLAG=1
        fi
        
        # ADD_FLAG is ON and exist now old file when backup
        if [ $ADD_FLAG -eq 1 -a -f $TARGET_FILE ] ; then
          $tar $U_OPT $BACKUP_PKG_FILE $TARGET_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
          if [ $? -ne 0 ] ; then
            FLAG=1
            arize_err "dmy" "${FILE_BACKUP_MISS}"
            arize_err "dmy" "$TARGET_FILE"
            break;
          fi
        fi
      else
        ## cmp result, already existed when no doing
        continue
      fi
      ##### backup END  #######################################################
      
      
      # No deploy -> Only Backup
      #if [ -f ${ESCAPE_LIST_FILE} ] ; then
      #  $cat $ESCAPE_LIST_FILE | grep -v '^#' | grep -v '^$' | grep $MODULE_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
      #  if [ $? -eq 0 ] ; then
      #    continue
      #  fi
      #fi
      
      # judge new file    #####################################################
      # when uninstall, delete new file, therefore write add_list           ###
      if [ ! -f $TARGET_FILE ] ; then
        # No exist now file -> new file
        echo "$TARGET_FILE" >> $ADDED_LIST_FILE
      fi
      
      # deploy from module.tar.gz         #####################################
      # change owner after deploy                                           ###
      CP_RET=0
      CHOWN_RET=0
      PERMIT_RET=0
      
      $cp -f $MODULE_DIR/$MODULE_FILE $TARGET_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
      CP_RET=$?
      $chown $TARGET_OWNER $TARGET_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
      CHOWN_RET=$?
      $chmod $TARGET_PERMIT $TARGET_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
      PERMIT_RET=$?
      
      if [ $CP_RET -ne 0 -o $CHOWN_RET -ne 0 -o $PERMIT_RET -ne 0 ] ; then
        DEPLOY_ERR_FLAG=1
        echo "$TARGET_FILE" >> $DEPLOY_ERR_FILE
      fi
      
      echo_sp "."
      
    done
    
    # deploy miss is error
    if [ $DEPLOY_ERR_FLAG -ne 0 ] ; then
      FLAG=1
      arize_err "dmy" "${MODULE_DEPLOY_MISS}"
      arize_err "dmy" "-----${DEPLOY_MISS_FILE}------------------------------------------------"
      for ERR_FILE in `$cat $DEPLOY_ERR_FILE | grep -v '^#' | grep -v '^$'`
      do
        arize_err "dmy" "$ERR_FILE"
      done
      arize_err "dmy" "---------------------------------------------------------------------------"
    fi
  else
    # Dont exist SMX deploy list when error
    FLAG=1
    arize_err "dmy" "${NOEXPECT_ERROR}: ${INSTINFO_LIST_NOEXIST}"
  fi
  
  
  # add_list into backup
  # when old add_list in backup, delete add_list in backup
  # delete file path head(/) in define.sh
  ADDED_LIST_FILE_SP=`echo "$ADDED_LIST_FILE" | $sed -e s/^'\/'/''/g`
  $tar $T_OPT $BACKUP_PKG_FILE | $grep "${ADDED_LIST_FILE_SP}" > /dev/null
  if [ $? -eq 0 ] ; then
    $tar $F_OPT $BACKUP_PKG_FILE $DEL_OPT $ADDED_LIST_FILE_SP 1> /dev/null 2>> ${ERR_LOG_FILE}
  fi
  $tar $U_OPT $BACKUP_PKG_FILE $ADDED_LIST_FILE 1> /dev/null 2>> ${ERR_LOG_FILE}
  if [ $? -ne 0 ] ; then
    FLAG=1
    arize_err "dmy" "${FILE_BACKUP_MISS}"
    arize_err "dmy" "$ADDED_LIST_FILE"
  fi

fi

IFS=$ORG_IFS


# 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
  $echo "${END}"
  write_log "${MODULE_DEPLOY_END}"
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
