#!/usr/bin/env bash # Standard include for Prole etc scripts. # - Loads values from PROLE_CONF/prole.cfg (INI-style) # - Does not override already-set env vars if [[ "${_PROLE_CFG_LOADED:-}" == "1" ]]; then return 0 2>/dev/null || true fi _PROLE_CFG_LOADED=1 _prole_cfg_script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) _prole_cfg_home_guess=$(cd "$_prole_cfg_script_dir/.." && pwd) _prole_trim() { local s="$1" s="${s#"${s%%[![:space:]]*}"}" s="${s%"${s##*[![:space:]]}"}" printf '%s' "$s" } _PROLE_CFG_SET_VARS="|" _prole_cfg_set_default() { local key="$1" value="$2" token="|$key|" if [[ -z "${!key:-}" || "$_PROLE_CFG_SET_VARS" == *"$token"* ]]; then printf -v "$key" '%s' "$value" export "$key" if [[ "$_PROLE_CFG_SET_VARS" != *"$token"* ]]; then _PROLE_CFG_SET_VARS="${_PROLE_CFG_SET_VARS}${key}|" fi fi } _prole_read_cfg() { local cfg="$1" line key value while IFS= read -r line || [[ -n "$line" ]]; do line="$(_prole_trim "$line")" [[ -z "$line" ]] && continue case "$line" in \#*|\;*|\[*\]) continue ;; esac if [[ "$line" == *"="* ]]; then key="$(_prole_trim "${line%%=*}")" value="$(_prole_trim "${line#*=}")" [[ -z "$key" ]] && continue _prole_cfg_set_default "$key" "$value" fi done <"$cfg" } _prole_cfg_file="" if [[ -n "${PROLE_CONF:-}" && -f "$PROLE_CONF/prole.cfg" ]]; then _prole_cfg_file="$PROLE_CONF/prole.cfg" elif [[ -n "${PROLE_HOME:-}" && -f "$PROLE_HOME/conf/prole.cfg" ]]; then _prole_cfg_file="$PROLE_HOME/conf/prole.cfg" elif [[ -f "$_prole_cfg_home_guess/conf/prole.cfg" ]]; then _prole_cfg_file="$_prole_cfg_home_guess/conf/prole.cfg" fi if [[ -n "$_prole_cfg_file" ]]; then if [[ -z "${PROLE_CONF:-}" ]]; then PROLE_CONF=$(cd "$(dirname "$_prole_cfg_file")" && pwd) export PROLE_CONF fi _prole_read_cfg "$_prole_cfg_file" fi if [[ -z "${PROLE_HOME:-}" && -d "$_prole_cfg_home_guess" ]]; then PROLE_HOME="$_prole_cfg_home_guess" export PROLE_HOME fi unset _prole_cfg_script_dir unset _prole_cfg_home_guess unset _prole_cfg_file unset _PROLE_CFG_SET_VARS