#!/usr/athena/bin/perl

require '/mit/mkgray/perl/io.pl';

&retrievelist();
while(1){
&pmenu();
	$opt = &io'input();
	if($opt eq "1"){
		&showlist();
		&io'anykey();
	}
	elsif($opt eq "2"){
		&mrec();
	}
	elsif($opt eq "3"){
		&msen();
	}
	elsif($opt eq "4"){
		&doexit();
	}

}
sub retrievelist {
	open(LIST, "/mit/mkgray/private/.mailq");
	$n=1;
	while(<LIST>){
		chop($_);
		$list[$n] = $_;
		$al{$_} = $n;
		$n++;
		}
	$nmax = $n-1;
}

sub pmenu {
	print("1) Show List\n");
	print("2) Mail received\n");
	print("3) Mail sent\n");
	print("4) Exit\n");
}

sub showlist {
	foreach $i (1..$nmax){
		print("$i:  $list[$i]\n");
		}
}
sub mrec {
	$n = &qwho();
	unless($n == 1){
		$temp = $list[$n-1];
		$list[$n-1] = $list[$n];
		$list[$n] = $temp;
	}
}
sub msen {
	$n = &qwho();
	$sent = splice(@list, $n, 1);
	push(@list, $sent);
}

sub qwho {
	&showlist();
	$x = &io'input();
	int($x);
}

sub doexit {
	`rm /mit/mkgray/private/.mailq`;
	&rewrite();
	}

sub rewrite {
	open(OUT, ">/mit/mkgray/private/.mailq");
	foreach $f (1..$nmax){
		print(OUT "$list[$f]\n");
	}
	close(OUT);
	exit(0);
}
