#!/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

error_message_if_missing_wiki_url_variable() {
  die 1 "environment variable WIKI_URL missing! Run for example:
  WIKI_URL=https://www.whonix.org/w ${0##*/}"
}

require_auth_data() {
  if [ -z "${WIKI_API_USER_NAME-}" ]; then
    die 1 "User name not supplied!"
  fi
  if [ -z "${WIKI_API_USER_PASS-}" ]; then
    die 1 "Password not supplied!"
  fi
}

source_credentials() {
  local f

  # The file '~/.credentials' is for backwards compatibility and should be
  # removed on future releases.
  for f in /usr/share/mediawiki-shell/credentials \
    ~/.mediawikishell_credentials \
    ~/.credentials; do
    test -f "$f" || continue
    log info "Sourcing credentials file: $f"
    # shellcheck source=credentials disable=SC1090,SC1091
    source -- "$f"
  done
}

[[ -v WIKI_URL ]] || error_message_if_missing_wiki_url_variable
case "${WIKI_URL}" in
  kicksecure) WIKI_URL="https://www.kicksecure.com/w";;
  whonix) WIKI_URL="https://www.whonix.org/w";;
esac

source_credentials

[[ -v WIKI_API ]] || WIKI_API="$WIKI_URL/api.php"
[[ -v WIKI_INDEX ]] || WIKI_INDEX="$WIKI_URL/index.php"
