#!/bin/csh

if ( $#argv == 0 || x"$1" == "x-h" ) then
  cat - <<EOF
Syntax: foreachfile [-e] [-n filename_to_skip] [-a and_filename] [-d depth] filename command
Execute the specified command in every first-level subdirectory containing the file filename.
The -e option cause foreachfile to skip directories containing the file "error".
The -n option cause foreachfile to skip directories containing the file filename_to_skip.
The -a option specifies that the file and_filename must also exist.
The -d option specifies to go down to lower level subdirectories as well (the default is 1).
EOF
exit
endif

set depth=1

while ( $#argv != 0 )
    switch ($1)
	case "-e":
	    set skiperror
	breaksw
	case "-n":
	    set toskip=$2
	    shift
	breaksw
	case "-a":
	    set toadd=$2
	    shift
	breaksw
	case "-d":
	    set depth=$2
	    shift
	breaksw
	default:
	    break
	breaksw
    endsw
    shift
end

@ depth = $depth + 1

foreach dirfile ( `find -mindepth 2 -maxdepth $depth -name $1` )
  set dir=`echo $dirfile | sed "s+/$1++g"`
  pushd $dir >& /dev/null
  set doit=1
  if ( $?skiperror ) then
    if ( -e error ) then
      set doit=0
    endif
  endif
  if ( $?toskip ) then
    if ( -e $toskip ) then
      set doit=0;
    endif
  endif
  if ( $?toadd ) then
    if ( ! -e $toadd ) then
       set doit=0;
    endif
  endif
  if ( $doit ) then
    csh -f -c "$argv[2-]"
  endif
  popd >& /dev/null
end