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

require 'ctime.pl';

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

$npar=$#ARGV;

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

if ($npar == 0) {
	print "Usage: grep <pattern>\n";
	exit 1;
}
 
if($npar == 1) { 
	$pattern = join(' ',@ARGV);

printf("\\1250\t%s\n \n",$pattern);

chdir($bulldir) || die "Es gibt kein Verzeichnis $bulldir\n";
opendir(DIR,$bulldir) || die "Es gibt kein Verzeichnis $bulldir\n";
@dirs = grep { ! /^\./ } readdir(DIR);
closedir(DIR);

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

if($npar == 2) {
	
	$pattern = shift @ARGV;
	$yr= shift @ARGV;
#	$pattern = join(' ',@ARGV);
printf("\\1250\t%s im Jahr %s\n",$pattern,$yr);

$bulldir = $bulldir . "/" ;
$bulldir = $bulldir . $yr;
chdir($bulldir) || die "Es gibt kein Verzeichnis $bulldir\n";
opendir(DIR,$bulldir) || die "Es gibt kein Verzeichnis $bulldir\n";
@dirs =  grep { ! /^\./ } readdir(DIR);
closedir(DIR);

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






@cnt=0;@cnt1=0;
printf(" \n");
# foreach (sort by_age @found) {
foreach (reverse @found) {
		system("grep \"DX-MB [0-9]* -\" $_");
		if($npar==1){system("/usr/local/clx/exec/interpr/cgrep \"$pattern\" $_");}
		if($npar==2){system("/usr/local/clx/exec/interpr/cgrep \"$pattern\" $yr/$_");}
		print("  \n");
		$cnt=$cnt+1;$cnt1=$cnt1+1;
	if( $cnt == 3 ){if($cnt1!=$#found+1)
		{
		printf("(%d/%d) Fundstellen. Weiter mit <return>, Abbruch mit /exit.\n",$cnt1,$#found+1);
		$wasnu=<STDIN>;
		if($wasnu eq "/exit\n") {die" \n";}
		$cnt=0;
		}}
}	

if ( $#found < 0 ) {
	printf ("Nichts gefunden zum Suchbegriff %s \n",$pattern);
} else {
	printf("\\1251\t%d\n",$#found+1);
}
