#!/s/std/bin/perl -w
#!/usr/bin/perl -w

# Alright. The hash variable %bigmama contains the information about
# which scripts you need to run. The hash key MUST be unique and that is
# the name that will appear on the button. The value of the hash entry
# is an anonymous array that contains two elements. The first element
# is the name of the script you want to execute, and the second element
# is either 'normal', or 'disabled'. This controls whether or not the
# button can be activated at all.

# Stuff you might need to change:
# Adding new entries to %bigmama.
# Changing the $condor_rm variable to be the path of the condor_rm executable.
# You can comment out a line from the hash table to get rid of a button.

use Tk;

# Add a symbolic name for something and a script name to run it with here.
%bigmama = (

# unique symbolic name => ["execute script file path", 'normal|disable'],

"ANL" => ["testme", 'normal'],
"BU" => ["testme", 'normal'],
"NCSA" => ["NCSA", 'normal'],
"UW" => ["UW", 'normal'],
);

# path to condor_rm
$condor_rm = "/bin/false";

# I wouldn't mess with the stuff below, unless you REALLY have to.

my @buttons = ();

$mw = MainWindow->new;
$mw->title("Condor GlideIn");

$index = 0;
foreach $key (sort keys %bigmama)
{
	$buttons[$index] = $mw->Button(-text => "$key-Off", -command =>
		DoWork($index, $key, $bigmama{$key}[0], $bigmama{$key}[1]),
		-state => $bigmama{$key}[1],
		-background => "red",
		-activebackground => "pink")->pack(-fill => "both", -expand => 1,);

	$index++;
}

$mw->Button(-text => "Exit", -command =>
	sub {
		exit;
	})->pack(-fill => "both", -expand => 1);


# the magic eight ball. Can we say closures?
sub DoWork
{
	my ($ind, $k, $script, $doit) = @_;
	my $active = 0;
	my @cluster;
	my @cnumbers;
	return sub
	{
		my @lines;
		my $cnum;

		if ($active == 0)
		{
			print "Gliding in to $k...\n";

			# start the script
			@lines = ();
			@lines = `$script | grep "submitted to cluster"`;

			# check the return value
			if (($? >> 8) != 0)
			{
				print "FAILURE of script \"$script\" detected!\n";
				$buttons[$ind]->configure(-text => "$k-Err");
				$buttons[$ind]->configure(-background => "gold");
				$buttons[$ind]->configure(-activebackground => "yellow");
				return;
			}

			@cnumbers = map /submitted to cluster (\d+)/s, @lines;
			chomp @cnumbers;

			print "@lines";

			foreach $cnum (@cnumbers)
			{
				print "Found cluster(s): $cnum\n";
			}


			$buttons[$ind]->configure(-text => "$k-On");
			$buttons[$ind]->configure(-background => "green");
			$buttons[$ind]->configure(-activebackground => "#C0FFC0");
			$active = 1;
		}
		else
		{
			print "Removing $k from pool...\n";
			$buttons[$ind]->configure(-text => "$k-Off");
			$buttons[$ind]->configure(-background => "red");
			$buttons[$ind]->configure(-activebackground => "pink");
			$active = 0;

			foreach $cnum (@cnumbers)
			{
				print "Removing cluster(s): $cnum\n";
				print "$condor_rm $cnum\n";
				`$condor_rm $cnum`;
				if (($? >> 8) != 0)
				{
					print "FAILURE to remove cluster $cnum!\n";
				}
			}
		}
	}
}

# start the madness
MainLoop;

