#!/bin/bash

## Copyright (C) 2025 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -o errexit
set -o nounset
set -o errtrace
set -o pipefail

target_key="${1:-}"
[ -z "${target_key}" ] && exit 2
final_val=""

shopt -s nullglob
for config_file in \
  /etc/sdwdate-gui.d/*.conf \
  /usr/local/etc/sdwdate-gui.d/*.conf \
  ; do

  while read -r line; do
    # shellcheck disable=SC2076
    if [[ "${line}" =~ '.*#' ]]; then
      continue
    fi
    line="$(sed 's/^\s*//; s/\s*$//' <<< "${line}")"
    if [[ "${line}" = "${target_key}="* ]]; then
      final_val="$(cut -d'=' -f2- <<< "${line}")"
    fi
  done < "${config_file}"
done

[ -z "${final_val}" ] && exit 1
echo "${final_val}"
exit 0
