#!/usr/athena/bin/perl

require 'ctime.pl';

$root = '/afs/sipb.mit.edu/project/www/queue';
$user = $ENV{'USER'};
$ENV{'HOME'} = $root;
$ENV{'PATH'} = '/usr/athena/bin:' . $ENV{'PATH'};
$report_address = 'stuffmaster@mit.edu';
$log_file = "$root/lock-log";

$mode = "Lock";
$action = "Locked";
$mail_action = "locked";
$infolder = 'inbox';
$outfolder = $user;

if ($0 =~ /\bunlock\b/) {
    $mode = "Unlock";
    $action = "Unlocked";
    $mail_action = "unlocked";
    $infolder = $user;
    $outfolder = undef;
}
elsif ($0 =~ /\brequeue\b/) {
    $mode = "Requeue";
    $action = "Requeued";
    $mail_action = "requeued";
    $infolder = $user;
    $outfolder = 'inbox';
}


if (! $user) {
    die "USER environment variable must be set.\n";
}

system 'folder', "+$infolder";

$childpid = open(PICK, "-|");

if (! defined($childpid)) {
    die "Aborting because fork for pick failed: $!.\n";
}
elsif ($childpid) {
    while (<PICK>) {
	chop;
	push(@msgs, $_);
	$msgs{$_}++;
    }
    close(PICK) || die "Aborting because close(PICK) failed: $!.\n";
}
else {
    exec 'pick', @ARGV;
    exit 1;
}

if (@msgs == 0) {
    die "No matching messages.\n";
}

for (@msgs) {
    s/\n//;
    $msgs{$_}++;
}

open(SCAN, "scan @msgs|") || die "Aborting because scan failed: $!.\n";

while (<SCAN>) {
    s/^(\s*[0-9]+)\+/$1 /;
    if (/^\s*([0-9]+)\s+([^\s].*)/) {
	$scan{$1} = $2;
    }
    else {
	die "Aborting because couldn\'t parse scan line: $_.\n";
    }
    print;
}

if (@msgs == 1) {
    &process($msgs[0]);
    exit 0;
}

close(SCAN) || die "Aborting because close(SCAN) failed: $!.\n";

while (scalar(keys %msgs) > 0) {
    print STDERR "$mode which one (all for all, return to exit)? ";

    chop ($response = <STDIN>);

    if ($response eq "") {
	exit 0;
    }

    if ($response eq "all") {
	for (sort keys %msgs) {
	    &process($_);
	    delete $msgs{$_};
	}
	last;
    }

    if ($response !~ /^[0-9]+$/) {
	print STDERR "Please enter a number.\n";
	next;
    }

    if (! $msgs{$response}) {
	local($,) = " ";
	warn "$response is not one of the listed messages.\n";
	print STDERR "Please pick one of:", keys %msgs, "\n";
	next;
    }

    &process($response);

    delete $msgs{$response};
}

sub process {
    local($msg) = $_[0];
    local($text) = "";

    if ($mail_action && $outfolder) {
	local($/) = "\n\n";
#	local($*) = 1;

	open(MSG, "$root/Mail/$infolder/$msg") || die "Aborting because couldn\'t open $root/Mail/$infolder/$msg: $!.\n";

	$text = <MSG>;

	close(MSG);

	$text =~ s/^(X400-|)Received:.*\n([ \t]+.*\n)*//g;
    }

    if ($outfolder) {
	if (system("refile $msg +$outfolder")) {
	    die "Aborting because refile failed.\n";
	}
    }
    elsif (system("rmm $msg")) {
	die "Aborting because rmm failed.\n";
    }

    if ($mail_action && $outfolder) {
	chop($ctime = &ctime(time));
	open(LOG, ">>$log_file") || 
	    die "Aborting because can't write to $log_file: $!.\n";
	print LOG "$ctime $ENV{'USER'} has $mail_action: $scan{$msg}\n" ||
	    die "Aborting because can't write to $log_file: $!.\n";
	close(LOG) ||
	    die "Aborting because close(LOG) failed: $!.\n";

#	open(MAIL, "|/usr/lib/sendmail -oi -t") || die "Aborting because sendmail failed: $!.\n";

#	print MAIL "From: $ENV{'USER'}\n";
#	print MAIL "To: $report_address\n";
#	print MAIL "Subject: $ENV{'USER'} has $mail_action: $scan{$msg}\n";
#	print MAIL "\nHeader of $mail_action message:\n\n";
#	print MAIL $text;

#	close(MAIL) || die "Aborting because close(MAIL) failed: $!.\n";
    }

    print "$action: $scan{$msg}\n";
}
