#!/usr/bin/env perl
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 ) ; 

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

if ( !exists( $my_main{"DM_DIR_RT"} ) ) { push ( @{$my_main{"DM_DIR_RT"}}, "" ) } ; 

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

my $MACRO_FILE = "rms_fit.cmds" ;
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 'd' || substr($keyword, 0,9) eq "directory" ) { goto DIRECTORY }
    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]
  -d|--directory)   directory for rotation and translational macros [@{$my_main{"DM_DIR_RT"}}]
     --doit)        create the macro 
END_OF_TEXT
    exit ;
OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
DIRECTORY: @{$my_main{"DM_DIR_RT"}} = 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" ;

#echo
#echo " generating \"./${MACRO_FILE}\" macro "
#echo " used for RMS fit based superpositions of molecules within each group"
print MACRO << "END_OF_TEXT" ;
key ca sele atom name CA end
key out sele .n all end
<define_key_out

END_OF_TEXT


my $SAVE_FILE ;
my ( $IGR, $GROUP, $STRATEGY, $SEGM_FROM, $SEGM_TO ) ;

for ( $IGR=1; $IGR<= $my_main{"SG_NGROUP"}[0] ; $IGR++ )
{
    $GROUP = join ( '', "SG_GROUP" , $IGR ) ;
    $STRATEGY = join ('', "SG_GROUP", $IGR, "_STRATEGY" ) ;
    print MACRO "! $IGR base group $GROUP : strategy $my_main{$STRATEGY}[0] \n\n" ;

    if ( $my_main{$STRATEGY}[0] eq "LINK" )
    {
	$GROUP = join ( '', "SG_GROUP" , $IGR ) ;
#	my $GROUP_LINK = join ( '', "SG_GROUP" , $my_main{$STRATEGY}[1] ) ;
	my $GROUP_LINK = join ( '', "SG_GROUP" , $IGR-1 ) ;
	my @link_files = () ; 
	my @group_files = () ; 
	foreach $SEGM_FROM ( @{$my_main{$GROUP}} )
	{
#	    print "! base segment $SEGM_FROM : strategy $my_main{$STRATEGY}[0]\n" ;
	    foreach $SEGM_TO ( @{$my_main{$GROUP}} )
	    {
		if ( $SEGM_FROM ne $SEGM_TO )
		{
		    $SAVE_FILE = join ('', $my_main{"DM_DIR_RT"}[0], "mol_", 
				       $SEGM_FROM, "_to_", $SEGM_TO, ".com" ) ;
		    push (@group_files, $SAVE_FILE) ;
		}
	    }
	}
	foreach $SEGM_FROM ( @{$my_main{$GROUP_LINK}} )
	{
	    foreach $SEGM_TO ( @{$my_main{$GROUP_LINK}} )
	    {
		if ( $SEGM_FROM ne $SEGM_TO )
		{
		    $SAVE_FILE = join ('', $my_main{"DM_DIR_RT"}[0], "mol_", 
				       $SEGM_FROM, "_to_", $SEGM_TO, ".com" ) ;
		    push (@link_files, $SAVE_FILE) ;
		}
	    }
	}
	my $NN = 0 ;
	foreach $SEGM_FROM ( @link_files )
	{
	    system ( "ln -s -f $link_files[$NN] $group_files[$NN] " ) ; $NN++ ;
	}
    } 
    elsif ( $my_main{$STRATEGY}[0] eq "COMPOSIT" )
    {
    print " ELSE ---- $IGR base group $GROUP : strategy $my_main{$STRATEGY}[0] \n" ;
	foreach $SEGM_FROM ( @{$my_main{$GROUP}} )
	{
	print MACRO "! base segment $SEGM_FROM : strategy $my_main{$STRATEGY}[0]\n" ;
	    foreach $SEGM_TO ( @{$my_main{$GROUP}} )
	    {
		if ( $SEGM_FROM ne $SEGM_TO )
		{
		    $SAVE_FILE = join ('', $my_main{"DM_DIR_RT"}[0], "mol_", 
				       $SEGM_FROM, "_to_", $SEGM_TO, ".com" ) ;
		    open ( TXT,  "> $SAVE_FILE" ) || die "FILE OPEN ERROR: >$SAVE_FILE< \n" ;
		    print TXT << "END_OF_TEXT" ;
! SAVING COMPSIT MODEL DATA: 
set matrix MAT_ROT number - 
  1.0 0.0 0.0 -
  0.0 1.0 0.0 -
  0.0 0.0 1.0 
set vari XTRAN global real = 0.0
set vari YTRAN global real = 0.0
set vari ZTRAN global real = 0.0
return 
END_OF_TEXT
close ( TXT ) ;

#rms coor all  eigen \\ 
#    sele ca .a segm name $SEGM_FROM .a .not out end \\ 
#    sele ca .a segm name $SEGM_TO .a .not out end 

                }
	    }
	}
    }
    else
    {
#    print " ESLE ---- $IGR base group $GROUP : strategy $my_main{$STRATEGY}[0] \n" ;
	foreach $SEGM_FROM ( @{$my_main{$GROUP}} )
	{
	print MACRO "! base segment $SEGM_FROM : strategy $my_main{$STRATEGY}[0]\n" ;
	    foreach $SEGM_TO ( @{$my_main{$GROUP}} )
	    {
		if ( $SEGM_FROM ne $SEGM_TO )
		{
		    $SAVE_FILE = join ('', $my_main{"DM_DIR_RT"}[0], "mol_", 
				       $SEGM_FROM, "_to_", $SEGM_TO, ".com" ) ;
		    print MACRO << "END_OF_TEXT" ;
echo from $SEGM_FROM to $SEGM_TO
make pair init seq name_mat \\
    sele ca .a segm name $SEGM_FROM .a .not out end \\ 
    sele ca .a segm name $SEGM_TO .a .not out end 
rms coor all  eigen pair
save over file $SAVE_FILE rms

END_OF_TEXT

#rms coor all  eigen \\ 
#    sele ca .a segm name $SEGM_FROM .a .not out end \\ 
#    sele ca .a segm name $SEGM_TO .a .not out end 

                }
	    }
	}
    }
}
print MACRO "return\n" ;

