#!/usr/athena/bin/perl
#
# $Header: /src/common/usc/news/inn/local/RCS/cleanold.pl,v 1.4 1993/10/12 22:08:16 mcooper Exp $
#
# Simply script to hunt down and remove articles that still exist
# but are not in the news history file for whatever reason.
#
# Author: Michael A. Cooper <mcooper@usc.edu>
#

# =()<$SPOOL = "@<_PATH_SPOOL>@";>()=
$SPOOL = "/news/spool";
# =()<$NEWSBIN = "@<_PATH_NEWSBIN>@";>()=
$NEWSBIN = "/news/bin";

$DaysOld = 30;
$DoRemove = 1;
$PrtReason = 0;
$GREP_HISTORY = $NEWSBIN . "/grephistory";
$| = 1;

if (chdir($SPOOL) != 1) {
    &Error("chdir $SPOOL failed: $!");
    exit(1);
}

if (!opendir(DIR, $SPOOL)) {
    &Error("Cannot open directory $SPOOL.");
    exit(1);
}

while ($Name = readdir(DIR)) {
    #
    # Skip ".", ".." and any name with "." in it.
    #
    if ($Name eq "." || $Name eq ".." || ($Name =~ /\../)) {
	next;
    }

    if (!open(FIND, "find $Name -type f -mtime +$DaysOld -print |")) {
	&Error("Cannot run find on %s/%s.", $SPOOL, $Name);
	exit(1);
    }

    while (<FIND>) {
	chop;
	&Check($_);
    }

    close(FIND);
}
closedir(DIR);

exit(0);

#
# Subroutines
#
sub Error {
    printf STDERR "cleanold: ";
    printf STDERR @_;
    printf STDERR "\n";
}

sub Check {
    local($File) = @_;

    if (!open(FILE, "< $File")) {
	&Error("Cannot open file for reading: $File: $!");
	return;
    }

    while (<FILE>) {
	chop;
	if (length($_) <= 0) {
	    #
	    # We've reached the end of the headers and we should not have.
	    #
	    close(FILE);
	    if ($PrtReason) {
		&Error("%s :Cannot locate \"Message-ID\"", $File);
	    }
	    &Handle($File);
	    return;
	}

	local($hdr) = $_;
	$hdr =~ y/A-Z/a-z/;
	if ($hdr !~ /^message-id: .*/) {
	    next;
	}

	close(FILE);

	local(@parts) = split;
	local($cmd) = sprintf("%s -q '%s' > /dev/null 2>&1", 
			      $GREP_HISTORY, $parts[1]);
	if (system($cmd) != 0) {
	    if ($PrtReason) {
		&Error("%s :Not in news history database.", $File);
	    }
	    &Handle($File);
	}
	return;
    }
}

sub Handle {
    local($File) = @_;

    if ($DoRemove) {
	if (unlink($File) != 1) {
	    &Error("Remove failed: %s: %s", $File, $!);
	} else {
	    printf "%s :Removed\n", $File;
	}
    } else {
	printf "%s\n", $File;
    }
}

