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

usage() {
  printf '%s\n' "Usage: ${0##*/} WIKI INPUT_DIR
Example:
  ${0##*/} 'https://www.kicksecure.com/w'
  ${0##*/} 'https://www.kicksecure.com/w' ~/mediawiki-shell/kicksecure-wiki-backup" >&2
  exit 1
}

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

wiki_url="$1"
input_dir="$2"

allpages_file="${TMPFOLDER}/allpages.txt"
safe-rm -f -- "$allpages_file"

log info "TMPFOLDER : $TMPFOLDER"
log info "input_dir : $input_dir"
log info "wiki      : $wiki_url"

mkdir -p -- "$input_dir"

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

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

mw-login-test "$wiki_url"
## TODO: unfinished script
die 1 "UNFINISHED SCRIPT!"

## Remove trailing spaces.
wiki_page_content=""
# shellcheck disable=SC2001,SC2317
wiki_page_content="$(sed 's/[[:space:]]*$//' <<<"$wiki_page_content")"
