#!/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 ) ; 

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

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

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


my $MACRO_FILE="create_all_others.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 ( 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]
     --doit)    create the macro 
END_OF_TEXT
    exit;
OUTPUT: $MACRO_FILE = 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" ;
subroutine char SEGMENT
! creating all other segments of the current working segment group

END_OF_TEXT

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

for ( $IGR=1; $IGR<= $my_main{"SG_NGROUP"}[0] ; $IGR++ )
{
    $GROUP = join ( '', "SG_GROUP" , $IGR ) ;
    $SEGM_LIST = join ( ' ', @{$my_main{$GROUP}} ) ;

    print MACRO << "END_OF_TEXT" ;
! creating group no. $IGR 

if ( SEGMENT .in. " $SEGM_LIST " ) then
delete atom sele .not segm name SEGMENT .and. \\ 
       segm name $SEGM_LIST \#* end

END_OF_TEXT

    foreach $SEGM_FROM ( @{$my_main{$GROUP}} )
    {
	print MACRO "! base segment $SEGM_FROM \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 ( " save dile: $SAVE_FILE \n" ) ;
		print MACRO << "END_OF_TEXT" ;
<MAIN_UTILS:create_if_new_segm.com SEGMENT $SEGM_FROM $SEGM_TO $SAVE_FILE
END_OF_TEXT
            }
        }
    print MACRO "\n" ; 
    }
    print MACRO "end_if \n" ; 
}

print MACRO "return" ;

close ( MACRO ) ;



