#!/bin/sh
# property type
# Replace reverse ( for uninstall )
# ---------------------------------------------- #

# param init
declare -a REP_LIST
declare -a ADD_LIST

ORG_NUM=0
ARG_NUM=0
DELIM_NUM=0
TARGET_NUM=0

##### Function ###################################
#----- param init2 ------------------------------#
init(){
  # sed cmd param before set sed
  # At getSed add param[ -e s/*/*/g ]
  SED_CMD="$sed"
  
  # file attribute
  FILENAME=""
  REALNAME=""
  DELIMLINE=""
  WRITETYPE=""
  
  # XML param change
  ORG_PARAM=""
  VAL_PARAM=""
  VAL_DATA=""
  ARG_PARAM=""
  DELIM_PARAM=""
  TARGET_PARAM=""
  REPEAT_PARAM=""
  
  # flag
  FILE_FLAG=0
  PATH_FLAG=0
  REAL_FLAG=0
  UNIT_FLAG=0
  REPEAT_FLAG=0
  PLATOON_FLAG=0
  CMT_FLAG=0
  ERR_FLAG=0
}

#----- get value from XML tag ------------------#
getParam(){     #args:tagStrign, TagName(ex:FILENAME,ORG,VALUE)
  TAG_STR=$1
  TAG_NAME=$2
  RES=`$echo "$TAG_STR" | $sed -e s/"<$TAG_NAME>"//g -e s/"<\/$TAG_NAME>"//g`
  if [ $? -ne 0 ] ; then
    arize_err "PRM001" "String | tag [$TAG_STR | $TAG_NAME]"
    $echo ""
    return 1
  else
    $echo "$RES"
    return 0
  fi
}

#----- sed parameter ----------------#
getSedParam(){  # args:ORG,VALUE(change before[org] after[value] ex:$HOST -> auth01)
  if [ "${1}" = "" -o "${2}" = "" ] ; then
    $echo ""
  else
    TMP_ORG=`$echo "$1" | $sed -e s/'"'/'\\\"'/g | $sed -e s/'\/'/'\\\\\/'/g | $sed -e s/'*'/'\\\*'/g`  # ["] [/] [*] possible
    TMP_VAL=`$echo "$2" | $sed -e s/'"'/'\\\"'/g | $sed -e s/'\/'/'\\\\\/'/g | $sed -e s/'*'/'\\\*'/g`  # ["] [/] [*] possible
    RES=' -e s/"'$TMP_VAL'"/"'$TMP_ORG'"/g'
    $echo "$RES"
  fi
}

#----- sed comment_out --------#
getSedCommentOut(){   #args:comment_out head String
  TMP_CMT=`$echo "$1" | $sed -e s/'"'/'\\\"'/g | $sed -e s/'\/'/'\\\\\/'/g | $sed -e s/'*'/'\\\*'/g`  # ["] [/] [*] possible
  RES=' -e s/"^'$TMP_CMT'"/"#'$TMP_CMT'"/g'
  $echo "$RES"
}

#----- separate argment fileName ----------------#
separate_arg_file(){
  REP_ARG_NUM=0
  INS_ARG_NUM=0
  for TARGET in $@
  do
    $echo "${TARGET}" | $grep "replace" > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
      REP_LIST[${REP_ARG_NUM}]="${TARGET}"
      REP_ARG_NUM=`expr $REP_ARG_NUM + 1`
      continue
    fi

    $echo "${TARGET}" | $grep "insert" > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
      ADD_LIST[${INS_ARG_NUM}]="${TARGET}"
      INS_ARG_NUM=`expr $INS_ARG_NUM + 1`
      continue
    fi
  done
}

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


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

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

# read setting file
load_setup_properties

# init
initialize
init

# result init
FLAG=0

# separate argment fileName
separate_arg_file $@

# START
echo_sp "${PARAM_SETBACK}."
write_log "${PARAM_SETBACKSTART}"

# It is possible to read space_line
# need IFS set
TMP_IFS=$IFS
IFS='
'

