#!/usr/bin/env bash

#
# ## read\_default()
#
# Reads default values from an extension's config/defaults file.
#
# ### Input Parameters
#
# First parameter is the defaults file key to read (key=value).
# Second parameter is the variable name to store the retrieved value in.
# Remaining parameters are parsed out as token, value and prefix
#   into|as <variable name>
#   prefix <name>
#   <variable> # If no specifier.
#
# ### Stream Outputs
#
# None.
#
# ### Environmental effects
#
# A variable will be set to the value, if the value is nonempty. If no variable
# name is specified the variable will be assigned the same name as the key.
#
# ### Return Codes
#
# 0 for success.
#
# ### Failure Scenarios
#
# Fails if no arguments are passed in, at least need to specify a key.
#
# ### Usage Examples
#
#     user$ read_default "version" prefix "package" # extension is nginx for example
#     user$ echo $package_version
#     1.0.0
#
# ### Notes
#
# read_default respects a tiered default file scheme,
#
#   "/etc/bdsm/${extension}/defaults"
#   "$HOME/.bdsm/${extension}/defaults"
#   "${extension_config_path}/defaults"
#
#
#
read_default()
{
  local key variable token value prefix _file _temp

  key="${1:-}"

  if variable_is_nonempty key
  then
    shift

    while (( $# > 0 ))
    do
      token="$1"
      shift
      case "$token" in
        into|as)
          variable="$1"
          shift
          ;;
        prefix)
          prefix="$1"
          shift
          ;;
        *)
          variable="$token"
          break
        ;;
      esac
    done

    if variable_is_empty variable
    then
      if variable_is_nonempty prefix
      then
        true "${variable:="${prefix:-}_$key"}"
      else
        true "${variable:="$key"}"
      fi
    fi

    # Run search in the same process to initialize cache variables
    settings_search ${extension} defaults > /dev/null

    for _file in  $( settings_search ${extension} defaults )
    do
      if file_is_nonempty "${_file}"
      then
        _temp="$(hash_file "${_file}" "${key}")"

        if [[ -n "${_temp}" ]]
        then
          value="${_temp}"
        fi
      fi
    done

    eval "${variable}=\"${value}\""
  else
    fail "$extension $action read_default() no arguments passed! "
  fi
}

settings_search()
{
  trace_filter settings_search

  local extension=$1 setting=$2 path paths

  # Standard paths
  paths=( "/etc/bdsm/${extension}" "$HOME/.bdsm/${extension}" )

  # Extension path
  if extension_path "${extension}"
  then
    paths+=( "${extension_path}/config" )
  fi

  # Additional paths
  for path in "${settings_search_path_append[@]}"
  do
    settings_search_path_update "${path}"
    paths+=( "${path}/${extension}" )
  done

  # Print existing paths
  for path in "${paths[@]}"
  do
    if path_exists "${path}" \
    && file_is_nonempty "${path}/${setting}"
    then
      printf "${path}/${setting}\n"
    fi
  done
}

settings_search_path_update()
{
  local path=$1
  if ! string_contains "${settings_search_path_cache[*]}" "${path}"
  then
    if file_exists "${path}/.uri"
    then
      fetch_uri "$(cat "${path}/.uri")" "${path}" >&2
    fi
    settings_search_path_cache+=( "${path}" )
  fi
}
