#!/afs/athena/contrib/perl/perl
#
# $Id: net_lookup.pl,v 1.4 93/05/31 11:16:52 tompalka Exp $
#
# find things in the net-directory
#

$basefilename =     "/mit/net-directory/net-dir/net.directory.part";
$mail_to_colleges = "/mit/consult/doc/college-email-";

#
# parse the command line arguments
#
while ($_=shift @ARGV) 
{
    /^-m$/ && ( $mail_only = 1  , next );
    /^-n$/ && ( $netdir_only = 1, next );
    /^-v$/ && ( $verbose = 1    , next );
    /^-/   && ( &usage_and_die );
    /\S/   && ( $keyword = $_   , next );
    &usage_and_die;
}

print "please wait.  the search might take a while..\n\n";


#
# attach appropriate filesystems if they ain't there
#
! -d "/mit/consult"       && system("attach -q consult");
! -d "/mit/net-directory" && system("attach -q net-directory");


# 
# call the file parser, passing the filename.  
#
! $mail_only && do {
    foreach $c ( 1..4 ) { &parse_file($basefilename.$c); }
};

! $netdir_only && do {
    foreach $c (1..3) { &parse_file($mail_to_colleges . $c . ".bak"); }
};

exit;


#
# print the usage message and die
#
sub usage_and_die {
    die <<EOU;
	
 usage:
        net_lookup [-n] [-m] [-v] keyword
	    
 This program will search the Net Directory for all the entries that
 contain the specified keyword.

      -m --> search Mail To Other Colleges directory only
      -n --> search Network Directory only
      -v --> display name of file searched
 keyword --> keyword to search for

EOU
}    

#
# go through the file slurping paragraphs, and calling another
# procedure to check if the paragraph contains the keyword or not
#
sub parse_file {
    local($filename) = @_;

    $/ = '';                        # paragraph mode
    $* = 1;

    $verbose && ( print "\n* searching through file $filename\n" );

    open(infile,$filename) || (print "ERROR cannot open file $filename: $!\n" );
    
    while(<infile>) {
	m/$keyword/i && print;
    }
    close infile;
}