close ( MACRO ) ;
exit ;

# #    elsif ( $my_main{$STRATEGY}[0] eq "ONE" )
# #    {
# #	   $SEGM_FROM = $my_main{$GROUP}[0] ;
# #	   print MACRO "! base segment $SEGM_FROM : strategy $my_main{$STRATEGY}[0]\n" ;
# #	   foreach $SEGM_TO ( @{$my_main{$GROUP}} )
# #	   {
# #	       if ( $SEGM_FROM ne $SEGM_TO )
# #	       {
# #		   $SAVE_FILE = join ('', $my_main{"DM_DIR_RT"}[0], "mol_", 
# #				      $SEGM_FROM, "_to_", $SEGM_TO, ".com" ) ;
# #		   print MACRO << "END_OF_TEXT" ;
# #rms coor all  eigen \\ 
# #    sele ca .a segm name $SEGM_FROM .a .not out end \\ 
# #    sele ca .a segm name $SEGM_TO .a .not out end 
# #save over file $SAVE_FILE rms
# #END_OF_TEXT
# #
# #		   $SAVE_FILE = join ('', $my_main{"DM_DIR_RT"}[0], "mol_", 
# #				      $SEGM_TO, "_to_", $SEGM_FROM, ".com" ) ;
# #		   print MACRO << "END_OF_TEXT" ;
# #rms coor all  eigen \\ 
# #    sele ca .a segm name $SEGM_TO .a .not out end \\ 
# #    sele ca .a segm name $SEGM_FROM .a .not out end 
# #save over file $SAVE_FILE rms
# #
# #END_OF_TEXT
# #	       }
# #	   }
# #print MACRO "return" ;
# #
# #close ( MACRO ) ;
# #    }
# #}



