# NUEXPIRE.PL by Roy M. Silvernail
# A Waffle BBS maintainence utility (designed for MS-DOS systems)
# This program is released into the public domain
#
# Please refer to the file NUREAD.ME for installing and using NUEXPIRE
#
# Presented without warranty.  Bug reports, thank-you notes, bequests,
# etc. to roy%cybrspc@cs.umn.edu
#
# 10/26/91


select (STDOUT); $| = 1;

sub max { local ($v) = (shift @_); grep(($_ > $v) && ($v = $_),@_); $v; }

sub fixpath {
    local ($z,$y,@x);
    $z = shift(@_);
    $z =~ s#\\#/#g;
    $z =~ y/A-Z/a-z/;
    @x = split(/\//,$z);
    foreach $y (@x) {
        $y = substr($y,0,8);
    }
    join('/',@x);
}


# parse some args, eh?

$arglist = join(' ',@ARGV);
&usage() unless ($arglist =~ /-t/) || ($arglist =~ /-u/);
&usage() if ($arglist =~ /-t/) && ($arglist =~ /-u/);
$expire_time = ($arglist =~ /-t\s*(\S+)/) ? $1 : 1;   # default to 1
$expire_grade = ($arglist =~ /-g\s*(\S+)/) ? $1 : 1;   # default to 1
$join_file = $1 if ($arglist =~ /-u\s*(\S+)/);
$debug = ($arglist =~ /-d/) ? 1 : 0;        # explicitly set 0 if no debug

# first, let's find out some things...

open(INFILE,$ENV{"WAFFLE"}) || die("Can't find static file");

while (<INFILE>) {
    $waffle_dir = &fixpath($1) if /^waffle\s+:\s+(\S+)$/;
    $user_dir = &fixpath($1) if /^user\s+:\s+(\S+)$/;
}
close(INFILE);

# now we know where things are....

if ($join_file) {
    open(JOINFILE,"$user_dir/$join_file/join") || die("no join");
    while (<JOINFILE>) {
        /^(\S+)\s+(\S+)$/;
        $high{$1} = $2;
    }
    close(JOINFILE);
}

# brag time

print STDERR "NUEXPIRE.PL by Roy M. Silvernail\n";
print STDERR "referencing $user_dir/$join_file/join to delete read articles\n"
    if ($join_file);
print STDERR "default expiry age is $expire_time day(s)\n" unless ($join_file);
print STDERR "expiring articles graded $expire_grade and below\n"
    if $expire_grade > 1;
print STDERR "debug mode - no files will be deleted\n" if $debug;

open(USENET,"$waffle_dir/system/usenet") || die("can't find usenet file");
while (<USENET>) {
    if (/^DEFAULT/) {
            $news_root = &fixpath($1) if (/\/dir=(\S+)/);
    } else {
        print if $debug;
        next if /^#/ || /^$/;
        /\s*(\S+)/;
        $thisgroup = $1;
        $thisdir = &fixpath($news_root ."/". join('/',split(/\./,$1)));

        $thisdir = &fixpath($1) if (/\/dir=(\S+)/);
        next if $thisdir =~ /nul/i;
        $exp = /\/expire=(\S+)/ ? $1 : $expire_time;
        next if $exp =~ /no|never|0/i;
        next if $exp =~ /grade\s*(\S+)/i && $1 > $expire_grade;
        eval "opendir(DIR,\"$thisdir\") || die(\"can't open dir $thisdir\")";
        next if $@;
        @dtemp = grep(/[0-9]/, readdir(DIR));
        closedir(DIR);
        $dhigh = &max(@dtemp);
        foreach $f (@dtemp) {
            $tf = "$thisdir/$f";
            next if $f == $dhigh;
            if ($join_file) {
                if ($debug) {
                    print "$tf\n" unless $f > $high{$thisgroup};
                } else {
                    unlink($tf) unless $f > $high{$thisgroup};
                }
            } else {
                if ($debug) {
                    print "$tf\n" unless (-M $tf) < $exp;
                } else {
                    unlink($tf) unless (-M $tf) < $exp;
                }
            }
        }
    }
}
close(USENET);

exit 0;

sub usage {
    print STDERR "usage: nuexpire [-t x|-u name][-g x][-d]\n";
    print STDERR "       -t: expire older than x days\n";
    print STDERR "       -u: expire read articles from user/join\n";
    print STDERR "       -d: debug mode; print connamds to STDOUT\n";
    print STDERR "       either -t or -u must be specified, but not both\n";
    exit 1;
}

