#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2014 Daniel Heule     (hda at sfs.biz)
#
# 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/>.
#

key="$(cat "$__object/parameter/key" 2>/dev/null \
   || echo "$__object_id")"
state="$(cat "$__object/parameter/state")"

file="$(cat "$__object/parameter/file")"

if [ ! -f "$file" ]; then
    echo "nosuchfile"
    exit
fi

delimiter="$(cat "$__object/parameter/delimiter")"
value="$(cat "$__object/parameter/value" 2>/dev/null \
   || echo "__CDIST_NOTSET__")"
if [ -f "$__object/parameter/exact_delimiter" ]; then
    exact_delimiter=1
else
    exact_delimiter=0
fi
export key state delimiter value exact_delimiter

awk_bin=$(PATH=$(getconf PATH 2>/dev/null) && command -v awk || echo awk)

"${awk_bin}" -f - "$file" <<"AWK_EOF"
BEGIN {
    state=ENVIRON["state"]
    key=ENVIRON["key"]
    delimiter=ENVIRON["delimiter"]
    value=ENVIRON["value"]
    exact_delimiter=ENVIRON["exact_delimiter"]
    found=0
}
# enter the main loop
{
    i = index($0,key)
    if(i == 1) {
        delval = substr($0,length(key)+1)
        delpos = index(delval,delimiter)
        if(delpos == 0) {
            # in this case, the delimiter was not found
            next
        }
        if(delpos > 1) {
            spaces = substr(delval,1,delpos-1)
            sub(/[ \t]*/,"",spaces)
            if( length(spaces) > 0 ) {
                # if there are not only spaces between key and delimiter,
                # continue since we we are on the wrong line
                next
            }
            if( exact_delimiter == 1) {
                # we have key and delimiter, but since additional spaces are not alowed
                # return wrongformat
                found=1
                print "wrongformat"
                exit
            }
        }
        found=1
        if(state == "absent") {
            # on state absent, only the ocurance is relevant, so exit here
            print "present"
            exit
        }
        linevalue=substr(delval,delpos + length(delimiter))
        if(exact_delimiter == 0){
            #ok, now strip tabs and whitespaces at the beginning of the value
            sub(/[ \t]*/,"",linevalue)
        }
        # Key with separator found
        if(linevalue == value) {
            # exact match found, so state is present
            print "present"
        }
        else {
            print "wrongvalue"
        }
        exit
    }
}
END {
    if(found == 0)
        print "absent"
}
AWK_EOF
