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

usage() {
  printf '%s\n' "Usage: ${0##*/} [OPTIONS] WIKI SCRIPT OUTPUT_DIR
Options:
  --continue-from-page=N     Continue from page index.
Note:
  ARGS are passed to the SCRIPT.
Example:
  ${0##*/} 'https://www.kicksecure.com/w' 'mw-patch-modify-wiki-page' '/tmp/user/1000/mw'" >&2
  exit 1
}

# shellcheck source=/usr/libexec/helper-scripts/parse_opt.sh
source /usr/libexec/helper-scripts/parse_opt.sh

## Allow overwriting using environment variable.
#continue_from_page=""

while true; do
  [[ "${1-}" =~ ^- ]] || break
  begin_optparse "${1:-}" "${2:-}" || break
  true "${opt-}" "${arg-}" "${opt_orig-}"
  case "${opt}" in
    continue-from-page) get_arg; continue_from_page="${arg}";;
    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_script="$2"
output_dir="$3"

check_vars_exist wiki_script output_dir

case "$wiki_script" in
  "mw-wiki-fetch-backup")
    true
    ;;
  "mw-patch-modify-wiki-page")
    true
    ;;
  *)
    log error "Unknown fetcher '$wiki_script'!"
    exit 1
    ;;
esac

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

log info "TMPFOLDER : $TMPFOLDER"
log info "WIKI_URL  : $WIKI_URL"

## Not required for public wiki.
#mw-login-test "$WIKI_URL"

allpages_file="${TMPFOLDER}/allpages.txt"

safe-rm -f -- "$allpages_file"
mw-all-pages "$WIKI_URL" allpages "$allpages_file"

test -r "$allpages_file"
unicode-show "$allpages_file"

counter_total="$(awk 'END {print NR}' "$allpages_file")"

counter_currently=0
counter_chunk=0
chunk_max_size=10
start_from_here="not-yet"

while IFS=$'\n' read -r item_from_all_pages; do
  (( counter_currently++ )) || true
  backup_page_item="$(set_backup_page_item "$item_from_all_pages")"
  backup_filename_item="$(set_backup_filename_item "$backup_page_item")"

  (( counter_chunk++ )) || true

  ## TODO: continue from specific page?
  #continue_from_page="Network_Time_Synchronization"

  if [ -n "${continue_from_page-}" ]; then
    if [ "${item_from_all_pages,,}" = "${continue_from_page,,}" ]; then
      start_from_here=true
    fi
  else
    start_from_here=true
  fi

  if [ ! "$start_from_here" = "true" ]; then
    log info "skip: $counter_currently / $counter_total | counter_chunk: $counter_chunk | $item_from_all_pages | $backup_filename_item"
    continue
  fi
  log info "act: $counter_currently / $counter_total | counter_chunk: $counter_chunk | $item_from_all_pages | $backup_filename_item"

  if [ "$item_from_all_pages" = "Changelog" ]; then
    continue
  fi

  TMPFOLDER_SEPARATE="$TMPFOLDER/separate/$counter_currently"
  mkdir --parents -- "$TMPFOLDER_SEPARATE"

  log_run \
    info \
    env \
    counter_currently="$counter_currently" \
    counter_chunk="$counter_chunk" \
    TMPFOLDER="$TMPFOLDER_SEPARATE" \
    mw-retry-wrapper "$wiki_script" "$WIKI_URL" "$item_from_all_pages" "$output_dir/$backup_filename_item"

  if [ "$counter_chunk" -ge "$chunk_max_size" ] || [ "$counter_currently" -ge "$counter_total" ]; then
    counter_chunk=0
  fi

done <"$allpages_file"
