#!/usr/bin/perl -w
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
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 ) ; 

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

my $MACRO_FILE = join ( '', "load_", $my_main{"SG_NMOL"}[0], "mol.com" ) ;
my $MENU_BLOCK = "N_MOLECUL" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 'b' || substr($keyword, 0,5) eq "block" ) { goto BLOCK }
    if ( substr($keyword, 0,4) eq "doit" ) { $doit = "TRUE" ; next ; }
    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;

HELP:
    print << "END_OF_TEXT" ;
  -h|--help)    prints this message with available options and current status 
  -o|--output)  set file name of the created macro [$MACRO_FILE]
  -b|--block)   set menu block name [$MENU_BLOCK]
     --doit)    create the macro 
END_OF_TEXT
    exit;
 OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;

BLOCK: $MENU_BLOCK = shift ( @{$commands{$keyword}} ) ; next ;
}

if ( $PROC_ERROR eq "TRUE" ) { exit ; }

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

$conf_file = "main_config.data" ; 
main_config::write_main_config_file ( $conf_file, %my_main ) ; 

if ( $doit eq "FALSE" )  { exit ; }

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

print "creating \"$MACRO_FILE\" \n" ;
if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}
open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ;

print MACRO << "END_OF_TEXT" ;
 
menu block ${MENU_BLOCK} delete
menu block ${MENU_BLOCK} create
menu block ${MENU_BLOCK} color 110
menu block ${MENU_BLOCK} document >doc/menu/nmol.txt
END_OF_TEXT

if ( $my_main{"SG_NGROUP"}[0] != $my_main{"SG_NMOL"}[0] )
{
print MACRO << "END_OF_TEXT" ;
menu block $MENU_BLOCK item WORK    \\  
     add text "<?MAIN_CMDS:nmol_work_mode.cmds $MENU_BLOCK WORK"
menu block $MENU_BLOCK item PUT_TO  \\  
     add text "<?MAIN_CMDS:nmol_work_mode.cmds $MENU_BLOCK PUT_TO"
menu block $MENU_BLOCK item GET_FROM \\   
     add text "<?MAIN_CMDS:nmol_work_mode.cmds $MENU_BLOCK GET_FROM"
menu block $MENU_BLOCK item GOTO_SEQ \\   
     add text "<?MAIN_CMDS:nmol_work_mode.cmds $MENU_BLOCK GOTO_SEQ"
set vari  NMOL_MODE global char = WORK
menu block $MENU_BLOCK item WORK     button on
menu block $MENU_BLOCK item PUT_TO   button off
menu block $MENU_BLOCK item GET_FROM button off
menu block $MENU_BLOCK item GOTO_SEQ button off

menu block $MENU_BLOCK item CRE_OTHER \\ 
     add text "<create_all_others.cmds CURRENT_W"
END_OF_TEXT
}

my ( $IGR, $GROUP, $SEGM ) ;

for ( $IGR = 1; $IGR <= $my_main{"SG_NGROUP"}[0]; $IGR++ )
{
    $GROUP = join ( '', "SG_GROUP" , $IGR ) ;
    if ( scalar ( @{$my_main{$GROUP}} ) <= 1 ) { next ; }
    foreach $SEGM ( @{$my_main{$GROUP}} )
    {
	print MACRO << "END_OF_TEXT" ;
menu block $MENU_BLOCK item SID_$SEGM \\ 
  add text "<MAIN_CMDS:nmol_action.cmds NMOL_MODE CURRENT_W $SEGM"  
END_OF_TEXT
    }
}

if ( $my_main{"SG_NGROUP"}[0] != $my_main{"SG_NMOL"}[0] )
{
    print MACRO << "END_OF_TEXT" ;
menu block $MENU_BLOCK item ROT_MAPS add text "<rotate_maps.cmds"
END_OF_TEXT
}

print MACRO << "END_OF_TEXT" ;
menu page 9 current
return
END_OF_TEXT

close ( MACRO ) ;

