#!/usr/bin/perl
# $Header: /home/makoto/perl/RCS/grephist,v 1.3 2000/03/05 11:32:31 makoto Exp makoto $
  $rcsid = q$Id: grephist,v 1.3 2000/03/05 11:32:31 makoto Exp makoto $;
$my_name = $0; 
$my_name =~ s#(.*)/##;

$my_path = $0;
$my_path =~ s|/[^/]+$||;

push(@INC,$my_path);

use Time::Local;
require 'getopts.pl';


Getopts('hrxD:O');

if ( $opt_D ) { $date = $opt_D }
else          { $date = 365 ; }
$old_day = time - $date * 24 * 60 * 60 ;

$SPOOL       = '/var/news/spool/articles/';
if ( $opt_O ) { 
    print "OLD INN version support.\n";
	$grephistory = '/usr/news/bin/grephistory' ;
}
else {
	$grephistory = '/usr/local/news/bin/grephistory' ;
}
if ( $opt_h ) { usage(); exit;}
### print $old_day,"\n"; 

while ( $file = shift @ARGV) {
    if ( -d $file ) { print "$file is directory, skipping\n"; next}
    $msgID = find_MSG_ID ( $file );
    if ( ($msgID) && not_found_in_history($file, $msgID)) {
	    my(@property) = lstat( $file ); 
	      ($dev,$ino,$mode,$nlink,$uid,$gid,$pprdev,
	       $size,$atime,$mtime,$ctime,$blocksize,$blocks)
		= @property ;
	    print ' -> ',scalar(localtime($mtime));
	    if ( $mtime < $old_day ) { 
		if ( $opt_r ) { 
		    print " .. unlinking ";
		    unlink $file || print " ** Cannot unlink: $! "}}
	    else              { 
		print " rather new ..";}
	    print "\n";}}

sub find_MSG_ID {
    $FILE = $_[0] ;
    if ( -e $FILE ) {
	if ( open(FILE) == 0 ) {
	    $link_to = readlink ($FILE);
	    $link_to =~ s/$SPOOL//;
	    print " ** Cannot open File ($FILE): $! \n";
	    print "(link) ", $link_to ;}
	while(<FILE>) {
	    if  ( /^$/ ) { last }
	    if  ( /Message-ID:.*<(.*)>/i ) {
		print "$1\n" if ( $opt_x );
		return $1; }}
	print "\n";
	return 0 ;}}

sub not_found_in_history {
    my ($file)  = $_[0];
    my ($msgID) = $_[1];
    my ($notfound) = 0 ;
    $msgID =~ s/\$/\\\$/g;
    print "$grephistory \'<$msgID>\' 2>&1 |","\n" if ( $opt_x );
    open(GREPHISTORY,"$grephistory \"$msgID\" 2>&1 |");
#    open(GREPHISTORY,"echo \"$msgID\" 2>&1 |");
    while (<GREPHISTORY>) {
	print if ($opt_x);
	if (/Not found/i) { 
	    $notfound = 1 ;
	    print "Not in history ($file) "}}
#	else {
#	    s/$SPOOL//;
#	    print; }}
    close(GREPHISTORY);
    return $notfound ;}

# ----------------
sub usage {
    print <<HELPHELP;
$my_name: check if the article (in argument) is in history
          if -r specified, it removes the article.
Synonyms:
  $my_name [ -d ] [ -h ] [-O] [-x] [-D days] article [article2] [...]
Where:
      -O inn-1.7.2 (otherwise 2.2.2)ppp
      -r remove if not in History
      -D days: remove this days or old (default $date)
      -x debug
Note:
     you need to chdir to spool before doing this
version: $rcsid
HELPHELP
    exit;
}

__END__
# $Log: grephist,v $
# Revision 1.3  2000/03/05 11:32:31  makoto
# 2.2.2 help
#
# Revision 1.2  1999/07/01 14:39:49  makoto
# working
#
# Revision 1.1  1998/12/19 00:19:45  makoto
# Initial revision
#


require 'getopts.pl';

&Getopts('h');  
# ----------------- help -------------
if ( $opt_h ) {  &usage ; }
# -----------------------------------
while (<>) { 
}

__END__
