cdist/cdist/conf/type/__ini_value/files/gen-awk.sh

66 lines
1.5 KiB
Bash
Executable File

#!/bin/sh -e
# __ini_value/files/gen-awk.sh
# Generates the awk script that will modify the line
#
# Arguments:
# 1: the file to modify
# 2: the should_state
strip_comments() {
grep -v '^[[:space:]]*\($\|#\)'
}
file="$1"
state="$2"
# Generate the basic awk struct
cat <<SHELL
tmpfile="\$(mktemp '${file}.cdist.XXXXXXXX')"
if [ -f '$file' ]; then
cp -p '$file' "\$tmpfile"
fi
awk -f - '$file' > "\$tmpfile" <<'AWK'
SHELL
# generate the awk script and strip unnecessary things
{
# basic functions which everyone needs
cat "$__type/files/common.awk"
# generate the script and strip
awk -v state="$state" '
function parse(line) {
if(match(line, /^[ \t]*# %code_print%$/) > 0) {
if(state == "present")
print "v_print()"
else if(state == "commented")
print "v_print_commented()"
else
print "print \"script compile error! cdist state " state " unkown!\" > /dev/stderr"
}
else print line
}
{
if(match($0, /^[ \t]*# %codeblock_([^%]+)%$/) > 0) {
split($2, result, "_"); type = substr(result[2], 1, length(result[2]) - 1)
file = (ENVIRON["__type"] "/files/parts/" state "/" type ".awk")
while((getline line < file) > 0)
parse(line)
close(file)
}
else print
}' "$__type/files/base.awk"
} | strip_comments
# end of here-doc
cat <<SHELL
AWK
mv -f "\$tmpfile" '$file'
SHELL