2018-05-17 23:25:35 +00:00
|
|
|
#!/bin/sh -e
|
2012-09-20 09:10:10 +00:00
|
|
|
#
|
2018-05-17 23:25:35 +00:00
|
|
|
# 2018 Steven Armstrong (steven-cdist at armstrong.cc)
|
2012-09-20 09:10:10 +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-05-17 23:25:35 +00:00
|
|
|
if [ -f "$__object/parameter/before" ]; then
|
|
|
|
position="before"
|
|
|
|
elif [ -f "$__object/parameter/after" ]; then
|
|
|
|
position="after"
|
|
|
|
fi
|
2012-09-20 09:10:10 +00:00
|
|
|
|
|
|
|
if [ -f "$__object/parameter/regex" ]; then
|
2018-05-17 23:25:35 +00:00
|
|
|
needle="regex"
|
2012-09-20 09:10:10 +00:00
|
|
|
else
|
2018-05-17 23:25:35 +00:00
|
|
|
needle="line"
|
2012-09-20 09:10:10 +00:00
|
|
|
fi
|
|
|
|
|
2018-05-17 23:25:35 +00:00
|
|
|
if [ -f "$__object/parameter/file" ]; then
|
|
|
|
file="$(cat "$__object/parameter/file")"
|
2012-09-20 09:10:10 +00:00
|
|
|
else
|
2018-05-17 23:25:35 +00:00
|
|
|
file="/$__object_id"
|
2012-09-20 09:10:10 +00:00
|
|
|
fi
|
2018-05-17 23:25:35 +00:00
|
|
|
|
|
|
|
awk -v position="$position" -v needle="$needle" '
|
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 {
|
|
|
|
getline anchor < (ENVIRON["__object"] "/parameter/" position)
|
|
|
|
getline pattern < (ENVIRON["__object"] "/parameter/" needle)
|
|
|
|
state = "absent"
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if (position == "after") {
|
|
|
|
if (match($0, anchor)) {
|
|
|
|
getline
|
2018-05-18 14:57:34 +00:00
|
|
|
if (_find($0, pattern)) {
|
2018-05-17 23:25:35 +00:00
|
|
|
state = "present"
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state = "wrongposition"
|
|
|
|
}
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (position == "before") {
|
2018-05-18 14:57:34 +00:00
|
|
|
if (_find($0, pattern)) {
|
2018-05-17 23:25:35 +00:00
|
|
|
getline
|
|
|
|
if (match($0, anchor)) {
|
|
|
|
state = "present"
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state = "wrongposition"
|
|
|
|
}
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-05-18 14:57:34 +00:00
|
|
|
if (_find($0, pattern)) {
|
2018-05-17 23:25:35 +00:00
|
|
|
state = "present"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END {
|
|
|
|
print state
|
|
|
|
}
|
|
|
|
' "$file"
|