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

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

WIKI_URL="$1"
output_file="$2"
origin="$3"

check_vars_exist output_file origin

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

log info "Fetching imageinfo '${WIKI_API}' '$origin'..."

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

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

## https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&iiprop=url&titles=File:Limbo%20Royal%20Blood.jpg
api_link="${WIKI_API}?action=query&format=json&prop=imageinfo&iiprop=url&titles=$origin"

## MediaWiki API return json, which contains an already encoded URL.
curl_run \
  "${curl_opts[@]}" \
  --cookie "$cookie_jar" \
  --cookie-jar "$cookie_jar" \
  --header "Accept-Language: en-GB" \
  --header "Connection: keep-alive" \
  --compressed \
  --output "${TMPFOLDER}/wiki-remote-file-name-fetch-to-file.json" \
  --request "GET" \
  "$api_link"

log info "Network for imageinfo ok."

query_result=$(stcat "${TMPFOLDER}/wiki-remote-file-name-fetch-to-file.json")

## example query_result in case of error:
## {"error":{"code":"internal_api_error_DBConnectionError","info":"[98c54e341525482101c71b32] Caught exception of type Wikimedia\\Rdbms\\DBConnectionError","errorclass":"Wikimedia\\Rdbms\\DBConnectionError"}}

log info "Saving URL in file output_file: '$output_file'"

url="$(stecho "$query_result" | jq -r ".query.pages[].imageinfo[].url")"
## url contains encoded URL.
## url example:
## https://www.kicksecure.com/w/images/7/74/MediaWiki-2020-logo-%28black%29.svg

overwrite "$output_file" "$url"

test -r "$output_file"

log info "Fetch imageinfo success."
