#!/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 $SEGM_DICT ;
if ( -f "segm_names" )
{ $SEGM_DICT = "segm_names" ; }
else
{ $SEGM_DICT = join ('', $ENV{"MAIN_CONF"}, "segm_names" ) ; }

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

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 'a' || substr($keyword, 0,4) eq "auto" ) { goto AUTO }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "file" ) { goto FILE }
    if ( $keyword eq 'g' || substr($keyword, 0,8) eq "group" ) { goto GROUP }
    if ( $keyword eq 's' || substr($keyword, 0,8) eq "strategy" ) { goto STRATEGY }
    if ( $keyword eq 'm' || substr($keyword, 0,5) eq "molec" ) { goto MOLEC }
    if ( substr($keyword, 0,4) eq "doit" ) { $doit = "TRUE" ; next ; }
    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;

HELP:
    my $MOLECULES = join ( ' ', @{$my_main{"SG_LIST"}} ) ;
    print << "END_OF_TEXT" ;
  -h|-help|--help) prints this message with available options and current status 
  -m|--molec)      defines the the list of molecular segments 
                   ${$my_main{"SG_NMOL"}}[0] : $MOLECULES
  -f|--file)       as --molec option, however, the list is taken from a file 
                   [$SEGM_DICT] 
                   and must be preceeded by the number of SEGIDs
  -g|--group )     defines NCS groups: specify the number of groups followed
                   by each group of segments embraced in " "
END_OF_TEXT

    my $IGR ;
my $GROUP ;
my $STRATEGY ;
for ( $IGR = 1; $IGR <= @{$my_main{"SG_NGROUP"}}[0] ; $IGR++ )
{
    $GROUP= join ( '', "SG_GROUP" , $IGR ) ;
    $STRATEGY = join ( '', "SG_GROUP" , $IGR, "_STRATEGY" ) ;
#    my $LINE = join (" ", $IGR, @{$my_main{$STRATEGY}}, '|', @{$my_main{$GROUP}},  ) ;
    printf ( "                 %3d %4s | @{$my_main{$GROUP}} \n", $IGR, @{$my_main{$STRATEGY}} ) ;
}

print << "END_OF_TEXT" ;
  -s|--strategy )  defines averaging scripts strategy - 
                   specify parameters for each group separately [1 EACH] 
                   EACH: average each molecule separately (default)
                   ONE: average one and distribute the averaged density to others
  -f|--rfree)      turns ON or OFF use of R-free flag [${$my_main{"DFR_RFREE"}}[0]]
  -a|--auto)       turns ON or OFF generation of auto start file \".main\" [$my_main{"AUTO_START"}[0]]
     --doit)       create all reasonable macros using current defintions
END_OF_TEXT
    exit ;
AUTO:
    $my_main{"AUTO_START"}[0] = shift ( @{$commands{$keyword}} ) ; next ;

GROUP:
    $IGR = 0 ; my $WORD ; 
foreach $WORD ( @{$commands{$keyword}} ) 
{
    $IGR++ ;
    $GROUP = join ( '', "SG_GROUP" , $IGR ) ;
    
    @{$my_main{$GROUP}} = split( ' ', $WORD ) ;
#    print $IGR, " ", $GROUP, " ", @{$my_main{$GROUP}}, "\n" ;

    $STRATEGY = join ( '', "SG_GROUP" , $IGR, "_STRATEGY" ) ;
#    if ( $my_main{$GROUP} == 1 )
#    {  $my_main{$STRATEGY} = "NONE" ; }
    if ( @{$my_main{$GROUP}} < 5 )
    {  $my_main{$STRATEGY}[0] = "EACH" ; }
    else 
    {  $my_main{$STRATEGY}[0] = "ONE" ; }
}

foreach $WORD ( @{$my_main{"SG_LIST"}} ) 
{
    my $MATCH = 0 ;
    my $MEMBER ;
    my $I ;
    for ( $I = 1; $I <= $IGR; $I++ )
    {
	$GROUP = join ( '', "SG_GROUP" , $I ) ;
	foreach $MEMBER ( @{$my_main{$GROUP}} )
	{
	    if ( $WORD =~m"$MEMBER" ) { $MATCH++ ; } #print "$MATCH ; $MEMBER found in $I $GROUP\n" ; }
	}
    }
    if ( $MATCH == 0 )
    {
	$IGR++ ;
	$GROUP = join ( '', "SG_GROUP" , $IGR ) ;
	@{$my_main{$GROUP}} = $WORD ;
	$STRATEGY = join ( '', "SG_GROUP" , $IGR, "_STRATEGY" ) ;
	$my_main{$STRATEGY}[0] = "EACH" ; 
    }
}

