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

## Does not unduplicate. That should be done with a different script.

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

usage() {
  printf '%s\n' "Usage: ${0##*/} INPUT_DIR
Example:
  ${0##*/} ~/sourcesown/wiki-backup/">&2
  exit 1
}

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

not_as_root

mkdir -p -- "$TMPFOLDER/wiki-links"

test -d "${input_dir}kicksecure-wiki-backup"
test -d "${input_dir}whonix-wiki-backup"

for dir in "${input_dir}kicksecure-wiki-backup" "${input_dir}whonix-wiki-backup"; do
  git -C "${dir}" config pull.ff only
  git -C "${dir}" pull
done

safe-rm -f -- "$TMPFOLDER/wiki-links/all-links.txt"

log info "Try: tail -f -- $TMPFOLDER/wiki-links/all-links.txt"

mw-folder-to-weblinks https://www.kicksecure.com/wiki "${input_dir}kicksecure-wiki-backup" | tee -a -- "$TMPFOLDER/wiki-links/all-links.txt"
mw-folder-to-weblinks https://www.whonix.org/wiki "${input_dir}whonix-wiki-backup" | tee -a -- "$TMPFOLDER/wiki-links/all-links.txt"

grep --invert-match -- "## " "$TMPFOLDER/wiki-links/all-links.txt" | tee -- "$TMPFOLDER/wiki-links/links-without-comments.txt" >/dev/null
sort --unique -- "$TMPFOLDER/wiki-links/links-without-comments.txt" | tee -- "$TMPFOLDER/wiki-links/links-sorted.txt" >/dev/null

log info "Try: cat -- $TMPFOLDER/wiki-links/links-sorted.txt"