# read replace XML, replace parameter ---------#
for REP_FILE in ${REP_LIST[*]}
do
	if [ -f ${REP_FILE} ] ; then
	for READLINE in `$cat ${REP_FILE}`
	do
	  # delete line_code(CR)
	  LINE=`$echo "$READLINE" | $tr -d "\r"`
	  
	#  echo "[$LINE]"
	  
	  # <FILE> process   ##########################################
	  #file START tag                                          ####
	  $echo "$LINE" | $grep ^"<FILE>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    init
	    FILE_FLAG=1
	    continue
	  fi
	  
	  # <FILENAME> prosecc   ######################################
	  # base file name tag                                     ####
	  $echo "$LINE" | $grep ^"<FILENAME>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    if [ $FILE_FLAG -eq 1 ] ; then
	      FILENAME=`getParam "$LINE" "FILENAME"`
	      if [ $? -ne 0 ] ; then
	        # get file name miss
	        ERR_FLAG=1
	      else
	        PATH_FLAG=1
	      fi
	    fi
	    continue
	  fi
	  
	  # <REALNAME> process   ######################################
	  # deploy path name tag                                   ####
	  $echo "$LINE" | $grep ^"<REALNAME>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    if [ $FILE_FLAG -eq 1 ] ; then
	      REALNAME=`getParam "$LINE" "REALNAME"`
	      if [ $? -ne 0 ] ; then
	        # get file name miss
	        ERR_FLAG=1
	      else
	        REAL_FLAG=1
	      fi
	    fi
	    continue
	  fi
	  
	  # <UNIT> process   ##########################################
	  # replace unit tag                                       ####
	  $echo "$LINE" | $grep ^"<UNIT>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    if [ $PATH_FLAG -eq 1 ] ; then
	      ORG_PARAM=""
	      VAL_PARAM=""
	      VAL_DATA=""
	      UNIT_FLAG=1
	    fi
	    continue
	  fi
	  
	  
	  ### Only uninstall reverse sed #############################
	  # Exchange ORG Tag and VALUE Tag at uninstall           ####
	  # <ORG> process    #########################################
	  # base replace String tag                               ####
	  $echo "$LINE" | $grep ^"<ORG>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    if [ $UNIT_FLAG -eq 1 ] ; then
	      ORG_PARAM=`getParam "$LINE" "ORG"`
	      if [ $? -ne 0 ] ; then
	        # get base replace miss
	        ERR_FLAG=1
	      fi
	    fi
	    continue
	  fi
	  
	  # <VALUE> process   ###########################################
	  # replace String tag                                       ####
	  # sed param create                                         ####
	  $echo "$LINE" | $grep ^"<VALUE>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    if [ $UNIT_FLAG -eq 1 -a "$ORG_PARAM" != "" ] ; then
	      VAL_PARAM=`getParam "$LINE" "VALUE"`
	      if [ $? -ne 0 ] ; then
	        # get replace String miss
	        ERR_FLAG=1
	      else
	        VAL_DATA=`eval $echo $VAL_PARAM`
	        if [ "$VAL_DATA" != "" ] ; then
              # not null after sed, go
              # replace String on base replace String
	          RET=`getSedParam "$ORG_PARAM" "$VAL_DATA"`
	          SED_CMD="$SED_CMD""$RET"
	          RET=""
	        fi
	      fi
	   fi
	    continue
	  fi
	  
	  # <COMMENT> process   ######################################
	  # comment_out START tag                                 ####
	  # comment write after replace null                      ####
	  $echo "$LINE" | $grep ^"<COMMENT>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    if [ $UNIT_FLAG -eq 1 -a "$ORG_PARAM" != "" -a "$VAL_PARAM" != "" -a "$VAL_DATA" = "" ] ; then
	      CMT_FLAG=1
	    fi
	    continue
	  fi
	  
	  # </COMMENT> process   ######################################
	  # comment_out END tag                                    ####
	  $echo "$LINE" | $grep ^"</COMMENT>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    if [ $CMT_FLAG -eq 1 ] ; then
	      CMT_FLAG=0
	    fi
	    continue
	  fi
	  
	  # </UNIT> process   #########################################
	  # replace unit END tag                                   ####
	  $echo "$LINE" | $grep ^"</UNIT>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    if [ $UNIT_FLAG -eq 1 ] ; then
	      UNIT_FLAG=0
	      ORG_PARAM=""
	      VAL_PARAM=""
	      VAL_DATA=""
	    fi
	    continue
	  fi
	  
	  # </FILE> process   #########################################
	  # file END tag                                           ####
	  # now sed START                                          ####
	  $echo "$LINE" | $grep ^"</FILE>" 1> /dev/null 2>> ${ERR_LOG_FILE}
	  if [ $? -eq 0 ] ; then
	    
	    write_log "Replace file. [$FILENAME]"
	    
	    # sed process
	    if [ $ERR_FLAG -eq 0 ] ; then
		  # when not error, excute
		  # base file copy temp
	      $cat $FILENAME > $TMP_FILE 2>&1
	      if [ $? -ne 0 ] ; then
	        ERR_FLAG=1
	        arize_err "PRM002" "File. [$FILENAME]"
	      else
            if [ "${SED_CMD}" = "$sed" ] ; then
               continue;
            fi
	        # copy file sed, and output basefile_name+(.change)
	        eval "$SED_CMD" < $TMP_FILE > $FILENAME".change" 2>&1   # sed
	        if [ $? -ne 0 ] ; then
	          ERR_FLAG=1
	          arize_err "PRM003" "Sed command. [ $SED_CMD < $TMP_FILE > ${FILENAME}.change ]"
	        fi
	      fi
	      
	      if [ -f $TMP_FILE ] ; then
	        $rm -f $TMP_FILE 2>&1
	      fi
	      
	      # copy basefile_name+(.change) on true_file
	      # Dont read REALNAME is error
	      if [ $REAL_FLAG -eq 1 ] ; then
	        $cp -f $FILENAME".change" $REALNAME 1> /dev/null 2>> ${ERR_LOG_FILE}
	        if [ $? -ne 0 ] ; then
	          ERR_FLAG=1
	          arize_err "PRM004" "File copy. [$FILENAME.change > $REALNAME]"
	        fi
	      else
	        # Dont read REALNAME is error
	        ERR_FLAG=1
	        arize_err "PRM005" "File. [$FILENAME]"
	      fi
	    fi
	    
	    # replace miss is ERROR_FLAG ON
	    if [ $ERR_FLAG -ne 0 ] ; then
	      FLAG=1
	    fi
	    
	    # one file end
	    echo_sp "."
	    
	    init
	    continue
	  fi
	  
	  # Not Tag process   ########################################
	  # between <COMMENT> and </COMMENT> (Not tag line)       ####
	  # treat comment_out                                     ####
	  # add line head [#]                                     ####
	  if [ $CMT_FLAG -eq 1 ] ; then
	    RET=`getSedCommentOut "$LINE"`
	    SED_CMD="$SED_CMD""$RET"
	    RET=""
	  fi
	  
	done
	fi
