##################################################################################
# Bash library for reading configuration file                                    #
#                                                                                #
# Example use:                                                                   #
#                                                                                #
#   #!/usr/bin/env bash                                                          #
#   source config.shlib; # load the config library functions                     #
#   echo "$(config_get myvar)"; # will be found in user-cfg                      #
#   printf -- "%s\n" "$(config_get myvar $config_file_name)"; # safer way of     #
#                                                             # echoing!         #
#   myvar="$(config_get myvar)"; # how to just read a value without echoing      #
#   echo "$(config_get othervar)"; # will fall back to defaults                  #
#   echo "$(config_get bleh)"; # "__UNDEFINED__" since it isn't set anywhere     #
#                                                                                #
# (C)2021 M.T. Konstapel https://meezenest.nl/mees                               #
#                                                                                # 
# This file is part of PE1RXF-APRS-server.                                       #
#                                                                                #
#    PE1RXF-APRS-server 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.                                         #
#                                                                                #
#    PE1RXF-APRS-server 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 PE1RXF-APRS-server  If not, see <https://www.gnu.org/licenses/>. #
#                                                                                #
##################################################################################

config_read_file() {
    (grep -E "^${2}=" -m 1 "${1}" 2>/dev/null || echo "VAR=__UNDEFINED__") | head -n 1 | cut -d '=' -f 2-;
}

config_get() {
    val="$(config_read_file "${2}" "${1}")";
    if [ "${val}" = "__UNDEFINED__" ]; then
        val="$(config_read_file pe1rxf-aprs-server.cfg.defaults "${1}")";
    fi
    printf -- "%s" "${val}";
}
