#!/bin/sh -e
#
# 2018 Steven Armstrong (steven-cdist at armstrong.cc)
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# 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/>.
#

if [ -f "$__object/parameter/file" ]; then
   file=$(cat "$__object/parameter/file")
else
   file="/$__object_id"
fi

[ -f "$file" ] || exit 0

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

if [ -f "$__object/parameter/regex" ]; then
   needle="regex"
else
   needle="line"
fi

awk -v position="$position" -v needle="$needle" '
function _find(_text, _pattern) {
   if (needle == "regex") {
      return match(_text, _pattern)
   } else {
      return index(_text, _pattern) == 1
   }
}
BEGIN {
   getline anchor < (ENVIRON["__object"] "/parameter/" position)
   getline pattern < (ENVIRON["__object"] "/parameter/" needle)

   found_line = 0
   correct_pos = (position != "after" && position != "before")
}
{
   if (position == "after") {
      if (match($0, anchor)) {
         getline
         if (_find($0, pattern)) {
            found_line++
            correct_pos = 1
            exit 0
         }
      } else if (_find($0, pattern)) {
         found_line++
      }
   } else if (position == "before") {
      if (_find($0, pattern)) {
         found_line++
         getline
         if (match($0, anchor)) {
            correct_pos = 1
            exit 0
         }
      }
   } else {
      if (_find($0, pattern)) {
         found_line++
         exit 0
      }
   }
}
END {
   if (found_line && correct_pos) {
      print "present"
   } else if (found_line) {
      print "wrongposition"
   } else {
      print "absent"
   }
}
' "$file"
