#!/usr/local/bin/perl
#                              -*- Mode: Perl -*- 
# active.pl -- 
# ITIID           : $ITI$ $Header $__Header$
# Author          : Ulrich Pfeifer
# Created On      : Wed Dec  8 13:19:47 1993
# Last Modified By: Ulrich Pfeifer
# Last Modified On: Fri Dec 10 10:12:10 1993
# Update Count    : 53
# Status          : Unknown, Use with caution!
# 

$home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
    (getpwuid($<))[7] || die "You're homeless!\n";

($tree) = @ARGV;
$tree = "Mail" unless $tree;
$tree =~ s/^://;

chdir $home;

$activefile = ".active-:$tree";
$oldactivefile = "$activefile.old";

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
                    $atime,$mtime,$ctime,$blksize,$blocks) = stat($activefile);
$activetime = $mtime;

rename ($activefile, $oldactivefile) 
    || die "Could not rename ~/$activefile: $!\n";

open(STDOUT, ">$activefile") 
    || die "Could not open ~/$activefile for write: $!\n";

open(ACTIVE, "<$oldactivefile") || die "Could not open file: $!\n";
while (<ACTIVE>) {
    ($group, $min, $max) = split;
    $dir = $group;
    $dir =~ s:\.:/:go;
    $dir = "$tree/$dir";
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
     $atime,$mtime,$ctime,$blksize,$blocks) = stat($dir);
    if ($mtime> $activetime) {
        print STDERR "$dir has new mails ?\n";
        opendir(DIR,"$dir") || warn "Can't open $dir: $!\n";
        undef $max;
        undef $min;
        while ($_ = readdir(DIR)) {
            if (/^[0-9]+$/) {
                $min = $_ if !defined $min || $min >= $_;
                $max = $_ if !defined $min || $max <= $_;
            }
        }
        closedir(DIR);
        $_ = "$group $max $min n\n";
        print STDERR "$group $max $min n\n";
    }
    print;
}
close(ACTIVE);



