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

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

log info "START"

# shellcheck disable=SC2317
exit_handler() {
  [[ -v retry_counter ]] || retry_counter=0
  if (( retry_counter > 1 )); then
    log notice "success after attempt $retry_counter"
  fi
}

trap exit_handler EXIT

usage() {
  printf '%s\n' "Usage: ${0##*/} WIKI PAGE OUTPUT
Example:
  ${0##*/} 'https://www.kicksecure.com/w' About /tmp/a" >&2
  exit 1
}

if [[ -z "${3-}" || "${1-}" =~ (-h|--help) ]]; then
  usage
fi

wiki_url="$1"
page="$2"
output="$3"

check_vars_exist page output

output_dir="$(dirname -- "${output}")"

temp_page_normalized="$(set_backup_filename_item "$page")"
temp_output_file="$TMPFOLDER/$temp_page_normalized"

log info "wiki_url        : $wiki_url"
log info "page            : $page"
log info "temp_output_file: $temp_output_file"
log info "output          : $output"

mkdir -p -- "$output_dir"
if ! test -d "$output_dir"; then
  die 1 "output_dir '$output_dir' does not exist! Run?: mkdir --parents -- '$output_dir'"
fi

if ! test -w "$output_dir"; then
  die 1 "output_dir '$output_dir' unwritable! Run?: chown --recursive -- '$USER:$USER' '$output_dir'"
fi

for retry_counter in {1..5}; do
  if ! mw-fetch "$wiki_url" "$page" "$temp_output_file"; then
    sleep 5
  fi

  ## 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.
  if ! mw-page-pending-check "$wiki_url" "$page"; then
    log warn "SKIP: page '$page' has PENDING edits."
    exit 0
  fi

  mv --verbose -- "$temp_output_file" "$output"
  log info "SUCCESS: page '$page' | output: '$output'"

  exit 0
done

## Failed even after retry attempts. Therefore exit non-zero.
exit 1
