#!/usr/bin/perl
#
# Searching the bulletin area for a specific pattern
#
# DL6RAI 18.9.1998

require 'ctime.pl';
@lit_month = ('Jan','Feb','Mar','Apr','May','Jun',
          'Jul','Aug','Sep','Oct','Nov','Dec');

#
# Sortierfunktion nach Dateidatum
#
sub by_age {
	(stat($a))[9] <=> (stat($b))[9];
}

($clx_user_uid,$clx_user_gid,$home) = (getpwnam('clx_us'))[2,3,7];
$bulldir = $home . "/box/bulletin";

shift @ARGV; # this is mycall when called from within CLX

if ($#ARGV < 0) {
	print "Usage: grep <pattern>\n";
	exit 1;
} else { 
	$pattern = join(' ',@ARGV);
}
printf("\\1250\t%s\n",$pattern);

chdir($bulldir) || die "Cannot change to $bulldir\n";
opendir(DIR,$bulldir) || die "Cannot opendir $bulldir\n";
@dirs = grep { ! /^\./ } readdir(DIR);
closedir(DIR);

@found = `grep -l \"$pattern\" */*`;
chop(@found);

foreach (sort by_age @found) {
		($size,$filedate) = (stat($_))[7,9];
		($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
			gmtime($filedate);
		( $year < 70 ) ? $year += 2000 : $year += 1900;

		printf("[%s (%d-%3s-%4d)]\n",
			$_,$mday,$lit_month[$mon],$year);
		system("grep \"$pattern\" $_");
		print("  \n");
}

if ( $#found < 0 ) {
	print ("\\1252\n");
} else {
	printf("\\1251\t%d\n",$#found+1);
}
