#!/usr/local/bin/perl
#	um -- scanning Unseen Mails.
#	   Original: by Haruhisa Hayashi <hayashi@atlas10.kuic.kyoto-u.ac.jp>
#	   Modified: by Katsumi Ohta <ohta@nuclear.dj.kit.ac.jp>
#	um.pl
#	   Original: by KAWAMOTO Yosihisa <kawamoto@ics.es.osaka-u.ac.jp>
$HOME = $ENV{'HOME'};
$MH   = $ENV{'MH'};
# private setting
 $path       = "$HOME/Mail";
 $unseen     = 'unseen';
 @folders    = split(/ /, 'inbox News Postmaster 98ML');
 $ignores    = ' alt comp fj jp ku rec ';
 $form_inbox = 'unseen2.form_mime';
 $form_other = 'unseen1.form_mime';
# public setting
$MH = "$HOME/.mh_profile" if ($MH eq '');
if ($path eq '' || $unseen eq '') {
    open MH || die "Do you use MH??\n";
    while (<MH>) {
	$path   = "$HOME/$1" if ($path eq '' && /^Path: (.*)$/i);
	$unseen = $1 if ($unseen eq '' && /^Unseen-Sequence: (.*)$/i);
    }
}

@folders = grep(-d && s/.*\///, <$path/*>) if (0+@folders == 0);
$mh_seq  = '.mh_sequences';
$unseen_num = 0;
foreach $folder (@folders) {
    next if ($ignores =~ / $folder / || ! -d "$path/$folder");
    open(MH_SEQ, "$path/$folder/$mh_seq") || next;
    @_ = <MH_SEQ>;
    if (0+grep(/^$unseen: /, @_) > 0) {
	print "<< Unseen Mail >> in +$folder\n";
	if ($folder eq 'inbox') {
	    $form = $form_inbox;
	} else {
	    $form = $form_other;
	}
	$scan = "scan +$folder $unseen";
	$scan .= " -form $form" if ($form ne '');
	if (open(SCAN, "$scan |")) {
	    while (<SCAN>) {
		print;
		$unseen_num++;
	    }
	    close(SCAN);
	}
    }
    close(MH_SEQ);
}
if ($unseen_num == 0) {
    print " You've already seen all of your mail.\n";
} elsif ($unseen_num == 1) {
    print " You have only one unseen mail.\n";
} else {
    print " You have $unseen_num unseen mails.\n";
}
exit 0;
