#!/usr/bin/perl
# $Header: /home/makoto/perl/RCS/grephist-find,v 1.1 2000/03/12 13:49:56 makoto Exp makoto $
  $rcsid = q$Id: grephist-find,v 1.1 2000/03/12 13:49:56 makoto Exp makoto $;
$my_name = $0; 
$my_name =~ s#(.*)/##;

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

push(@INC,$my_path);

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

Getopts('dhrvxD: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"; 
if ( $#ARGV > -1 ) { $target_dir = $ARGV[0] }
else               { $target_dir = '.'}


find(\&check_file, $target_dir );

## while ( $file = shift @ARGV) {
sub  check_file {
    my ($name) = $File::Find::name;
    my ($file) = $_;
    if ( (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _ ) {
	
	$msgID = find_MSG_ID ( $file );
##     print "file($file), msgID($msgID) \n" if ( $opt_v);
	if ( ($msgID) && not_found_in_history($name, $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: $! "}}
		    unlink($file) || print " ** Cannot unlink: $! "}}
	    else              { 
		print " rather new ..";}
	    print "\n";}}
    else { printf "Entering ($file)\n" if ( $opt_v )
	       ; return 1}}

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

sub not_found_in_history {
    my ($name)  = $_[0];
    my ($file)  = $_[1];
    my ($msgID) = $_[2];
    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 ($name) "}}
#	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)
      -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-find,v $
# Revision 1.1  2000/03/12 13:49:56  makoto
# Initial revision
#
# 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
#

