2017-06-28 11:19:32 +00:00
|
|
|
#!/bin/sh -e
|
2012-09-20 11:20:30 +00:00
|
|
|
#
|
2018-05-17 23:25:35 +00:00
|
|
|
# 2018 Steven Armstrong (steven-cdist at armstrong.cc)
|
2012-09-20 11:20:30 +00:00
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2018-10-01 20:13:24 +00:00
|
|
|
if [ -f "$__object/parameter/before" ] && [ -f "$__object/parameter/after" ]; then
|
2018-05-17 23:25:35 +00:00
|
|
|
echo "Use either --before OR --after but not both." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2012-09-20 11:20:30 +00:00
|
|
|
|
2018-05-17 23:25:35 +00:00
|
|
|
state_should="$(cat "$__object/parameter/state")"
|
2012-09-20 11:20:30 +00:00
|
|
|
state_is="$(cat "$__object/explorer/state")"
|
|
|
|
|
2018-05-17 23:25:35 +00:00
|
|
|
if [ "$state_should" = "$state_is" ]; then
|
|
|
|
# nothing to do
|
|
|
|
exit 0
|
|
|
|
fi
|
2014-01-07 21:30:59 +00:00
|
|
|
|
2018-05-17 23:25:35 +00:00
|
|
|
if [ -f "$__object/parameter/before" ]; then
|
|
|
|
position="before"
|
|
|
|
elif [ -f "$__object/parameter/after" ]; then
|
|
|
|
position="after"
|
|
|
|
else
|
|
|
|
# By default we append to the end of the file.
|
|
|
|
position="end"
|
|
|
|
fi
|
2014-01-07 21:30:59 +00:00
|
|
|
|
2018-05-17 23:25:35 +00:00
|
|
|
if [ -f "$__object/parameter/regex" ]; then
|
|
|
|
needle="regex"
|
|
|
|
else
|
|
|
|
needle="line"
|
|
|
|
fi
|
2012-09-20 11:20:30 +00:00
|
|
|
|
2018-05-17 23:25:35 +00:00
|
|
|
if [ -f "$__object/parameter/file" ]; then
|
|
|
|
file="$(cat "$__object/parameter/file")"
|
|
|
|
else
|
|
|
|
file="/$__object_id"
|
|
|
|
fi
|
2012-09-20 15:35:14 +00:00
|
|
|
|
2018-05-17 23:25:35 +00:00
|
|
|
add=0
|
|
|
|
remove=0
|
|
|
|
case "$state_should" in
|
|
|
|
present)
|
|
|
|
if [ "$state_is" = "wrongposition" ]; then
|
|
|
|
echo updated >> "$__messages_out"
|
|
|
|
remove=1
|
|
|
|
else
|
|
|
|
echo added >> "$__messages_out"
|
|
|
|
fi
|
|
|
|
add=1
|
|
|
|
;;
|
|
|
|
absent)
|
|
|
|
echo removed >> "$__messages_out"
|
|
|
|
remove=1
|
|
|
|
;;
|
|
|
|
esac
|
2013-09-26 14:26:01 +00:00
|
|
|
|
2018-05-17 23:25:35 +00:00
|
|
|
cat << DONE
|
2014-02-21 23:51:26 +00:00
|
|
|
tmpfile=\$(mktemp ${file}.cdist.XXXXXXXXXX)
|
|
|
|
# preserve ownership and permissions of existing file
|
|
|
|
if [ -f "$file" ]; then
|
|
|
|
cp -p "$file" "\$tmpfile"
|
|
|
|
fi
|
2018-05-17 23:25:35 +00:00
|
|
|
|
|
|
|
awk -v position="$position" -v needle="$needle" -v remove=$remove -v add=$add '
|
2018-05-18 14:57:34 +00:00
|
|
|
function _find(_text, _pattern) {
|
|
|
|
if (needle == "regex") {
|
|
|
|
return match(_text, _pattern)
|
|
|
|
} else {
|
|
|
|
return index(_text, _pattern)
|
|
|
|
}
|
|
|
|
}
|
2018-05-17 23:25:35 +00:00
|
|
|
BEGIN {
|
|
|
|
line_file = ENVIRON["__object"] "/parameter/line"
|
|
|
|
getline line < line_file
|
|
|
|
# Need to close line file as it may be re-read as pattern below.
|
|
|
|
close(line_file)
|
|
|
|
getline pattern < (ENVIRON["__object"] "/parameter/" needle)
|
|
|
|
getline anchor < (ENVIRON["__object"] "/parameter/" position)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if (remove) {
|
2018-05-18 14:57:34 +00:00
|
|
|
if (_find(\$0, pattern)) {
|
2018-05-17 23:25:35 +00:00
|
|
|
# skip over this line -> remove it
|
|
|
|
next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (add) {
|
|
|
|
if (anchor && match(\$0, anchor)) {
|
|
|
|
if (position == "before") {
|
|
|
|
print line
|
|
|
|
print
|
|
|
|
} else if (position == "after") {
|
|
|
|
print
|
|
|
|
print line
|
|
|
|
}
|
|
|
|
next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print
|
|
|
|
}
|
|
|
|
END {
|
|
|
|
if (add && position == "end") {
|
|
|
|
print line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
' "$file" > "\$tmpfile"
|
2014-02-21 23:51:26 +00:00
|
|
|
mv -f "\$tmpfile" "$file"
|
2018-05-17 23:25:35 +00:00
|
|
|
DONE
|
2019-03-28 21:31:38 +00:00
|
|
|
|
|
|
|
if [ -f "$__object/parameter/onchange" ]; then
|
|
|
|
cat "$__object/parameter/onchange"
|
|
|
|
fi
|