#!/usr/local/bin/perl -w

# Antonios Kavarnos (Antonios.Kavarnos.Home@cs.ntua.gr) 05 Jun 2000

# Auto-Judge programming contest submit program
#	in Perl and uses fastmail (from package elm)

# VERSION 2.0.4

# TODO

use strict;

# ----- CONFIGURATION -----

my $fastmail_prg = "/usr/local/bin/fastmail";
#my $fastmail_prg = "/bin/echo";
my $judge_email = 'judge@cs.ntua.gr';

# uncomment for the competition should be
#my $problemIDre = '^(aproblem|bproblem|cproblem|dproblem|eproblem)$';
my $problemIDre = '^(disjoint|nafm|turns3x3|mindfa)$';
# uncomment otherwisefor normal use should be
#my $problemIDre = '^\w+$';

# change here your team ID
my $default_teamID = "ntua1";

# ----- END OF CONFIGURATION -----

#
# subroutines
#

sub user_input ($$$)
{
	my ($what,$default,$re)=@_;
	my ($user);

	do {
		print "Enter $what [$default]: ";
		chomp($user=<STDIN>);
		if (length($user) == 0) {
			$user = $default;
		}
		if ($user !~ /$re/) {
			print "Invalid input \"$user\"(should conform to /$re/)\n";
		}
	} while ($user !~ /$re/);
	return $user;
}

sub user_input_fn ($)
{
	my ($what) = @_;
	my ($user);

	do {
		print "Enter filename for $what: ";
		chomp($user=<STDIN>);
		unless (-f $user) {
			print "File \"$user\" does not exists or is not a file. Retry...\n";
		}
	} while (! -f _);

	return $user;
}

sub string2hex ($)
{
	my($string)=@_;
	my($result);

	$result = '';
	foreach (split('',$string)) {
		$result .= sprintf("%02X",ord($_));
	}
	return $result;
}

#
# main
#

my ($problemID,$language,$teamID,$password_plain,$filename);
my ($password_coded,$subject,$result);

$problemID = user_input("ProblemID","",$problemIDre);
$language = user_input("Language (c,c++,pascal,fortran)","",'^(c|c\+\+|pascal|fortran)$');
$teamID = user_input("TeamID",$default_teamID,'^\w+$');
$password_plain = user_input("Password","","");

$filename = user_input_fn("submission program");

$password_coded = string2hex($password_plain);
$subject = "problemID:$problemID Language:$language TeamID:$teamID Password:$password_coded";

$result = system($fastmail_prg,"-s",$subject,$filename,$judge_email);
if ($result == 0) {
	print "Submission OK\n";
} else {
	die "system() exited with $result: $! $?, stopped";
}

