#!/usr/local/bin/perl

# Perl program to find a full path to various TeX-interesting files.
# Michal Jaegermann, 1 July 1994
#
# Names with '.' in name are taken literally, otherwise all extensions
# for a given class will be added and tried.  The program does not
# expand any wildcards (beyond what was done by shell).  In particular
# do not use '~' in path names, unless you want it to be interpreted
# literally.  For 'home directory' use instead, in arguments, $HOME.
# The search is stopped after the first hit, unless '-a' option given.
#
# Default path values are used if not specified on a command line
# and environment variables do not give anything.  Modify defaults
# to fit your system.  What is below serves rather as an example.
# Adding other file types and wildcard expansion is left as an exercise
# to a user.

require "getopts.pl";

$sepr    = '/';			# this value for Unix
($myname = $0) =~ s:.*$sepr::;
$use = qq!$myname [-p path] [-r] [-n] [-b] [-f] [-m] [-a] <name> [<name> ...]

Print a full path to a file <name> which will be included by TeX.
If extension not specified explicitely all supported extensions are tried.
Options:
\t-p <path>  search along specified path
\t-r         recursive; include in search subdirectories
\t-n         non-recursive; only directories in path
\t-b         search for bibtex files
\t-f         search for fonts (.tfm files)
\t-m         search for metafont inputs
\t-a         show all files matching search criteria
!;

# defaults - modify to your installation and circumstances

@tex_exts = ('.tex', '.sty', '.cls', '.dtx', '.ltx', '.txi', '.texi');
@bib_exts = ('.bbl', '.bib', '.bst');
@font_exts = ('.tfm');
@mf_exts = ('.mf');

$myown = $ENV{'HOME'} . '/Library/texinputs:';
$default_texpath = '.:' . $myown . '/usr/lib/tex/inputs';
$default_bibpath = '.:' . $myown . '/usr/lib/tex/inputs';
$default_mfpath  = '.:' . $myown . '/usr/lib/mf/inputs';
$default_fontpath= '.:/usr/lib/tex/fonts/tfm:/LocalLibrary/Fonts/TeXFonts/tfm';

$recurse = 1;
$all     = 0;			# if 0 then stop at the first find

# end of defaults

&Getopts('p:rnbfma') || die $use;
if ($opt_p) { $path = $opt_p; }
if ($opt_r) { $recurse = 1; }
if ($opt_n) { $recurse = 0; }
if ($opt_b) {
    $path = $ENV{'BIBINPUTS'} unless $path;
    $path = $default_bibpath  unless $path;
    @exts = @bib_exts;
}
if ($opt_f) {
    $path = $ENV{'TEXFONTS'}  unless $path;
    $path = $default_fontpath unless $path;
    @exts = @font_exts;
}
if ($opt_m) { 
    $path = $ENV{'MFINPUTS'} unless $path;
    $path = $default_mfpath  unless $path;
    @exts = @mf_exts;
}
if ($opt_a) { $all = 1; }
@ARGV || die $use;

$path = $ENV{'TEXINPUTS'} unless $path;
$path = $default_texpath  unless $path;
@exts = @tex_exts unless @exts;

chop($cwd = `pwd`);
@dirs = grep(length > 0, split(':', $path));

while ($_ = shift @ARGV) {
    if (/\./) {
	@files = ($_);
    }
    else {
	@files = ();
	foreach $ext (@exts) {
	    push(@files, $_ . $ext);
	}
    }
    foreach $check (@dirs) {
	$check = $cwd if $check eq '.';
	chdir $check || die "Can't cd to $check; $!";
	&dodir($check);
    }
    chdir $cwd || die "Can't cd to $cwd; $!";
}

sub dodir {
    local($dpath) = @_;

    opendir(DIR, '.') || die "Can't open $dpath; $!";
    local(@dirlist) = readdir(DIR);
    closedir(DIR);
    
    @dirlist = sort @dirlist;

    #check this directory first
    
    foreach $file (@files) {
	for (@dirlist) {
	    if ($file eq $_) {
		print $dpath, $sepr, $_, "\n";
		next if $all; 
		return;
	    }
	}
    }

    if ($recurse) {
	for (@dirlist) {
	    next if $_ eq '.';
	    next if $_ eq '..';
	    if ( -d $_ ) {		# we have subdirectory
		$subdir = $dpath . $sepr . $_;
		chdir $_ || die "Can't cd to $subdir; $!";
		&dodir($subdir);
		chdir $dpath;
	    }
	}
    }
}