#!/usr/local/bin/perl5.6.1 
#die "I'm sorry ! pcr is unavailable at the moment... \n";

#author kva (Karen Ambrose)


my $prog = 'pcr';
$ENV{'DISPLAY'} || die "$prog: \$DISPLAY environment variable not set, Please set via 'ncd' or 'dis' command.\n";

use strict;
use Tk;
use Tk::IO;
require Tk::Balloon;
require 'badgerGetOpt.pl';

#use create_pcr;
my $create_pcr = '$HOME/badger-bin/finch/create_pcr';

#use cancel_pcr;

my $who = &user_who;
my @kids;

my $a = "Create a PCR plate";
my $FUNCTION_a;
my $b = "View/Cancel a PCR plate";
my $FUNCTION_b;

###################################################
# BUILD THE MAIN WINDOW                           # 
###################################################
my $mainWindow = MainWindow->new;
$mainWindow-> configure(cursor  => 'hand2');
$mainWindow->wm('minsize', 350, 300); 
$mainWindow->wm('maxsize', 350, 300);
my $f = $mainWindow->Frame(-height => 300, -width => 350);
$f->pack();
$mainWindow->idletasks();
$mainWindow->packPropagate(0);
$f->packForget();

$SIG{INT} = 'IGNORE';
$SIG{QUIT} = 'IGNORE';
$mainWindow->protocol('WM_DELETE_WINDOW' => sub {
    foreach my $n (@kids){
	if (kill 0 => $n){
	    kill 9 => $n;
	}
    }
    &Tk::exit;
});

my $text = "Welcome to PCR utility";
my $scrollbox = $mainWindow->Entry(-state => 'disabled',
                                   -width => 22,
                                   -font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
                                   -bg => 'white',
                                   -textvariable => \$text,
                                   )->pack(side => 'top');

my $frame_c = $mainWindow->Frame(-relief => 'groove', -borderwidth => '3');


my $cleanButton = $frame_c->Button(-text => 'Clean up',
				   -width => 10,
                                   -command => sub {
                                       
                                       foreach my $n (@kids){
                                           #warn "Looking at ...$n\n";
                                           if (kill 0 => $n){
#					      warn "I see child $n ! Die !\n";
                                               kill 9 => $n;
                                           }
                                           $FUNCTION_a->configure(-state => 'normal', -text => "$a", -background => 'light grey');
                                           $FUNCTION_b->configure(-state => 'normal', -text => "$b", -background => 'light grey');
                                       }
                                       @kids = ();
                                   });
$cleanButton->pack(-side => 'left');

my $exitButton = $frame_c->Button(-text => 'Close',
				  -width => 10,
				  -command => sub {
				      foreach my $n (@kids){
					  if (kill 0 => $n){
#					      warn "I see child $n ! Die !\n";
					      kill 9 => $n;
					  }
				      }
				      @kids = ();
				      &Tk::exit;
				  });
$exitButton->pack(-side => 'right');

$frame_c->pack(-side => "bottom");

my $frame_b = $mainWindow->Frame(-relief => 'groove', -borderwidth => '3');
$frame_b->pack(-side => "bottom");

my $label = $frame_b->Label(-text => "PCR comprises of tools which will\n allow the handling of pcr products.\n\nAuthor - kva\@sanger.ac.uk", 
                         -bg => 'white');
$label->pack(-side => 'top');
my $frame_a = $mainWindow->Frame(-relief => 'groove', -borderwidth => '3');
$frame_a->pack("-expand" => 1, -side => "right");

my $return;

$FUNCTION_a = $frame_a->Button(-text => "$a", -width => 25);
$FUNCTION_a->pack(-side => 'top');
$FUNCTION_a->configure(
                       -command => sub {
                           my $child;
                           $FUNCTION_a->configure(-state => 'disabled', -text => "Please wait", -background => 'Lightgoldenrod');
                           $child = Tk::IO->new(-linecommand => sub {$FUNCTION_a->configure(-state => 'normal', -text => "$a", -background => 'light grey');}, -childcommand => sub {$child->wait()});
			   my $return = $child->exec("$create_pcr");
                           push @kids, $return;
                       }
		       );
my $balloon1 = $frame_a->Balloon();
$balloon1->attach($FUNCTION_a,
                  -balloonmsg => 'Create a PCR plate',
		  );


$FUNCTION_b = $frame_a->Button(-text => "$b", -width => 25);
$FUNCTION_b->pack(-side => 'top');
$FUNCTION_b->configure(
		       -command => sub {
			   my $child1;
			   $FUNCTION_b->configure(-state => 'disabled', -text => "Please wait", -background => 'Lightgoldenrod');
			   $child1 = Tk::IO->new(-linecommand => sub {$FUNCTION_b->configure(-state => 'normal', -text => "$b", -background => 'light grey');}, -childcommand => sub {$child1->wait()});
#			   my $return = $child1->exec("gull_proj");
			   push @kids, $return;
		       }
		       );
my $balloon2 = $frame_a->Balloon();
$balloon2->attach($FUNCTION_b,
		  -balloonmsg => 'View or Cancel an exhisting pcr plate.',
		  );


MainLoop();


###############
# Subroutines #
###############

sub user_who {
    my $who;
    $who =`whoami`;
    chop ($who);
    return $who;
}



