#!/bin/csh -f

goto MAIN

USAGE:
  echo "$0 [-l] [-m] name ..."
  echo "Looks for files of the given names in each of the directories in the"
  echo "current path."
  set also = "also looks for"
  set incurr = "in the current"
  echo "  -m  $also name.* $incurr MANPATH directories"
  echo "  -l  $also name or name.* $incurr LD_LIBRARY_PATH directories"
  echo ""
  exit

MAIN:
if ( $#argv == 0 ) then
  goto USAGE
endif

if ( $#argv > 1 && "$argv[1]" == "-m" ) then
  shift
  set doman
endif

if ( $#argv > 1 && "$argv[1]" == "-l" ) then
  shift
  set dolib
endif

if ( $?doman) then
  # set standard manuals path if this has not already been done
  if ( $?MANPATH ) then
    set manuals = ( `echo "$MANPATH" | tr ':' ' '` )
  else
    set manuals = ( /usr/man /usr/contrib/man /usr/local/man )
  endif
endif

if ( $?dolib) then
  # use LD_LIBRARY_PATH
  if ( $?LD_LIBRARY_PATH ) then
    set libraries = ( `echo "$LD_LIBRARY_PATH" | tr ':' ' '` )
  else
    set libraries = ( /usr/lib /usr/local/lib )
  endif
endif

# set up the not-found list
set bad

# check each argument in turn
foreach a ( $argv )

  # assume not found
  set a_ok = n

  # check commands in the path
  set done_path
  foreach d ( $path )
    if ( " $done_path " =~ *" ${d} "* ) continue
    set done_path = ( $done_path $d )
    if ( -e $d/$a ) then
      echo "$d/$a"
      set a_ok = y
    endif
  end

  if ( $?doman) then
    # check the manuals
    set done_path
    foreach d ( $manuals )
      if ( " $done_path " =~ *" ${d} "* ) continue
      set done_path = ( $done_path $d )
      if ( -d $d ) then
        set list = ( `find $d -name "${a}.*" -print` )
        if ( "$list" != "" ) set a_ok = y
        foreach f ( $list )
	  echo $f
        end
      endif
    end
  endif

  if ( $?dolib) then
    # check the libraries
    set done_path
    foreach d ( $libraries )
      if ( " $done_path " =~ *" ${d} "* ) continue
      set done_path = ( $done_path $d )
      if ( -d $d ) then
        set list = ( `find $d \( -name "${a}.*" -o -name "${a}" \) -print` )
        if ( "$list" != "" ) set a_ok = y
        foreach f ( $list )
	  echo $f
        end
      endif
    end
  endif

  # if not found, add it to the list of not-found arguments
  if ($a_ok == n) set bad = ( $bad $a )
end

# list not-founds
if ( { tty -s } ) then
  foreach a ( $bad )
    echo "Could not find $a"
  end
endif
