#!/usr/bin/perl -s--
#
# Mclassify [option]... [dir]...
#
# Classify files in a  mail  directory.   The  directory  defaults  to
# $HOME/M/in,  and  our classification is as follows:  We read thru
# the directory, looking for message files (those whose names end with
# a  digit).  When one is found, we look at its subject and/or source,
# and depending on what we find, we either  move  it  to  a  different
# folder,  or  perhaps delete it entirely.  It might be a good idea to
# run Mcleanup on the affected directories when we're done.

$exitstat = 0;
$| = 1;
($me = $0) =~ s".*/"";
$dbg = $ENV{"D_$me"} || $ENV{"V_$me"} || 1;
$h = $ENV{'HOME'} || '.';
@ARGV = ("$h/M/in") if !@ARGV;

for $mdir (@ARGV) {			# Mail directory.
	($Mdir = $mdir) =~ s"/[^/]*$"";
	opendir(D,$mdir) || die "Cannot opendir \"$mdir\" [$!]";
	@dfiles = readdir(D);	# List of files in directory.
	foreach $mfil (@dfiles) {	# Mail files.
		$path = "$mdir/$mfil";	# Full path to mail file.
		next if (! -f $path);	# Skip subdirectories.
		if ($mfil =~ /^\./) {
			print "Ignore \"$mfil\"\n" if $dbg>1;
			next;
		}
		if (($n,$l) = ($mfil =~ /^(\d+)([A-Za-z])$/)) {
			# File names ending with letters are "control" files.
			if (! -f "$mdir/$n") {	# Does message file exist?
				print "$path but no $mdir/$n\n" if $dbg>0;
				$flist = `ls $mdir/../*/$n`;	# Is it misplaced?
				@flist = split(/\s+/,$flist);	# Where we found it.
Msg:			for $msg (@flist) {
					if ($msg) {
						print "--- Msg $msg ...\n" if $dbg>0;
						($dir,$mfl) = ($msg =~ '^(.+/)(.+)$');
						print "--- Dir $dir\n" if $dbg>0;
						print "--- Move $path to $dir/$mfil\n" if $dbg>0;
						rename("$path","$dir/$mfil");
						last Msg;
					}
				}
				if (-f "$path") {
					print "$path deleted.\n";
					unlink("$path");
				}
				next;
			}
			if ($mfil =~ /19960330001/) {
				print "Found $mfil.\n";
			}
			# Process various control files:
			if ($l eq 's') { &subject; next}
			if ($l eq 'p') { &path; next}
			if ($l eq 'f') { &from; next}
			if ($l eq 't') { &to; next}
			print "Ignored: \"$mfil\"\n" if $dbg>1;
			next;
		}
		if ($mfil =~ /^(\d+)$/) {
			# File names consisting of digits are mail messages.
			print "Message \"$mfil\" OK\n" if $dbg>1;
			next;
		}
		print "Dubious \"$mfil\"\n" if $dbg>1;
		if ($i) {
			print "Trash \"$mfil\"\n" if $dbg>1;
			if (! -d "$mdir/.trash") {
				mkdir("$mdir/.trash",0775);
			}
			rename("$path","$mdir/.trash/$mfil");
		}
	}
}

exit $exitstat;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Move the current message files to a new folder.
sub moveto {
	local($folder) = @_;
	local($x,$fd);
	$fd = "$Mdir/$folder";
	mkdir($fd,0755) if (! -d "$fd");
	print "Move $n to $folder\n" if $dbg>0;
	@files = grep(/^$n/,@dfiles);
	foreach $x (@files) {
		print "Move $mdir/$x to $fd/$x\n" if $dbg>0;
		rename("$mdir/$x","$fd/$x");
	}
	if (-f "$p") {
		print "Err: $p still exists.\n" if $dbg>0;
		print "Move $p to $fd/$f\n" if $dbg>0;
		rename("$p","$fd/$f");
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub from {
	open(F,"<$path") || return;
	for $x (<F>) {
		if ($x =~ /bcslnx/) {
			&moveto('bcs');
			return;
		} 
		if ($x =~ /cambridge_town_crier/) {
			&moveto('ctc');
			return;
		} 
		if ($x =~ /privacy\@vortex/) {
			&moveto('privacy');
			return;
		} 
		if ($x =~ /naarsvol\@gac/) {
			&moveto('norsk');
			return;
		} 
		if ($x =~ /Don Henson\@gac/) {
			&moveto('mgt');
			return;
		} 
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub path {
	open(F,"<$path") || return;
	for $x (<F>) {
		if ($x =~ /math\.msu\.edu!owner-scand/) {
			&moveto('scand');
			return;
		} 
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub subject {
	open(F,"<$path") || return;
	for $x (<F>) {
		if ($x =~ /comp\.lang\.tcl/) {
			&moveto('risks');
			return;
		} 
		if ($x =~ /RISKS DIGEST/) {
			&moveto('risks');
			return;
		} 
		if ($x =~ /2020world Digest/) {
			&moveto('2020');
			return;
		} 
		if ($x =~ /^test\b/i) {
			&moveto('test');
			return;
		} 
		if ($x =~ /\.sh\d+ of \d+\s*$/) {
			&moveto('kit');
			return;
		} 
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
sub to {
	open(F,"<$path") || return;
	for $x (<F>) {
		if ($x =~ /!java-linux-request>$/) {
			&moveto('java');
			return;
		} 
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
