#!/usr/bin/env perl
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
use Tk;

my %commands = () ;

if ( @ARGV <= 0 ) { push ( @{$commands{'help'}}, ' ' ) ; } 
else { (%commands) = main_config::get_commands ( @ARGV ) ; }

my $conf_file ;
if ( -f "main_config.data" )
{    $conf_file = "main_config.data" ; }
else
{    $conf_file = join ('', $ENV{"MAIN_CONF"}, "main_config.data" ) ; }

my %my_main = main_config::read_main_config_file ( $conf_file ) ; 

if ( !exists($my_main{"PREF_KICK_CR0"}  ) )  { push ( @{$my_main{"PREF_KICK_CR0"}} , 0 ) ; }
if ( !exists($my_main{"PREF_KICK_CR1"}  ) )  { push ( @{$my_main{"PREF_KICK_CR1"}} , 0 ) ; }
if ( !exists($my_main{"PREF_STEP_COOL"} ) )  { push ( @{$my_main{"PREF_STEP_COOL"}} , 0 ) ; }
if ( !exists($my_main{"PREF_STEP_MIN"}  ) )  { push ( @{$my_main{"PREF_STEP_MIN"}}  , 30 ) ; }
if ( !exists($my_main{"PREF_BVAL_MIN"}  ) )  { push ( @{$my_main{"PREF_BVAL_MIN"}}  , 5 ) ; }
if ( !exists($my_main{"PREF_BVAL_MAX"}  ) )  { push ( @{$my_main{"PREF_BVAL_MAX"}}  , 60 ) ; }
if ( !exists($my_main{"PREF_SCAL_XTARG"} ) )  { push ( @{$my_main{"PREF_SCAL_XTARG"}} , 800.0 ) ; }
if ( !exists($my_main{"PREF_SCAL_XDIFF"}) )  { push ( @{$my_main{"PREF_SCAL_XDIFF"}} , 5000.0 ) ; }
if ( !exists($my_main{"PREF_SCAL_DENS"} ) )  { push ( @{$my_main{"PREF_SCAL_DENS"}} , 200.0 ) ; }
if ( !exists($my_main{"PREF_MAP_XTARG"}  ) )  { push ( @{$my_main{"PREF_MAP_XTARG"}}  , 3 ) ; }
if ( !exists($my_main{"PREF_MAP_XDIFF"} ) )  { push ( @{$my_main{"PREF_MAP_XDIFF"}} , 3 ) ; }
if ( !exists($my_main{"PREF_MAP_DENS"}  ) )  { push ( @{$my_main{"PREF_MAP_DENS"}} , 1 ) ; }
if ( !exists($my_main{"PREF_NCS_FLAG"}  ) )  { push ( @{$my_main{"PREF_NCS_FLAG"}} , "OFF" ) ; }
if ( !exists($my_main{"PREF_NCS_FORCE"} ) )  { push ( @{$my_main{"PREF_NCS_FORCE"}} , 5.0 ) ; }
if ( !exists($my_main{"BREF_NCS_FORCE"} ) )  { push ( @{$my_main{"BREF_NCS_FORCE"}} , 0.004 ) ; }
if ( !exists($my_main{"PREF_TARGET"}  ) )  { push ( @{$my_main{"PREF_TARGET"}} , "LSQ" ) ; }
if ( !exists($my_main{"PREF_ML_MISSIN"}  ) )  { push ( @{$my_main{"PREF_ML_MISSIN"}} , "100" ) ; }
if ( !exists( $my_main{"BULK_SOLV_METHOD"} ) ) { push ( @{$my_main{"BULK_SOLV_METHOD"}}, "NONE" ) } ; 
if ( !exists( $my_main{"DFR_RFREE"} ) ) { push ( @{$my_main{"DFR_RFREE"}}, "OFF" ) } ; 

my $key ;
my @key_list = () ;
foreach $key ( sort keys (%my_main) ) 
{

if ( $key =~ m"PREF" or $key =~ m"BREF" or $key eq "BULK_SOLV_METHOD"
     or $key eq "DFR_RFREE" ) { push (@key_list, $key) ; }

}

######################################################

my $MACRO_FILE = "refine.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;

my $mw = MainWindow->new;
$mw->title("Tk: $MACRO_FILE");
my $f = $mw->Frame->pack(-side => 'bottom');
$f->Button(-text => "Exit", 
           -command => sub { exit; })->pack(-side => 'left');
$f->Button(-text => "doit",
           -command => \&create_macro )->pack(-side => 'bottom');
my $t = $mw->Scrolled("Text", -width => 32, 
                   -wrap => 'none')->pack(-expand => 1, -fill => 'both');

foreach $key ( @key_list )
{
	my $w = $t->Label(-text => "$key:", -relief => 'groove', -width => 20);
	$t->windowCreate('end', -window => $w);
	$w = $t->Entry(-width => 12, -textvariable => \$my_main{$key}[0]);
	$t->windowCreate('end', -window => $w);
	$t->insert('end', "\n");
}
$t->configure(-state => 'disabled');
MainLoop;


sub create_macro
{
system ( "
  create_refine.pl \\
  --kick  \"$my_main{\"PREF_KICK_CR0\"}[0]\" $my_main{\"PREF_KICK_CR1\"}[0] \\
  --cool  $my_main{\"PREF_STEP_COOL\"}[0] \\
  --step  $my_main{\"PREF_STEP_MIN\"}[0] \\
  --rec   $my_main{\"PREF_SCAL_XTARG\"}[0] \\
  --diff  $my_main{\"PREF_SCAL_XDIFF\"}[0] \\
  --dens  $my_main{\"PREF_SCAL_DENS\"}[0] \\
  --qmap  $my_main{\"PREF_MAP_XTARG\"}[0] \\
  --xmap  $my_main{\"PREF_MAP_XDIFF\"}[0] \\
  --map   $my_main{\"PREF_MAP_DENS\"}[0] \\
  --target $my_main{\"PREF_TARGET\"}[0] \\
  --ncs   $my_main{\"PREF_NCS_FLAG\"}[0] \\
  --bramp $my_main{\"PREF_BVAL_MIN\"}[0] $my_main{\"PREF_BVAL_MAX\"}[0] \\
  --doit
"
    )
}
