#!/usr/bin/perl
#
# Mfolders [dir]
#
# Produce a list of the folders (i.e., subdirectories) of the given directory
# ($HOME/M  by  default),  and  a  list of the number of mail messages (i.e.,
# files whose names end with a digit) in each.  Also, we count  the  messages
# that  have  been  shown  to  the user (those whose names end with 'x'), and
# return the number of new messages.  The output is tcl set commands, because
# our caller is the Mail tcl script.

$dir = $ARGV[1] || "$ENV{'HOME'}/M";

for $d (<$dir/*>) {
	if ( -d $d ) {
		($f = $d) =~ s'.*/'';
		if (opendir(D,$d)) {
			@d = readdir(D);
			@f = grep(/[0-9]$/,@d);
			$cnt = $new = 0;
			for $m (@f) {
				++$cnt;
				++$new unless -f "$d/${m}x";
			}
			closedir(D);
			$x = @x;
		} else {
			$x = '?';	# Can't read folder.
		}
		print "set fcnt($f) {$cnt}; set xfldr($f) {$new}; lappend folders $f\n";
	}
}
