#!/usr/local/bin/perl -- -*- Perl -*-

# Usage: redit pattern directory

$debugging = 0;

$pattern = shift;
$start_file = shift || $ENV{'LINK_BASE'} || '.';

die "Usage: $0 <pattern> <directory>\n" if (! $pattern || ! (-d $start_file));

@allfiles = `find $start_file -name '$pattern' -print`;

foreach (@allfiles) {
    chop $_;
    push(@textfiles, $_) if ( -T $_ );
}

die "No text files match pattern.\n" if(! @textfiles);

die "Too many text files ($#textfiles) match pattern.\n" 
    if(! (@textfiles == 1));

if ( $ENV{'EDITOR'} ) {
    print "Using $ENV{'EDITOR'} for $textfiles[$[]..\n";
    exec($ENV{'EDITOR'}, $textfiles[$[]);
}
else {
    print "No \$EDITOR, using vi for $textfiles[$[]..\n";
    exec('vi', $textfiles[$[]);
}






