diff --git a/bin/create_config.sh b/bin/create_config.sh index 26bbb9fa0950eca6378f2ae00947819b1f89f651..edbdbd788064f309616ac5ac0a86e9a0650b6204 100755 --- a/bin/create_config.sh +++ b/bin/create_config.sh @@ -7,6 +7,7 @@ # # ---------------------------------------------------------------------- # 2020-12-15 v0.0 <axel.hahn@iml.unibe.ch> +# 2021-05-07 v1.0 <axel.hahn@iml.unibe.ch> multiple replacement files # ====================================================================== # ---------------------------------------------------------------------- @@ -15,12 +16,35 @@ tplfile=$1 outfile=$2 -replacefile=$3 +shift 2 +replacefile=$* test -z "$replacefile" && replacefile="replace*.txt" tmpfile=/tmp/myreplacement.tmp usage="`basename $0` TEMPLATE-FILE OUTFILE [optional: REPLACE-DATA-FILE]" +# ---------------------------------------------------------------------- +# FUNCTIONS +# ---------------------------------------------------------------------- + +# show colored text + line break +function _colmessage(){ + echo -e "\033${1}${2}\033[0m" +} + +# show red error message +function _err(){ + _colmessage "[31m" "$1" +} +# show green message +function _ok(){ + _colmessage "[32m" "$1" +} +# show green message +function _warn(){ + _colmessage "[33m" "$1" +} + # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- @@ -36,20 +60,20 @@ echo # --- parameter checks if [ -z "$tplfile" -o -z "$outfile" ]; then - echo "ERROR: missing params." + _err "ERROR: missing params." echo $usage exit 1 fi if [ ! -f "$tplfile" ]; then - echo "ERROR: template [$tplfile] does not exist." + _err "ERROR: template [$tplfile] does not exist." echo $usage exit 2 fi ls $replacefile >/dev/null if [ $? -ne 0 ]; then - echo "ERROR: replacement data file [$replacefile] does not exist." + _err "ERROR: replacement data file [$replacefile] does not exist." echo $usage exit 2 fi @@ -60,7 +84,7 @@ cat $tplfile > $tmpfile # --- apply replace rules grep "^[a-zA-Z]" $replacefile | grep "=" | while read line do - key=$( echo $line | cut -f 1 -d "=" ) + key=$( echo $line | cut -f 2- -d ":" | cut -f 1 -d "=" ) val=$( echo $line | cut -f 2- -d "=" ) regex="<\%\=\ *\@replace\[\"$key\"\]\ *\%>" @@ -75,7 +99,7 @@ echo echo --- changes: diff "$tplfile" "$tmpfile" if [ $? -eq 0 ]; then - echo "ERROR: template was unchanged. Check placeholders in $tplfile and replacements:" + _err "ERROR: template was unchanged. Check placeholders in $tplfile and replacements:" cat $replacefile exit 3 fi @@ -85,9 +109,22 @@ mv -f "${tmpfile}" "${outfile}" echo echo --- output file: -ls -l "${outfile}" && exit 0 +ls -l "${outfile}" +if [ $? -ne 0 ]; then + _err "ERROR: unable to write target file $outfile." + exit 4 +fi -echo "ERROR: unable to write target file $outfile." -exit 4 +grep '<\%\=\ *\@replace\[\"' ${outfile} >/dev/null +if [ $? -eq 0 ]; then + echo + _warn "WARNING: Maybe you need to verify replacements (can be a false positive)" + grep '<\%\=\ *\@replace\[\"' ${outfile} +else + _ok "OK" +fi + + exit 0 -# ---------------------------------------------------------------------- \ No newline at end of file + +# ----------------------------------------------------------------------