#!/bin/sh
# property type
# Replace
# ---------------------------------------------- #

# 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:tagString, 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_ORG'"/"'$TMP_VAL'"/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
}

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

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

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

# read setting file
load_setup_properties

# Read auth_define.sh (for AUTH server only)
# . ${AUTH_DEF_SH_DIR}

# init
initialize
init

# result init
FLAG=0

# separate argment fileName
separate_arg_file $@

# START
echo_sp "${PARAM_SET}."
write_log "${PARAM_SETSTART}"

# 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
  
  # <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
        # ORG or VALUE is ""
        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"`
          ORG_LINE=`$cat $FILENAME | $grep "${ORG_PARAM}"`
	      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
	  
	  # </FILE> process  #########################################
	  # one File END                                          ####
	  # now insert temp_file and copy on true_file            ####
	  $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
	            # read line is <ORG> when add START
	            # <ORG> is null when add last line
	            if [ $ADDED_FLAG -eq 0 ] ; then
	              # <ORG> is not null -> insert on <ORG>
	              if [ $LASTEST_FLAG -eq 0 -a "$REAL_LINE" = "$ORG_LINE" ] ; then
	                # same -> insert
	                if [ -s $TMP_FILE ] ; then
	                  # Exist add String when insert
	                  # insert String among SMX separates
	                  $echo "$DELIMLINE" >> $FILENAME".change"   # START separate
	                  $cat $TMP_FILE >> $FILENAME".change"       # add String
	                  $echo "$DELIMLINE" >> $FILENAME".change"   # END separate
	                  $echo "$REAL_LINE" >> $FILENAME".change"   # add <ORG>
	                fi
	              else
	                # not same -> no change write
	                $echo "$REAL_LINE" >> $FILENAME".change"
	              fi
	            fi
	          done
	          
	          # add lase line when last insert
	          if [ $LASTEST_FLAG -eq 1 ] ; then
	            if [ -s $TMP_FILE ] ; then
	              # Exist add String when insert
	              # insert String among SMX separates
	              $echo "$DELIMLINE" >> $FILENAME".change"       # START separate
	              $cat $TMP_FILE >> $FILENAME".change"          # add String
	              $echo "$DELIMLINE" >> $FILENAME".change"       # END separate
	            fi
	          fi
	          
	          # 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
	  
	  # No Tag line process   ####################################
	  # add String between <TEXT> and </TEXT>                 ####
	  if [ $CMT_FLAG -eq 1 ] ; then
	       # Get parameter_name
	       TMP_PARAM=`$echo "$LINE" | $sed -e s/'\`'//g -e s/=.*/=/g`
	       # when exist CMD in TMP_PARAM, that cmd excute 
	       CMD_STR=`$echo "$LINE" | $grep '\`' | $sed -e s/'\`'//g -e s/.*=//g`
	       if [ "$CMD_STR" != "" ] ; then
	         # Result of excuted CMD is PARAM_NAME_TAIL
	         $echo "$TMP_PARAM"`eval \`$echo "$CMD_STR"\`` >> $TMP_FILE 2>&1
	       else
	         # when No exist CMD, No change write
	         $echo "$LINE" >> $TMP_FILE 2>&1
	       fi
	  fi
	  
	done
	fi
done


IFS=$TMP_IFS

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

exit $FLAG
