# Copyright (c) 2012 Tresys Technology LLC, Columbia, Maryland, USA
# #
# # This software was developed by Tresys Technology LLC
# # with U.S. Government sponsorship.
# #
# # This library is free software; you can redistribute it and/or
# # modify it under the terms of the GNU Lesser General Public
# # License as published by the Free Software Foundation; either
# # version 2.1 of the License, or (at your option) any later version.
# #
# # Unless required by applicable law or agreed to in writing, software
# # distributed under the License is distributed on an "AS IS" BASIS,
# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# # See the License for the specific language governing permissions and
# # limitations under the License.
# #

function add_entry {
# Adds an entry or uncomments it if it exists.
#
# arguments:
# add_entry (ENTRY, FILEDIR)
#     ENTRY   - ENTRY to check for and add
#     FILEDIR - File to which the entry should be added
# 
       ENTRY=$1
        FILEDIR=$2

        F=` /bin/echo $FILEDIR|/bin/grep -Po "[^\/]*$" `
        FILEDIR=${FILEDIR%"/$F"}

        if /bin/grep -Pq "^(\s*)(\#*)(\s*)$ENTRY" "$FILEDIR/$F"; then
                ENTRY=` /bin/echo "$ENTRY"|/bin/sed 's!\([]\*\$\/&[]\)!\\\1!g' `
                /bin/sed -i -r -e "s!(\s*)(\#*)(\s*)$ENTRY!\3\4!g" "$FILEDIR/$F"
        else
                /bin/echo "$ENTRY" >> "$FILEDIR/$F"
        fi

        return
}

function add_entry_before {
# Adds an entry before an existing entry
#
# arguments:
# add_entry_before ($1, $2, $3)
#     $1      - Existing entry
#     $2      - Entry to add
#     $3      - File to edit	
	FILE=$3
        /bin/grep -qPzo -- "$2\n$1" $FILE && return
        /bin/sed -i -r -e "/^$(/bin/echo -n $1 | /usr/bin/tr "/" ".")$/d" $FILE
        /bin/sed -i -r -e "/$2/a$1" $FILE
}

function add_entry_after {
# Adds an entry after an existing entry
#
# arguments:
# add_entry_after ($1, $2, $3)
#     $1      - Existing Entry
#     $2      - Entry to add
#     $3      - File to edit

        FILE=$3
	/bin/grep -qPzo -- "$1\n$2" $FILE && return
        /bin/sed -i -r -e "/^$(/bin/echo -n $1 | /usr/bin/tr "/" ".")$/d" $FILE
        /bin/sed -i -r -e "/$1/a$2" $FILE
}

function safe_add_field {
# Searches for a regular expression and replaces anything outside of parentheses
# with the specified replace string.
#       E.G.
#       /bin/cat $FILE
#               # # HELLO WORLD
#       safe_add_field "(HELLO).*" " MARS" $FILE
#       /bin/cat $FILE
#               HELLO MARS
#
# If the string does not exist, the value inside parentheses and the new entry value
# string are combined and appended to the file.
#
#  safe_add_field (SAFE_FIELD, VAL, FILEDIR)
#       SAFE_FIELD - an entry to be modified, with the unchanged contents in parentheses
#       VAL        - a new value to replace anything outside of SAFE_FIELD's parens
#       FILEDIR    - the name of the file to be changed
#

        SAFE_FIELD=$1
        VAL=$2
        FILEDIR=$3

        F=` /bin/echo $FILEDIR|/bin/grep -Po "[^\/]*$" `
        FILEDIR=${FILEDIR%"/$F"}

        if /bin/grep -Pq "^(\s*)(\#*)(\s*)($SAFE_FIELD)" "$FILEDIR/$F"; then
                /bin/sed -i -r -e "s!(\s*)(\#*)(\s*)$SAFE_FIELD!\3\4$VAL!g" "$FILEDIR/$F"
        else
                OUT=` /bin/echo $SAFE_FIELD|/bin/grep -Po "\([^\(|^\)]*\)" `
                OUT=` /bin/echo $OUT | /bin/grep -Po "[^\(|^\)]*"`$VAL
		OUT=` /bin/echo $OUT| /bin/sed -r -e "s!\\\\\s\+! !g" `
		OUT=` /bin/echo $OUT| /bin/sed -r -e "s!\\\\\s\*!!g" `
		OUT=` /bin/echo $OUT| /bin/sed -r -e "s!\\\\\s! !g" `
		OUT=` /bin/echo $OUT| /bin/grep -Po '[^\\\\]*'`
		
                /bin/echo $OUT >> "$FILEDIR/$F"
        fi

        return
}
