use XML::Simple;
use Tk;
use Tk::DialogBox;
use Term::ANSIColor;
use Tk::ColorEditor;
use Tk::BrowseEntry;
use Tk::LabEntry;
use Tk::Pane;
my $SYSTEMJOB;
my @lic_options = ("none","opt1","opt2");
#license window vars; do not edit
my $mainwindow;
my $win2;
my $allserial = 0;
my $systemxml = "";
for (my $i = 0; $i < @ip; $i++) {
$tempip++;
my $serial = $confHlpr->getSerialNumber($itxip[$i]); #returns serial or X
my $createtime = time;
$systemxml .= "
$itxip[$i]
192.168.0.$tempip
$i
proceed
$serial
$createtime
unknown
unknown
unknown
unknown
password
none
unknown
unknown
";
}
$systemxml .= "";
my $xs = new XML::Simple(keeproot=>1, forcearray=>1);
$SYSTEMJOB = $xs->XMLin($systemxml);
#Set License
createLicenseWindow();
MainLoop();
sub createLicenseWindow {
my $mins = int(8); #waittime in minutes
my $waittime = int($mins * 60); #wait in seconds
$mainwindow = MainWindow->new();
$mainwindow->title("License Units");
$mainwindow->configure(-background=> 'wheat');
$mainwindow->protocol('WM_DELETE_WINDOW',sub{$mainwindow->messageBox(-message=>"Window cannot be closed this way.", -title=>"Disabled");return;});
my $center = $mainwindow->Frame->grid();
$center->configure(-background=> 'wheat');
$center->Label(-text=>"Select license option to set on unit. New feature.\n\nWill automatticly continue after $mins minutes.", -background=>'tan', -foreground=>'darkgreen', -font=>['courier', '14', 'bold'])->grid(-row=>0, -column=>0, -columnspan=>2, -sticky=>'ew');
my $lic_frame = $center->Scrolled('Frame', -scrollbars=>'e', -height=>'300')->grid(-row=>1, -column=>0, -columnspan=>2);
my $lic_grid = $lic_frame->grid();
$lic_grid->configure(-background=> 'wheat');
my @serialEnt;
for (my $count = 0; $count < @{$SYSTEMJOB->{systemlist}->[0]->{system}}; $count++) {
my $system = $SYSTEMJOB->{systemlist}->[0]->{system}->[$count];
if ($system->{serial}->[0] =~ m/X/) {
$system->{serial}->[0] = "";
}
my $bgcolor;
if ($count % 2 == 0) {
$bgcolor="LemonChiffon";
} else {
$bgcolor="cornsilk";
}
$lic_grid->Label(-text=>"IP: ".$system->{ip}->[0], -background=>$bgcolor."1")->grid(-row=>$count, -column=>0);
$serialEnt[$count] = $lic_grid->LabEntry(-label=>"Serial: ", -background=>$bgcolor."2", -textvariable=>\$system->{serial}->[0], -labelPack=>[-side=>'left', -anchor=>'w'])->grid(-row=>$count, -column=>1);
$lic_grid->BrowseEntry(-label=>"Set License: ", -background=>$bgcolor."3", -variable=>\$system->{license}->[0], -choices=>\@lic_options)->grid(-row=>$count, -column=>2);
my $blink = $lic_grid->Button(-text=>"Blink Unit", -background=>$bgcolor."4", -command=>sub {
if ($system->{raidtype}->[0] =~ m/software/i or $system->{raidtype}->[0] =~ m/unknown/i) {
qx"wincuri 2 $system->{ip}->[0] \"/usr/AmiNas/cli blink -t ld -l /dev/md255 raid\"";
}
if ($system->{raidtype}->[0] =~ m/hardware/i or $system->{raidtype}->[0] =~ m/unknown/i) {
qx"wincuri 2 $system->{ip}->[0] \"/usr/AmiNas/cli blink -t ld -l /dev/sda raid\"";
}
$serialEnt[$count]->focus;
})->grid(-row=>$count, -column=>3);
if ($system->{raidtype}->[0] =~ m/none/i) {
$blink->enabled = 0;
}
}
for (my $count = 0; $count < @serialEnt; $count++) {
#give the first system with no serail focus
my $system = $SYSTEMJOB->{systemlist}->[0]->{system}->[$count];
if ($system->{serial}->[0] eq "") {
$serialEnt[$count]->focus;
last;
}
}
for (my $count = 0; $count < @serialEnt; $count++) {
my $serialRef = \@serialEnt;
$serialEnt[$count]->bind('' => [\&Linputcheck, $count, $serialRef]);
}
$center->Button(-text=>"Continue", -background=>'tan', -command=>sub {
my $allset = 2;
my $message = "";
for (my $count = 0; $count < @{$SYSTEMJOB->{systemlist}->[0]->{system}}; $count++) {
my $system = $SYSTEMJOB->{systemlist}->[0]->{system}->[$count];
if ($system->{serial}->[0] eq "") {
$allset = 0;
} elsif (length($system->{serial}->[0]) ne 15) {
$message .= "Serial number ".$system->{serial}->[0]." is ".length($system->{serial}->[0])." charaters\n";
$allset = 1;
}
}
if ($allset == 1) {
$mainwindow->messageBox(-message=>"Serial numbers must be 15 charaters long\n\n$message", -title=>"Invalid Input");
} elsif ($allset == 2) {
$waittime = -1;
&setSerailNumbers;
$mainwindow->destroy;
}
})->grid(-row=>2, -column=>0, -columnspan=>2, -sticky=>'ew');
$center->LabEntry(-label=>"Auto Continue in: ", -background=>'tan', -textvariable=>\$waittime, -labelPack=>[-side=>'left', -anchor=>'w'])->grid(-row=>3, -column=>0);
$center->Button(-text=>"Stop Auto Continue", -background=>'tan', -command=>sub {$waittime = -1;})->grid(-row=>3, -column=>1);
my $timer = $mainwindow->repeat(1000, sub{
if ($waittime == 0) {
my $allset = 1;
$waittime = -1;
for (my $c = 0; $c < @{$SYSTEMJOB->{systemlist}->[0]->{system}}; $c++) {
if (length($SYSTEMJOB->{systemlist}->[0]->{system}->[$c]->{serial}->[0]) ne 15) {
$allset = 0;
}
}
if ($allset == 1) {
&setSerailNumbers;
$mainwindow->destroy;
} else {
licenseEmail();
}
} elsif ($waittime > 0) {
$waittime--;
}
});
}
sub Linputcheck {
my ($widget, $count, $serialRef) = @_;
my $system = $SYSTEMJOB->{systemlist}->[0]->{system}->[$count];
$system->{serial}->[0] =~ s/\s//g;
print "My serail number for unit $count is [".$system->{serial}->[0]."]\n";
if (length($system->{serial}->[0]) eq 15) {
if ($count + 1 eq @$serialRef) {
print "All serail numbers have been entered, correctly\n";
my $cnt = 30;
if ($allserial == 0) {
$allserial = 1; #only popup once
$win2 = $mainwindow->Toplevel();
#$win2->update;
$win2->raise();
$win2->protocol('WM_DELETE_WINDOW',sub{$win2->messageBox(-message=>"Window cannot be closed this way.", -title=>"Disabled");return;});
$win2->title("Ready to continue?");
$win2->Label(-text=>"Are you ready to continue or do\n you need to change the license settings?")->pack(-side => 'top');
$win2->Button(-text=>"Contine", -command=>sub{&setSerailNumbers; $mainwindow->destroy;})->pack(-side => 'left');
$win2->Button(-text=>"Change License", -command=>sub{$win2->destroy;})->pack(-side => 'right');
$win2->LabEntry(-label=>"Auto Continue in: ", -textvariable=>\$cnt, -labelPack=>[-side=>'left', -anchor=>'w'])->pack(-side => 'bottom');
my $timer2 = $win2->repeat(1000, sub{
$cnt--;
if ($cnt == 0) {
&setSerailNumbers;
$mainwindow->destroy;
}
});
}
} else {
print "There are more serial numbers to be entered\n";
@$serialRef[$count+1]->focus;
}
}
return;
}
sub setSerailNumbers {
for (my $count = 0; $count < @{$SYSTEMJOB->{systemlist}->[0]->{system}}; $count++) {
my $system = $SYSTEMJOB->{systemlist}->[0]->{system}->[$count];
system("plink ".$system->{ip}->[0]." \"echo '".$system->{serial}->[0]."' > /etc/itxserial\"");
}
return;
}