#!/usr/local/bin/perl

use Tk;
#use Tk::Dialog;

$TALK="say 'be see ehn yoo has detected an error'";
$SOUND="/usr/bin/play";

require "dotsh.pl";

&dotsh("/usr/local/bcnu/etc/bcnuenv");

push (@INC,"$BCNUHOME/cgi-bin");

$ALERT="$BCNUHOME/www/sounds/alert.wav";

$ENV{DISPLAY}="localhost:0.0" unless $DISPLAY;

$soundplay=1;
$playtimes=10;

while (<STDIN>)
	{
	$description .= $_
	};

my $w = MainWindow->new(-background=>"white",); # || &soundonly;

$w->title("bcnu Alert message");

$change=0;
$image=$w->Photo(-file=>"$BCNUHOME/www/images/bcnu.gif");
$w->Label(-image=>$image,-bd=>0)->pack(-side=>"left",-padx=>"20", -pady=>"20");
$w->Button(-text=>"Acknowledge", -command=>sub{exit})->pack(-side=>"bottom");
$sound=$w->Button(-text=>"Audible alert on/off", -command=>\&mute)->pack(-side=>"bottom");
$w->Label(	-bg=>"black",
		-fg=>"yellow",
		-justify=>"left",
#		-width=>"80",
    		-textvariable=>\$description)->pack(-side=>"left");

#$fred=$w->Label(-textvariable=>\$change)->pack(-side=>"bottom");
#$cnc=$w->Button(-text=>"Cancel", -command=>sub{exit})->pack(-side=>"bottom");

$sound->update;

 while ($playtimes--) {
	if ($soundplay) {
		if ($TALK) {     	
			system("$TALK >/dev/null 2>&1");
			}
		$sound->update;
		if ($SOUND) {
			system("$SOUND $ALERT");
			}
		$sound->update;
		$change++;
	}
	for (1..30)
	{
     		$sound->update;
		sleep(1);
	}
}

#MainLoop;


sub mute {

#print "called with $soundplay\n";
if ($soundplay) {
	$soundplay=0;
	}
else	{
	$soundplay=1;
	}
$sound->update;
}

sub soundonly {

if ($TALK) {     	
	system("$TALK >/dev/null 2>&1");
	}
if ($SOUND) {
	system("$SOUND $ALERT");
	}
exit
}
