#!/usr/bin/python
# -*- python -*-
import sys,os
import getopt
import string

(opts,f)  = getopt.getopt (sys.argv[1:], 'ehc',['--exact', '--help','--containing'])

def usage ():
	sys.stderr.write(r"""
name: fbn
synopsis: find files by name
usage: fbn PATTERN1 PATTERN2 ...

Find file names that match PATTERNs in current directory and
subdirectories.  PATTERN may contain ? and * as wildcards. Do not
forget to quote to prevent shell expansion of wildcards.

options:
	-c,--containing         Find all names containing all patterns,
	                          On by default
        -e,--exact              Find names that match any of PATTERNs exactly  
        -h,--help               print this help

examples:

	fbn exp                 Find all files containing "exp" in their name, 
                                  equivalent with `fbn -c foo'
	fbn foo                 Find all files whose name starts with "foo"
	fbn bla blub		Find files named either "bla" or "blub"

for more elaborate searches, use the command find(1).

author:  Han-Wen Nienhuys <hanwen@cs.uu.nl>
license: GPL

""")

prefix = '*'
suffix = '*'
infix = ''

for opt in opts:
	(o,a) = opt
	if o == '--help' or o == '-h':
		usage()
		sys.exit(0)
	elif o =='-c' or o == '--containing':
		prefix = '*'
		suffix = '*'
		infix = ''
	elif o =='-e' or o =='--exact':
		prefix = ''
		suffix = ''
		infix = '-or'
	else:
		print o
		sys.stderr.write ("unknown option  %s\n" % o)

if f == []:
	sys.stderr.write ("error: no PATTERNs given\n")
	usage()
	sys.exit(2)

pats = []
for p in f:
	pats.append (prefix + p + suffix)


ps = string.join (pats, " %s -name " % infix )
arg =  " . -name %s" % ps
spcmd =  string.split (arg)

os.execv("/usr/bin/find", spcmd)





