#!/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##*/} WIKI PAGE_FILE OUTPUT
Example:
  ${0##*/} 'https://www.kicksecure.com/w' File:Search-engine-clipart.jpg /tmp/About_icon.png" >&2
  exit 1
}

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

WIKI_URL="$1"
page_file="$2"
page_file_local="$3"

check_vars_exist page_file page_file_local

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

log info "Downloading file '${WIKI_INDEX}' '$page_file'..."

if test -d "$page_file_local"; then
  die 1 "page_file_local '$page_file_local' is a folder!"
fi

safe-rm -f -- "$page_file_local"
touch -- "$page_file_local"
test -w "$page_file_local"

## mw-file-to-url gets already encoded URL from MediaWiki API.
mw-file-to-url "$WIKI_URL" "${TMPFOLDER}/wiki-remote-file-url.txt" "$page_file"

url="$(stcat "${TMPFOLDER}/wiki-remote-file-url.txt")"
## url contains encoded URL.
## url example:
## https://www.kicksecure.com/w/images/7/74/MediaWiki-2020-logo-%28black%29.svg

log info "saving file to page_file_local: '$page_file_local'"

curl_run_no_encode=true \
  curl_run \
    "${curl_opts[@]}" \
    --header "Accept-Language: en-GB" \
    --header "Expect:" \
    --output "$page_file_local" \
    "$url"

log info "Fetch file success."
