#!/bin/bash

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

## style-ok: no-strict
##
## UTF-8 locale default (no block "export LC_ALL=C"): these tools process
## Unicode wiki content, so the C byte-locale would break nocasematch,
## "[:space:]" trimming and "grep -i" on non-ASCII. Byte-exact operations
## carry an explicit "LC_ALL=C" prefix instead.
set -o errexit
set -o nounset
set -o errtrace
set -o pipefail
shopt -s inherit_errexit
shopt -s shift_verbose

# shellcheck source=../share/mediawiki-shell/common
source /usr/share/mediawiki-shell/common

log info "START"

usage() {
  printf '%s\n' "Usage: ${0##*/} [OPTIONS] WIKI WIKI_TARGET PAGE [OUTPUT]
Options:
  --edit-msg=X     Summary to specify for edit
  --dry-run        Preview only; skip the actual write on the server.
Defaults:
  OUTPUT=todo
Example:
  ${0##*/} 'https://www.kicksecure.com/w' 'https://www.whonix.org/w' About
  ${0##*/} 'https://www.kicksecure.com/w' 'https://www.whonix.org/w' About ${TMPFOLDER}/About" >&2
  exit 1
}

# shellcheck source=../../../helper-scripts/usr/libexec/helper-scripts/parse_opt.sh
source "${HELPER_SCRIPTS_PATH:-}"/usr/libexec/helper-scripts/parse_opt.sh

edit_msg=""

while true; do
  [[ "${1-}" =~ ^- ]] || break
  begin_optparse "${1-}" "${2-}" || break
  true "${opt-}" "${arg-}" "${opt_orig-}"
  case "${opt}" in
    edit-msg)
      get_arg
      edit_msg="${arg}"
      ;;
    dry-run)
      export DRY_RUN="true"
      ;;
    h|help)
      usage
      ;;
    --|"")
      break
      ;;
    *)
      die 2 "Invalid option: '${opt_orig}'"
      ;;
  esac
  shift "${shift_n:-1}"
done

if [[ -z "${3-}" ]]; then
  usage
fi

WIKI_URL="$1"
wiki_url_target="$2"
page_title="$3"
page_title_local="${4-}"
if [ -z "${page_title_local}" ]; then
  log info "default output_file: yes"
  page_title_local="$(set_backup_page_item "${page_title}")"
  output_file="${TMPFOLDER}/${page_title_local}"
  assert_path_within_dir "${TMPFOLDER}" "${output_file}"
else
  log info "default output_file: no"
  output_file="${page_title_local}"
fi

check_vars_exist wiki_url_target page_title

## Defense-in-depth: check page_title for malicious unicode before
## using it for fetch and edit operations.
printf '%s\n' "${page_title}" | unicode-show

# shellcheck source=../share/mediawiki-shell/wiki-config
source /usr/share/mediawiki-shell/wiki-config

log info "WIKI_URL             : ${WIKI_URL}"
log info "WIKI_API             : ${WIKI_API}"
log info "wiki_url_target      : ${wiki_url_target}"
log info "page_title           : ${page_title}"
log info "page_title_local     : ${page_title_local}"
log info "output_file          : ${output_file}"

#mw-login-test "$WIKI_URL"

log info "Fetching page '${WIKI_URL}' '${page_title}' '${output_file}'..."
mw-fetch "${WIKI_URL}" "${page_title}" "${output_file}"
log info "Fetch page success."

## time-of-check to time-of-use TOCTOU
## Check for pending edits only after page was fetched.
## Checking for pending edits before fetching page would leave room for
## making a pending edit after the page has been fetched.
## This ensures that the fetched edit was not pending, i.e. confirmed.
mw-page-pending-check "${WIKI_URL}" "${page_title}"

log info "Editing page... '${wiki_url_target}' '${output_file}' '${page_title}'"

if [ -n "${edit_msg}" ]; then
  mw-edit "${wiki_url_target}" "${output_file}" "${page_title}" "${edit_msg}"
else
  mw-edit "${wiki_url_target}" "${output_file}" "${page_title}"
fi

log info "Edit page success."
