#! /usr/bin/perl
# script to restore alarm settings using "burtwb"
# author Yi Qiang, yqiang@jlab.org

use strict;
use warnings;
use Tk;
require Tk::Dialog;

if(@ARGV < 1){
    print "Error: no input specified, exiting!\n";
    exit(1);
}
elsif(@ARGV > 1){
    print "Error: too many input parameters, exiting!\n";
    exit(2);
}

# change input name to lowercase
my $input_name = $ARGV[0];
$input_name =~ tr/A-Z/a-z/;
my $snap_dir = "/gluex/data/burt/alarms/";

my $found = 0;
my @list_names = ("gas","solenoid");
foreach my $list_name(@list_names){
    if($list_name eq $input_name){
        $found += 1;
    }
}

if($found<1){
    print "Error: wrong input name specified, exiting!\n";
    exit(3);
}
else{
    $snap_dir = $snap_dir.$input_name;
    &SelectBackupFile($input_name, $snap_dir);
}

exit(0);

# subroutine to select a file
sub SelectBackupFile{
    my $list_name = $_[0];
    my $snap_dir = $_[1];

    # create the text entry widget
    our $tk_main = MainWindow->new;
    $tk_main->title("Select File Name");
 
    my $win_label = sprintf( "\t\tSelect backup file name to restore $list_name alarms\t\t\t");
    my $tk_label = $tk_main->Label(-text=>$win_label);
    $tk_label -> pack;

    my @file_list = `ls -t $snap_dir/\*.snap`;
    foreach(@file_list){
	chop ($_);
    }

    our $file_name = "";
    if(@file_list>0){
	$file_name = $file_list[0];
    }

    my $tk_entry = $tk_main->Entry(-textvariable=>\$file_name);
    $tk_entry->pack(-expand=>1, -fill=>'x');

    if(@file_list>0){
	$file_name = $file_list[0];
    }

    my $tk_listbox = $tk_main->Scrolled("Listbox", 
                                        -scrollbars=>"e", 
                                        -selectmode=>"single", 
                                        -height=>10,
                                        -width=>0)->pack();
    $tk_listbox->insert('end', @file_list);
    $tk_listbox->bind('<Button-1>',sub{
        $file_name = $tk_listbox->get($tk_listbox->curselection());
                      });
    $tk_listbox->activate(0);
    $tk_listbox->selectionSet(0);
    $tk_listbox->focus();

    my $tk_ok_button = $tk_main->Button(-text=>'RESTORE',
                                        -command=>[\&Restore_Setting]);
    $tk_ok_button->pack(-side=>'left', -padx=>2);

    my $tk_dismiss = $tk_main->Button(-text=>'CANCEL',
                                      -command=>[$tk_main=>'destroy']);
    $tk_dismiss->pack(-side=>'right', -padx=>2);

    MainLoop;
}

sub Restore_Setting{
    our $file_name;
    our $tk_main;

    my $choice = $tk_main->Dialog(-title=>'Confirmation',
                                  -text=>'Are you sure to restore the alarms?',
                                  -default_button=>'NO',
                                  -buttons=>['YES','NO'])->Show();

    if($choice eq 'YES'){
    my $command = "burtwb -f $file_name";
    print "Excuting : $command \n";
    system "$command" || die "Could not execute $command \n";
    }
    else{
     print "Restore cancelled.\n";
    }
    $tk_main->destroy;
}
