#!/bin/sh
#
# FindURL
# Bourne shell script, should work on any Unix-based platform whose
# 'grep' command supports the -R and -l options.
#
# Global search for a given URL in all the web pages in a directory tree.
# Only works fully specified URLs that start with "http".
#
# Install this file in any directory that's in your PATH (or any
# other directory, if you don't mind invoking by specific # pathname).
# Give it execute permission:
#
#   cd (directory where you saved this this)
#   chmod +x FindURL
#
# Instructions:
#  1. cd to the desired website directory
#  2. Type "pwd" to make sure you are in the right directory.
#  3. type "FindURL.sh url > somefilename"
#      (where "url" is the URL to search for and "somefilename" is the
#       name of a file to store the list.)
#
# Example:
#
#  cd projects
#  pwd
#  FindURL https://www.kermitproject.org > filestochange
#
# Result: A list of every file that contains the given URL, relative to the
# current directory.  This list can be consulted after using ChangeURL to
# make a global URL change, to check that the new URLs are functioning
# correctly.
#
# Author: Frank da Cruz, 4 June 2017

if [ $# != 1 ]; then
  echo "Usage: $0 url > filename"
  exit 1
fi

grep -R xxx /dev/null > /dev/null
if [ $? -eq 2 ]; then
   echo "Fatal: grep command lacks -R option"
   exit 1
fi

grep -l xxx /dev/null > /dev/null
if [ $? -eq 2 ]; then
   echo "Fatal: grep command lacks -l option"
   exit 1
fi

for i in `grep -Rl http . | egrep "\.html$"`; do
   grep $1 $i > /dev/null
   if [ $? -eq 0 ]; then
     echo $i
   fi
done
exit
