#!/bin/sh -e
#
# urls - grab urls from piped input or stdin
# dependencies: fix

# urls - output all urls
# grep -P -o '(?:https?://|ftp://|news://|mailto:|file://|\bwww\.)[a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*(\([a-zA-Z0-9\-\@;\/?:&=%\$_.+!*\x27,~#]*\)|[a-zA-Z0-9\-\@;\/?:&=%\$_+*~])+' "$@"

# grab urls from piped input or stdin
#  ~ credit to z3bra
# note: reportedly doesn't work with all versions of grep
#
# prot='[a-zA-Z]+://'
# host='[-a-zA-Z0-9._+]+'
# path='[-a-zA-Z0-9._+#=?&:;%/!~()]+'
#
# grep -Eo "${prot}${host}${path}"

case "$#" in
  0)
    /usr/bin/perl -pe 's/[^[:ascii:]]/+/g' | fix \
      | tr '{};<>[]()\ ' '\n' | grep -i 'http'
    ;;
  *)
    cat "$@" | /usr/bin/perl -pe 's/[^[:ascii:]]/+/g' | fix \
      | tr "'{};<>[]()\ " '\n' | grep -i 'http'
    ;;
esac
