#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-FileCopyrightText: 2016 Matt Churchyard <churchers@gmail.com>

# load a configuration file
# this reads the specfied file into the global VM_CONFIG variable.
# we have very basic parsing that uses # for comments and requires
# all variables to be at the beginning of the line in lowercase.
# Note also that a # within double quotes will still be treated
# as the start of a comment.
#
# @param string _file full path of the file to read
# @modifies VM_CONFIG
#
config::load(){
    local _file="$1"

    # read config file
    # we kick out any lines that don't start with a letter,
    # scrap anything after a # character, and remove double-quotes
    VM_CONFIG=$(grep '^[a-z]' "${_file}" 2>/dev/null | awk -F# '{print $1}' | sed -e 's@ *$@@' | tr -d '"')
}

# get a configuration value from the current config file
#
# @param string _var the variable to put value into
# @param string _key the name of the config key to retrieve
# @param optional string _def default value to return if setting not found
# @return true if setting found
#
config::get(){
    local _c_var="$1"
    local _c_key="$2"
    local _c_def="$3"
    local _c_line
    local IFS=$'\n'

    for _c_line in ${VM_CONFIG}; do
        if [ "${_c_key}" = "${_c_line%%=*}" ]; then
            setvar "${_c_var}" "${_c_line#*=}"
            return 0
        fi
    done

    # not found
    setvar "${_c_var}" "${_c_def}"
    return 1
}

# simple wrapper to check a config setting to see if it's
# set to yes/no true/false etc
#
# @param string _key the config key to check
# @param string _def default value if config key doesn't exist
# @return true(0) if set to anything other than no/false/off/0
#
config::yesno(){
    local _key="$1"
    local _def="$2"
    local _value

    config::get "_value" "${_key}" "${_def}"
    util::yesno "${_value}"
}

# set a value in guest configuration file
# we check for newline at the end as sysrc won't add it
# and that will mess up the new key and the existing one
# from the end of the file
#
# @param string _name guest name
# @param string _key config key to set
# @param string _value value
# @param int _skip_newline_check skip the check for newline
# @return true if sysrc successful
#
config::set(){
    local _name="$1"
    local _key="$2"
    local _value="$3"
    local _skip_newline_check="$4"
    local _newline

    if [ -z "${_skip_newline_check}" ]; then
        _newline=$(tail -1 "${VM_DS_PATH}/${_name}/${_name}.conf" | wc -l | tr -d " ")
        [ "${_newline}" -eq "0" ] && echo "" >> "${VM_DS_PATH}/${_name}/${_name}.conf"
    fi

    sysrc -inqf "${VM_DS_PATH}/${_name}/${_name}.conf" "${_key}=${_value}" >/dev/null 2>&1
}

# remove a value from guest config
config::remove(){
    local _name="$1"
    local _key="$2"

    sysrc -inxqf "${VM_DS_PATH}/${_name}/${_name}.conf" "${_key}" >/dev/null 2>&1
}

# load core configuration file
#
# @modifies VM_CORE_CONFIG VM_CONFIG_USER
#
config::core::load(){
    VM_CONFIG_USER="console;compress;decompress;suspend;"

    # check config file exists
    # this is mainly for upgrades to make sure switch/datastore config are migrated
    # DEPRECATED 1.3, remove after
    if [ ! -e "${vm_dir}/.config/system.conf" ]; then
        cat "${vm_dir}/.config/switch" > "${vm_dir}/.config/system.conf" 2>/dev/null
        cat "${vm_dir}/.config/datastore" >> "${vm_dir}/.config/system.conf" 2>/dev/null
    fi

    VM_CORE_CONFIG=$(grep '^[a-z]' "${vm_dir}/.config/system.conf" 2>/dev/null | awk -F# '{print $1}' | sed -e 's@ *$@@' | tr -d '"')
}

# get a value from core config
#
# @param string _c_var variable name to put value into
# @param string _c_key config key to look for
# @param string _c_def default value if not value
# @return 0 if found
#
config::core::get(){
    local _c_var="$1"
    local _c_key="$2"
    local _c_def="$3"
    local _c_line
    local IFS=$'\n'

    for _c_line in ${VM_CORE_CONFIG}; do
        if [ "${_c_key}" = "${_c_line%%=*}" ]; then
            setvar "${_c_var}" "${_c_line#*=}"
            return 0
        fi
    done

    # not found
    setvar "${_c_var}" "${_c_def}"
    return 1
}

# add a value to core configuration
#
# @param string _var variable to set
# @param string _value new value
# @param string _append non-empty to append to existing value
#
config::core::set(){
    local _var="$1"
    local _value="$2"
    local _append="$3"

    if [ -n "${_append}" ]; then
        sysrc -inqf "${vm_dir}/.config/system.conf" "${_var}"+="${_value}" >/dev/null 2>&1
    else
        sysrc -inqf "${vm_dir}/.config/system.conf" "${_var}"="${_value}" >/dev/null 2>&1
    fi
}

# remove a value from core configuration
#
# @param string _var variable to remove
# @param string _value if non-empty we will try to remove just this value from setting
# @return non-zero on error
#
config::core::remove(){
    local _var="$1"
    local _value="$2"

    if [ -n "${_value}" ]; then
        sysrc -inqf "${vm_dir}/.config/system.conf" "${_var}"-="${_value}" >/dev/null 2>&1
    else
        sysrc -inxqf "${vm_dir}/.config/system.conf" ${_var} >/dev/null 2>&1
    fi
}
