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

if ( @ARGV <= 0 ) { print " create_work_model.pl REQUIRES a segment ID argument \n" ; exit ; } 
else { $WORK_MODEL = join( " ", @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_MASK"} ) ) { push ( @{$my_main{"DM_DIR_MASK"}}, "" ) } ; 

my $MACRO_FILE = "work_model.com" ;

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

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" ;

my ( $GROUP, $IGR, $SEGM_FROM, $SEGM_TO, $SEGM, $CRYST ) ;
my @LINES = () ;
my $MAP ;
if ( $my_main{"SG_NCRYST"}[0] > 1 )
    {
	for ( $CRYST = 0; $CRYST < $my_main{"SG_NCRYST"}[0] ; $CRYST++ )
	{
	    $GROUP = join ( '', "SG_CRYST" , $CRYST+1 ) ;
#    print " GROUP: $CRYST \n" ;
	    foreach $SEGM_FROM ( @{$my_main{$GROUP}} )
	    {
#		print " foreach : $SEGM_FROM \n" ;
		if ( $WORK_MODEL eq $SEGM_FROM )
		    {
			push ( @LINES, "set crystal $CRYST + 1" ) ;
#			$MAP = join ( '', "MAP_CRYS_" , $CRYST+1 ) ;
			$MAP = 2*$CRYST+1 ;
#			my $MAP = "MAP_CRYS_".2*$CRYST+1 ;
			push ( @LINES, "set vari WORK_SEGM char global = \" " ) ; 
			foreach $SEGM ( @{$my_main{$GROUP}} ) { @LINES = (@LINES, $SEGM, " " ) ; }
			push ( @LINES, "\" \n" ) ;
			push ( @LINES, "\n" ) ;
			@LINES = ( @LINES, "image map MAP_2FOFC OFF \n" ) ;
			@LINES = ( @LINES, "image map MAP_FOFC OFF \n" ) ;
			@LINES = ( @LINES, "set vari MAP_2FOFC global inte = ", $MAP, "\n" ) ;
			@LINES = ( @LINES, "set vari MAP_FO global inte = ", $MAP, "\n" ) ;
			@LINES = ( @LINES, "set vari MAP_FOFC global inte = ", $MAP + 1, " \n" ) ;
			@LINES = ( @LINES, "set vari MAP_WORK global inte = ", $MAP + 1, " \n" ) ;
			@LINES = ( @LINES, "image map MAP_2FOFC ON \n" ) ;
			@LINES = ( @LINES, "image map MAP_FOFC ON \n" ) ;
			@LINES = ( @LINES, "set vari MAP_ACT global inte = ", $MAP, " \n" ) ;
			last ;
		    }
	    }
	}
	
	push ( @LINES, "\n" ) ;
	push ( @LINES, "! image part \n" ) ;
	push ( @LINES, "\n" ) ;
	push ( @LINES, "ima from CRYSTAL \n" ) ;
	push ( @LINES, "ima erase \n" ) ;
	push ( @LINES, "ima erase CRYSTAL\n" ) ;
	push ( @LINES, "<>utils/show.cell \n" ) ;
	
    }
# single crystal from only
else
    { 
	push ( @LINES, "\n" ) ;
	push ( @LINES, "! image part SINGLE CRYSTAL\n" ) ;
	push ( @LINES, "ima erase\n" ) ;
	push ( @LINES, "ima erase CHAIN_TRACE \n" ) ;
	push ( @LINES, "\n" ) ;
    }
# the work group superposition - all forms
	push ( @LINES, "! here ALL forms \n" ) ;

for ( $IGR = 1; $IGR <= $my_main{"SG_NGROUP"}[0] ; $IGR++ )
{
    $GROUP = join ( '', "SG_GROUP" , $IGR ) ;
#    print " GROUP: $IGR \n" ;
    foreach $SEGM_FROM ( @{$my_main{$GROUP}} )
    {
        if ( $WORK_MODEL eq $SEGM_FROM )
	    {
#		print " INN: $SEGM_FROM \n" ;
		@LINES = ( @LINES, "set vari CURRENT_W char global = ", $SEGM_FROM, " \n" ) ;
		my $ICOL=94 ;
		my $ISTEP=4 ;
		push ( @LINES, "ima from CRYSTAL \n" ) ;
		push ( @LINES, "ima erase\n" ) ;
		foreach $SEGM_TO ( @{$my_main{$GROUP}} )
		{
#		print " INN TOO LOOP: $SEGM_TO \n" ;
		if ( $SEGM_FROM ne $SEGM_TO )
		    {
                    $ICOL = $ICOL + $ISTEP ;
                    @LINES = ( @LINES, "<?MAIN_UTILS:mol_work_model ",
			$my_main{"DM_DIR_MASK"}[0], "mol_", $SEGM_TO, "_to_", $SEGM_FROM, ".com ",
			$SEGM_TO, " ", $ICOL, " \n" )
		    }
		}

		push ( @LINES, "ima from \n" ) ;

		if ( $my_main{"IMAGE_CHAIN"}[0] ne "ON" ) 
		{
		    push ( @LINES, "image erase CHAIN_TRACE\n" ) ;
		    push ( @LINES, "image group CHAIN_TRACE\n" ) ;
		    push ( @LINES, "ima sele atom name C N CA .a segm name WORK_SEGM end col 1 bond \n" ) ;
		}
		@LINES = ( @LINES, "<?MAIN_CMDS:re_image.cmds WORK_SEGM \n" ) ;
		@LINES = ( @LINES, "image cent calc sele segm name CURRENT_W end \n" ) ;
		@LINES = ( @LINES, "image map over \n" ) ;
		@LINES = ( @LINES, "return \n" ) ;
#		print @LINES ;
		print MACRO @LINES ; 
		exit ;
	    }
    }
}

close(MACRO) ;
