#!/bin/bash

## Example:
## mw-wiki-link-clean "https://en.wikipedia.org/wiki/Atomic_clock"
## Example:
## mw-wiki-link-clean "[https://en.wikipedia.org/wiki/Atomic_clock"
## Example:
## mw-wiki-link-clean "https://en.wikipedia.org/wiki/Atomic_clock"

#set -x
set -e

command -v str_replace >/dev/null

## TODO: no root check

link_result_cleaned="$1"
if [ "$link_result_cleaned" = "" ]; then
   echo "$0: ERROR: No link given!"
   exit 1
fi

## NOTE: does not support spaces?
## NOTE: case in-sensitive. No need to capitalization.
grep_skip_fixed_string_list_to_exclude=$(echo "\
<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_tupils_list=$(echo "\
<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 echo "$link_result_cleaned" | grep --quiet --ignore-case --fixed-string "$grep_skip_fixed_string_list_to_exclude" ; then
   exit 0
fi

## '<ref name=f_secure>https://blog.f-secure.com/cold-boot-attacks/'
if echo "$link_result_cleaned" | grep --quiet --fixed-string --ignore-case "name=" ; then
   longest_match_after_character=">"
   link_result_cleaned="${link_result_cleaned#*$longest_match_after_character}"
fi

while read -r replace_config_line ; do
   if [ "$replace_config_line" = "" ]; then
      echo "ERROR! replace_config_line is empty!"
      exit 1
   fi

   true "replace_config_line: '$replace_config_line'"
   read -r first second <<< "$replace_config_line"

   true "first: $first"
   true "second: $second"

   if [ "$first" = "" ]; then
      echo "ERROR! first is empty!"
      exit 1
   fi
   ## Second can be empty.
   #if [ "$second" = "" ]; then
      #echo "ERROR! second is empty!"
      #exit 1
   #fi

   link_result_cleaned=$(echo "$link_result_cleaned" | LANG=C str_replace "$first" "$second" 2>/dev/null)
done < <( echo "$string_replace_tupils_list" )

first='"'
second=""
link_result_cleaned=$(echo "$link_result_cleaned" | LANG=C str_replace "$first" "$second" 2>/dev/null)

if [ "$link_result_cleaned" = "https://" ]; then
   exit 0
fi
if [ "$link_result_cleaned" = "http://" ]; then
   exit 0
fi
if [ "$link_result_cleaned" = "()" ]; then
   exit 0
fi

echo "$link_result_cleaned"
