4.4BSD/usr/src/contrib/mh-6.8/miscellany/audit/rfolder

#!/usr/bin/perl

$program = $0;
$program =~ s|.*/||;
$| = 1;

unshift(@INC, $ENV{'DELIVERPATH'});
require 'audit.pl' || die "$program: cannot include audit.pl: $@";
require 'mh.pl' || die "$program: cannot include mh.pl: $@";


@SW = (
	'-exec',
	'-except "+folder..."',
	'-all',
	'-verbose',
	'-clean',
	'-debug',
	'-recurse',
	'-norecurse',
	'-help',
      );


&mh_profile();

($folder = shift @ARGV) if ($ARGV[0] =~ /^\+/);

&mh_parse();

defined($SW{'help'}) && do {
    print "syntax: $program [+folder] [switches] [-exec MH command]\n";
    &print_switches();
    exit;
};


#if (!defined($folder)) {
#    $mailpath = $MH{'path'} . '/';
#    $folder = `mhpath`; chop $folder; 
#    $folder =~ s|^$mailpath|\+|;
#};


@args = (($program =~ /s$/) ? ("-all") : ( )); 
for ('all', 'recurse', 'norecurse') {
    push(@args, "-$_") if defined($SW{$_});
};


if (defined($SW{'exec'})) {
    @command = @ARGV;
} else {
    exec "folder $folder @args";
};


$path = `mhpath $folder`; chop $path;
die "$0: unable to change directory to $path: No such file or directory.\n" 
    if (! -d $path);


open(FOLDERS, "folder $folder @args -fast -noheader |") || die 
	"$0: cannot find list of folders: $?";

@exceptions = split(' ', $SW{'except'});

SKIP:
while (<FOLDERS>) {
    chop; 
    for $ef (@exceptions) {
	($f = $ef) =~ s/^\+//;
	next SKIP if ($_ =~ m|$f|);
	$f = `mhpath +$f`; chop $f;
	next SKIP if ($_ eq $f);
    };

    if (defined($SW{'clean'})) {
	$contents = `folder +$_ -total`; 
	if ($contents =~ /\s+0\s+messages/) {
	    print "removing empty folder +$_...\n" if defined($SW{'verbose'});
	    $f = `mhpath +$_`; chop $f;
	    rmdir($f) unless defined($SW{'debug'});
	    next; 
	};
    };

    print "@command +$_ \n" if defined($SW{'verbose'});
    system "@command +$_" unless (defined($SW{'debug'})); 
    print "\n";
};

close(FOLDERS);