Revert package name changes
[dmaap/messagerouter/messageservice.git] / src / main / swm / common / utils / findreplace.sh
1 #!/bin/sh
2 #*******************************************************************************
3 #  ============LICENSE_START=======================================================
4 #  org.onap.dmaap
5 #  ================================================================================
6 #  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
7 #  ================================================================================
8 #  Licensed under the Apache License, Version 2.0 (the "License");
9 #  you may not use this file except in compliance with the License.
10 #  You may obtain a copy of the License at
11 #        http://www.apache.org/licenses/LICENSE-2.0
12 #  
13 #  Unless required by applicable law or agreed to in writing, software
14 #  distributed under the License is distributed on an "AS IS" BASIS,
15 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #  See the License for the specific language governing permissions and
17 #  limitations under the License.
18 #  ============LICENSE_END=========================================================
19 #
20 #  ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 #  
22 #*******************************************************************************
23 # Copyright 2011 AT&T Intellectual Properties
24 ##############################################################################
25 # findreplace.sh <template> <destination>
26 #
27 #     This script searches a provided file for templatized variable names
28 #     in the format __varname__ and, if found in the current environment
29 #     replaces those.  Once complete, it will move the final copy of the file
30 #     to <destination>.
31 #
32 ##############################################################################
33 TEMPLATE=${1:?"Template file path required"}
34 DESTINATION=${2:?"Destination file path required"}
35
36 if [ ! -f "${TEMPLATE}" ]; then
37     echo "ERROR: Specified template file does not exist: ${TEMPLATE}"
38     exit 100
39 fi
40
41 DIRECTORY=`dirname ${DESTINATION}`
42 if [ ! -d "${DIRECTORY}" ]; then
43     echo "ERROR: Destination directory does not exist: ${DIRECTORY}"
44     exit 200
45 fi
46
47 SED_SCR=/tmp/sed.$$
48 echo "{" > ${SED_SCR}
49
50 # create a sed script for replacing variables from current environment
51 for  i in `env | awk -F= '{ print $1}'`; do
52     if [ "$i" = "IFS" ] ; then
53        continue;
54     fi
55
56     VALUE=`eval echo '$'${i}` || {
57         echo 'WARNING: Unable to format '${i}' for sed replacement'
58         continue;
59     }
60     
61     for x in '@' '^' '&' '?' '#' '~' '%' '|' '+' '/'; do
62         echo ${VALUE} | grep "$x" 2>/dev/null 1>/dev/null
63         if [ $? != 0 ]; then
64             CCHAR="$x"
65             break
66         fi
67     done
68     
69     if [ -z "${CCHAR}" ]; then
70         echo "WARNING: Unable to find a suitable sed replacement character for ${VALUE}, will ignore setting ${KEY} in templates"
71         continue;
72     fi
73     
74     echo "      s${CCHAR}__${i}__${CCHAR}${VALUE}${CCHAR}g" >> ${SED_SCR}
75 done
76
77 sed -e 's/\\\@/\\\\@/g' ${SED_SCR} > ${SED_SCR}.1 || exit 300
78
79 if [ -f ${DESTINATION} ]; then
80         TIMESTAMP=`date +%Y%m%d%H%M%S`
81         o_dir=`dirname ${DESTINATION}`
82         o_file=`basename ${DESTINATION}`
83     mv ${DESTINATION} ${o_dir}/bu.${o_file}.${TIMESTAMP}
84 fi
85
86 mv -f ${SED_SCR}.1 ${SED_SCR} || exit 400
87
88 echo "}" >> ${SED_SCR} || exit 500
89
90 sed -f ${SED_SCR} ${TEMPLATE} > ${DESTINATION} || exit 600
91
92 rm -f $SED_SCR
93
94 exit 0