#!/usr/bin/env bash

# if -d then do diff at end
DIFF=:
if [ x$1 = x-d ]
then
  if ( which tkdiff > /dev/null 2>&1 ); then
    DIFF=tkdiff
  else
    DIFF="diff -u"
  fi
fi

OLD=/tmp/old.txt
NEW=/tmp/new.txt
>$OLD
>$NEW

for f in `find . -name '*.yam' |egrep -v \
  '\.svn|resources.yam-|resources.[0-9]*k.yam|..tmp|dot-grails|wiki.antlr.tests'`
do
  echo -n .
  echo $f >>$OLD
  echo $f >>$NEW
  
  grep -noP \
    '(?<!\%\()(?<!\%image\()(?:https?:\/\/|ftp:\/\/|mailto:)[^ \t\n\r]+' \
    $f >> $OLD

  grep -noP \
    '(?<!\%\()(?<!\%image\()(?:https?:\/\/|ftp:\/\/|mailto:)(?:[A-Za-z0-9:,_\-\.\/\?=&;#\+%~@]|\\.)*(?:[A-Za-z0-9_\-\.\/\?=&;#\+%~@]|\\.)' \
    $f >> $NEW

  echo >>$OLD
  echo >>$NEW
done
echo

$DIFF $OLD $NEW
