#!/bin/bash

## 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.
##
## example:
## mw-file-to-weblinks ~/sourcesown/wiki-backup/kicksecure-wiki-backup/Secure_Downloads.mw

#set -x
set -e

## TODO: no root check

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

tr -s '[:blank:]' '[\n*]' < "$file_name" | while IFS= read -r word; do
   #echo "word: '$word'"

   if [ "$word" = "" ]; then
      continue
   fi

   [[ "$word" =~ .*https?://.* ]] || true

   if [ -n "${BASH_REMATCH[0]}" ]; then
      echo "${BASH_REMATCH[0]}"
   fi
done