$my_main{"SG_NGROUP"}[0] = $IGR ;
next ;

MOLEC:
    @{$my_main{"SG_LIST"}} = @{$commands{$keyword}} ;
$IGR = 0 ; my $SEGM ;
foreach $SEGM ( @{$my_main{"SG_LIST"}} )
{
    $IGR++ ;
    $GROUP = join ( '', "SG_GROUP" , $IGR ) ;
    $STRATEGY = join ( '', "SG_GROUP" , $IGR, "_STRATEGY" ) ;
    @{$my_main{$GROUP}} = () ; $my_main{$GROUP}[0] = $SEGM ;
    $my_main{$STRATEGY}[0] = "EACH" ;
}
$my_main{"SG_NMOL"}[0] = $IGR ;
$my_main{"SG_NGROUP"}[0] = $IGR ;
next ;

FILE:
    $my_main{"SG_NMOL"}[0] = shift ( @{$commands{$keyword}} )  ; 
my $STR = shift ( @{$commands{$keyword}} )  ; 
if ( length($STR) > 0 ) { $SEGM_DICT = $STR ; }
open ( TXT, $SEGM_DICT ) || die "FILE OPEN ERROR: >$SEGM_DICT< \n" ; 

$IGR = 0 ; @{$my_main{"SG_LIST"}} = () ;
while(<TXT>)
{
    $IGR++ ;
    if ( $IGR > $my_main{"SG_NMOL"}[0] ) { $IGR-- ; last ; }
    chomp ;
    my $SEGM = $_ ;
    if ( length($SEGM) > 0 ) 
    {
	push ( @{$my_main{"SG_LIST"}}, $SEGM );
	$GROUP = join ( '', "SG_GROUP" , $IGR ) ;
	$STRATEGY = join ( '', "SG_GROUP" , $IGR, "_STRATEGY" ) ;
	@{$my_main{$GROUP}} = $SEGM ;
	$my_main{$STRATEGY}[0] = "EACH" ;
	$my_main{"SG_NGROUP"}[0] = $IGR ;
    }
    else
    {
	print "dictionary file does not contain enough segment names ? \n" ;
	last ;
    }
}
if ( $my_main{"SG_NMOL"}[0] > $IGR ) { $my_main{"SG_NMOL"}[0] = $IGR ; }
next ;

RFREE:
    $my_main{"DFR_RFREE"}[0] = shift ( @{$commands{$keyword}} ) ; next ;

STRATEGY:
    $IGR = shift ( @{$commands{$keyword}} ) ; 
    $GROUP = join ( '', "SG_GROUP" , $IGR ) ;
    if ( !exists ( $my_main{$GROUP} ) )
    {
	print " a non-existing group $IGR requested? \n" ;
	$PROC_ERROR = "TRUE" ;
    }
    else
    {
	$STRATEGY = join ( '', "SG_GROUP" , $IGR, "_STRATEGY" ) ;
	$my_main{$STRATEGY}[0] = shift ( @{$commands{$keyword}} ) ;
    }
    next ;

}


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

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

# print " SAVING FILE:::\n" ;
main_config::write_main_config_file ( $conf_file, %my_main ) ; 

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

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

my $macro_file ;

if ( $my_main{"AUTO_START"}[0] eq "ON" ) 
{
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_auto_start.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
}
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_read.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_save_file.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_re_image.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_symmetry.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_symmetry_ca.sh" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_refine.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_refine_b.sh" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_gen_solv.sh" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_make_masks.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_dm_prep.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_dm_next.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_dm_loop.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_re_fft_map.pl" ) ; 
              system ( "$macro_file --doit" ) ; 

if ( ${$my_main{"SG_NMOL"}}[0] > ${$my_main{"SG_NGROUP"}}[0] ) 
{	    
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_load_nmol.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_rms_fit.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_create_all_others.sh" ) ; 
              system ( "$macro_file --doit" ) ; 
}





