#!/usr/local/bin/perl -- # -*-Perl-*-
#------------------------------------------------------------------
# Filename:      fd
# Created on:    Mon Apr 10 19:01:12 1995
# By:            Liyun Wang, GSFC/ARC
# Last Modified: Tue Apr 18 13:39:13 1995 (lwang@achilles.nascom.nasa.gov)
#------------------------------------------------------------------
#
# Purpose:
#     To find file or files on the disk with the find command
#
# Modification History:
#     Version 1, created, Liyun Wang, GSFC/ARC, April 10, 1995

while ($ARGV[0] =~ /^-(.*)/ && shift) {
    if    ($1 =~ /^d$/) {$dir = shift @ARGV;}
    elsif ($1 =~ /^m$/) {$days = shift @ARGV;}
    elsif ($1 =~ /^h$/) {&usage;}
    elsif ($1 =~ /^v$/) {$verb = 1;}
    elsif ($1 =~ /^idl$/) {$idl = 1;}
    else {print "Invalid option -$1 ignored.\n";}
}

$| = 1;
# $purpose = "/disk1/lwang/.script/purpose";

$file = $ARGV[0];

$dir = "." unless (defined($dir));
unless ($dir =~ m|/$|) {$dir .= "/";}

die "Invalid searching directory!\n" unless (-d $dir);

&usage unless (defined($file));

if (defined($days)) {
    $mfile = "/tmp/marker$$";
    $sec_since = time - ($days * 86400);
    open(TEMP,">$mfile");
    close TEMP;
    utime($sec_since,$sec_since,$mfile);
    $cmd = "find $dir -name \"$file\" -newer $mfile";
} else {
    $cmd = "find $dir -name \"$file\"";
}

$cmd .= " -print" unless ($idl);
    
if ($verb) {
    print "Command to be used:\n";
    print "   $cmd\n";
    print "Searching ... ";
}

@content = `$cmd`;
if ($verb) {print "Done.\n";}

if (defined(@content)) {
    if ($idl) {
	chop(@content);
	&purpose(@content);
    } else {print "@content";}
} else {
    print "Nothing is found.\n";
}
unlink $mfile;

exit;

sub usage {
    print "Usage:   fd [options] filename\n\n";
    print "Options: -d dir_name   Set search directory (Default: current dir)\n";
    print "         -m time       Search files which are \e[1mtime\e[0m days old\n";
    print "         -v            Turn on verbose mode (shows what command will be used)\n";
    print "         -idl          Print out purposes of found IDL code\n";
    print "         -h            Print this message\n";
    exit;
}

sub purpose {
    local(@files) = @_;
    foreach $file (@files) {
	local($prefix);
	next unless (-e $file);
	if (defined($full)) {
	    print "$file:\n";
	    $prefix = '   ';
	}
	open(PFILE,"$file") || die "Cannot open $file for reading.\n";
	while (<PFILE>) {
	    chop;
	    next unless (/^\s*;/);
	    unless (defined($full)) {
		$start = 1 if (/;\+/);
		next unless ($start);
	    }
	    if (/;\s?NAME\s*:(.*)/i) {
		if ($1 =~ /^\s*$/) {$name_again = 1;} else {
		    ($name = $1) =~ s/^\s*//;
		    $name_again = 0;
		}
		next;
	    }
	    if (/;\s?PURPOSE\s*:(.*)/i) {
		if ($1 =~ /^\s*$/) {$pur_again = 1;} else {
		    ($purpose = $1) =~ s/^\s*//;
		    $pur_again = 0;
		}
		next;
	    }
	    if ($name_again) {
		($name) = ($_ =~ /^;\s*(.*)$/);
		$name_again = 0;
	    }
	    if ($pur_again) {
		($purpose) = ($_ =~ /^;\s*(.*)$/);
		$pur_again = 0;
	    }
	    if ($name && $purpose) {
		$out = $prefix . "$name\t- $purpose";
		print substr($out,0,80) . "\n";
		undef ($name, $purpose);
		last unless (defined($full));
	    }
	}
	close PFILE;
    }
}