done

###################
# file add write
###################

for ADD_FILE in ${ADD_LIST[*]}
do
	if [ -f $ADD_FILE ] ; then
	for READLINE in `$cat $ADD_FILE`
	do
		# delete linecode(CR)
		LINE=`$echo "$READLINE" | $tr -d "\r"`
		
		#  echo "[$LINE]"
		
		# <FILE> process   ##########################################
		# one FILE START tag                                     ####
		$echo "$LINE" | $grep ^"<FILE>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		init
		
		if [ -f $TMP_FILE ] ; then
		  $rm -f $TMP_FILE 2>&1
		fi
		
		FILE_FLAG=1
		continue
		fi
		
		# <FILENAME> process   ######################################
		# base insert file tag                                   ####
		$echo "$LINE" | $grep ^"<FILENAME>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		if [ $FILE_FLAG -eq 1 ] ; then
		  FILENAME=`getParam "$LINE" "FILENAME"`
		  if [ $? -ne 0 ] ; then
		    # get file name miss
		    ERR_FLAG=1
		  else
		    PATH_FLAG=1
		  fi
		fi
		continue
		fi
		
		# <REALNAME> process   ######################################
		# insert file tag                                        ####
		$echo "$LINE" | $grep ^"<REALNAME>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		if [ $FILE_FLAG -eq 1 ] ; then
		  REALNAME=`getParam "$LINE" "REALNAME"`
		  if [ $? -ne 0 ] ; then
		    # get file name miss
		    ERR_FLAG=1
		  else
		    REAL_FLAG=1
		  fi
		fi
		continue
		fi
		
		# <DELIMLINE> process   ######################################
		# SMX separate line tag                                   ####
		$echo "$LINE" | $grep ^"<DELIMLINE>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		DELIMLINE=`getParam "$LINE" "DELIMLINE"`
		if [ $? -ne 0 ] ; then
		  # get SMX separate line miss
		  ERR_FLAG=1
		fi
		continue
		fi
		
		# <UNIT> process   ##########################################
		# insert unit START tag                                  ####
		$echo "$LINE" | $grep ^"<UNIT>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		if [ $PATH_FLAG -eq 1 -a $REAL_FLAG -eq 1 ] ; then
		  VAL_PARAM=""
		  VAL_DATA=""
		  ORG_PARAM=""
		  UNIT_FLAG=1
		fi
		continue
		fi
		
		# <VALUE> process   #########################################
		# replace paramString($***)                              ####
		# null after replace, when no doing                      ####
		$echo "$LINE" | $grep ^"<VALUE>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		if [ $UNIT_FLAG -eq 1 ] ; then
		  VAL_PARAM=`getParam "$LINE" "VALUE"`
		  if [ $? -ne 0 ] ; then
		    # replace miss
		    ERR_FLAG=1
		  else
		    VAL_DATA=`eval $echo $VAL_PARAM`
		    if [ "$VAL_DATA" = "" ] ; then
		      # null after replace, when no doing and flag ON
		      CMT_FLAG=2
		    fi
		  fi
		fi
		continue
		fi
		
		# <ORG> process   ###########################################
		# base insert tag                                        ####
		$echo "$LINE" | $grep ^"<ORG>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		if [ $UNIT_FLAG -eq 1 ] ; then
		  ORG_PARAM=`getParam "$LINE" "ORG"`
		  if [ $? -ne 0 ] ; then
		    # get base insert miss
		    ERR_FLAG=1
		  fi
		fi
		continue
		fi
		
		# <TEXT> process   ##########################################
		# add String START tag                                   ####
		$echo "$LINE" | $grep ^"<TEXT>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		if [ $UNIT_FLAG -eq 1 ] ; then
		  if [ $CMT_FLAG -eq 2 ] ; then
		    # VALUE is null when no doing
		    CMT_FLAG=0
		  else
		    # add String flag ON
		    CMT_FLAG=1
		  fi
		  continue
		fi
		fi
		
		# </TEXT> process   #########################################
		# add String END tag                                     ####
		$echo "$LINE" | $grep ^"</TEXT>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		  if [ $CMT_FLAG -eq 1 ] ; then
		    CMT_FLAG=0
		  fi
		  continue
		fi
		
		# </UNIT> process   #########################################
		# insert unit END tag                                    ####
		$echo "$LINE" | $grep ^"</UNIT>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		  if [ $UNIT_FLAG -eq 1 ] ; then
		    UNIT_FLAG=0
		    VAL_PARAM=""
		    VAL_DATA=""
		  fi
		  continue
		fi
		
		### Only uninstall, delete add part ########################
		# delete among DELIMLINE at uninstall                   ####
		# </FILE> process   ########################################
		# one File End                                          ####
		
		$echo "$LINE" | $grep ^"</FILE>" 1> /dev/null 2>> ${ERR_LOG_FILE}
		if [ $? -eq 0 ] ; then
		if [ $ERR_FLAG -eq 0 ] ; then
		  # No error when go
		  if [ $REAL_FLAG -eq 1 ] ; then
		    if [ "$DELIMLINE" != "" ] ; then
		      # init
		      ADDED_FLAG=0                                      # add_flg init
		      $rm -f $FILENAME".change" 1> /dev/null 2>> ${ERR_LOG_FILE}        # delete base_filename.change
		      
		      LASTEST_FLAG=0                                    # add_write_flg(0=insert on A, 1=Last line)
		      
		      if [ "$ORG_PARAM" = "" ] ; then
		        # <ORG> is null -> Last line
		        LASTEST_FLAG=1
		      fi
		      
		      # read true_file
		      for REAL_LINE in `$cat $REALNAME`
		      do
		        # judge SMX separate line -> FLAG O
		        if [ "$REAL_LINE" = "$DELIMLINE" ] ; then
		          if [ $ADDED_FLAG -eq 0 ] ; then
		            ADDED_FLAG=1                                # FLAG OFF -> FLAG ON
		            continue
		          elif [ $ADDED_FLAG -eq 1 ] ; then
		            ADDED_FLAG=0                                # FLAG ON -> FLAG OFF
		            continue
		          fi
		        fi
		        
		        if [ $ADDED_FLAG -eq 0 ] ; then
		          # Else DELIMLINE, when no change line
		          $echo "$REAL_LINE" >> $FILENAME".change"
		        fi
		        
		      done
		      
		      # copy on true_file
		      $cp -f $FILENAME".change" $REALNAME 1> /dev/null 2>> ${ERR_LOG_FILE}
		      if [ $? -ne 0 ] ; then
		        ERR_FLAG=1
		        arize_err "PRM004" "File copy. [$FILENAME.change > $REALNAME]"
		      fi
		      
		    else
		      # Dont read SMX separate when error
		      ERR_FLAG=1
		      arize_err "PRM006" "Delimline. [$DELIMLINE]"
		    fi
		  else
		    # Dont read REALNAME when error
		    ERR_FLAG=1
		    arize_err "PRM005" "File. [$FILENAME]"
		  fi
		  
		  if [ -f $TMP_FILE ] ; then
		    $rm -f $TMP_FILE 2>&1
		  fi
		fi
		
		# insert miss is ERROR_FLAG ON
		if [ $ERR_FLAG -ne 0 ] ; then
		  FLAG=1
		fi
		
		# one File finish
		echo_sp "."
		write_log "Added file. [$FILENAME]"
		
		init
		continue
		fi
	done
	fi
done



IFS=$TMP_IFS

# result
if [ $FLAG -eq 0 ] ; then
  $echo "${END}"
  write_log "${PARAM_SETBACKEND}"
else
  $echo "${OCCURED_ERROR}"
  $echo ""
  msg_err "${PARAM_SETBACK_MISS}" \
          "${PROSECC_QUIT}"
  arize_err "dmy" "${PARAM_SETBACK_MISS}"
fi

exit $FLAG
