#!/bin/sh
#
# Copyright 1995, by Hewlett-Packard Company
#
# The code in this file is from the book "Portable Shell
# Programming" by Bruce Blinn, published by Prentice Hall.
# This file may be copied free of charge for personal,
# non-commercial use provided that this notice appears in
# all copies of the file.  There is no warranty, either
# expressed or implied, supplied with this code.
#
# NAME
#    findfile - recursively search for a file
#
# SYNOPSIS
#    findfile [-dfv] name [directory ...]
#
# DESCRIPTION
#    This command searches the directories and their
#    subdirectories for the file.  If no directories are
#    listed on the command line, the current directory is
#    searched.  If the file is found, the path name of the
#    file is printed.
#
#    If the file name contains wildcard characters, it must
#    be quoted so that the wildcard characters can be
#    processed inside this shell script rather than being
#    expanded into file names on the command line.
#
#    -d   Only find directories; default is to find files and
#         directories that match the name.
#    -f   Only find files; default is to find files and
#         directories that match the name.
#    -v   Verbose; by default, this command suppresses error
#         messages caused by attempting to access directories
#         and file that you do not have access to.  The verbose
#         option will cause these messages to be printed.
#
# RETURN VALUE
#    0    Successful completion
#    1    Usage error
#
# CHANGES
#    15 Jun 00  Added v option.
#    28 Jun 00  Added d and f options.
#
############################################################
CMDNAME=`basename $0`
USAGE="Usage: $CMDNAME [-dfv] file [directory ...]"
VERBOSE=                 # Verbose option
TYPE=                    # Type option to find(1) command.

#
# Parse command options.
#
if [ "$OPTIND" = 1 ]; then
	while getopts dfv OPT
	do
		case $OPT in
			d)	TYPE="-type d"
				;;
			f)	TYPE="-type f"
				;;
			v)	VERBOSE=TRUE
				;;
			\?)	echo "$USAGE" 1>&2
				exit 1
				;;
		esac
	done
	shift `expr $OPTIND - 1`
else
	echo "$CMDNAME: getopts is not supported." 1>&2
	exit 1
fi

if [ $# -eq 0 ]; then
     echo "$USAGE" 1>&2
     exit 1
fi

NAME=$1
shift

if [ "$VERBOSE" = "TRUE" ]; then
	find "${@:-.}" $TYPE -name "$NAME" -print
else
	find "${@:-.}" $TYPE -name "$NAME" -print 2>&1 |
		grep -v "Permission denied"
fi

exit 0
