#!/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##*/} LINK
Examples:
  ${0##*/} 'http://en.wikipedia.org/wiki/Atomic_clock'
  ${0##*/} 'https://en.wikipedia.org/wiki/Atomic_clock'
  ${0##*/} '[https://en.wikipedia.org/wiki/Atomic_clock'" >&2
  exit 1
}

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

## NOTE: does not support spaces?
## NOTE: case in-sensitive. No need to capitalization.
grep_skip_fixed_string_list_to_exclude="\
<pre>
</pre>
<code>
</code>
{{Code2|
<nowiki>
</nowiki>
image:
file:
|image=
archive.org
{{stable
{{project
tor+https
tor+http
https://www.gnu.org/licenses
load.php
?useformat=mobile
?action=purge
https://www.whonix.org
https://admin.whonix.org
http://mirror.whonix.de
https://www.kicksecure.com
https://admin.kicksecure.com
https://www.debian-administration.org/users/dkg/weblog/64
https://grsecurity.net/paxctld/paxctld_1.2.1-1_amd64
(http://pi3.com.pl)
https://d2cly7j4zqgua7.cloudfront.net
gplv3
http_proxy=
https_proxy=
ALL_PROXY=
color=
onion=
text=
href=
src=
site:
support=
size=
contributor=
immerda.ch
action=
libosinfo
xmlns:
xmlns=
SUPPORT_URL=
HOME_URL=
REPO_PROXY=
socksProxy=
w3.org
>tinyproxy</a>
example.com
127.0.0.1
127.0.0.2
10.137.255.254
10.152.152.11
.onion
Qubes_onion
.i2p
https://yum.qubes-os.org
http://HTTPS///
localhost
projectName
QubesOS_onion
{{Check.torproject.org}}
<!--
scurl-download
"

## NOTE: does not support spaces
## NOTE: double quotes (") are hardcoded below in the script
## NOTE: case-sensitive. Capitalization is important.
string_replace_tuples_list="\
<u>[http:// http://
<u>[https:// https://
\"[https:// https://
([https:// https://
([http:// http://
(http:// http://
(https:// https://
[https:// https://
[http:// http://
<ref>http:// http://
<ref>https:// https://
</ref>
{{link|link=
{{Link|link=
{{scurl
}}
<s>
</s>
),
|url=
'''
''
<s>
|
<blockquote>
</blockquote>
<
>
"

if grep --ignore-case --fixed-strings "$grep_skip_fixed_string_list_to_exclude" <<<"$link_result_cleaned" >/dev/null 2>&1; then
  exit 0
fi

## '<ref name=f_secure>https://blog.f-secure.com/cold-boot-attacks/'
if grep --fixed-strings --ignore-case "name=" <<<"$link_result_cleaned" >/dev/null 2>&1; then
  longest_match_after_character=">"
  link_result_cleaned="${link_result_cleaned#*"${longest_match_after_character}"}"
fi

while read -r replace_config_line; do
  if [ -z "$replace_config_line" ]; then
    log error "replace_config_line is empty!"
    exit 1
  fi

  log debug "replace_config_line: '$replace_config_line'"
  read -r first second <<<"$replace_config_line"

  log debug "first: $first"
  log debug "second: $second"

  if [ -z "$first" ]; then
    die 1 "first is empty!"
  fi

  link_result_cleaned="$(str_replace "$first" "$second" <<<"$link_result_cleaned" 2>/dev/null)"
done < <(stecho "$string_replace_tuples_list")

first='"'
second=""
link_result_cleaned="$(str_replace "$first" "$second" <<<"$link_result_cleaned" 2>/dev/null)"

case "$link_result_cleaned" in
  http://|https://|"()")
    exit 0
    ;;
esac

stprint "$link_result_cleaned"
