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

## Takes a file as an input parameter.
## Goes through line by line of that file, goes through every line word by word
## and outputs all links included in that file or nothing.

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

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

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

file_name="$1"
test -r "$file_name"

while IFS= read -r word; do
  if [ -z "$word" ]; then
    continue
  fi
  [[ "$word" =~ .*https?://.* ]] || true
  if [ -n "${BASH_REMATCH[0]-}" ]; then
    printf "%s\n" "${BASH_REMATCH[0]}"
  fi
done < <(tr -s '[:blank:]' '[\n*]' <"$file_name")
