#!/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_MAP_IN"} ) ) { push ( @{$my_main{"DM_MAP_IN"}}, 1 ) } ; 
if ( !exists( $my_main{"DM_MAP_TARGET"} ) ) { push ( @{$my_main{"DM_MAP_TARGET"}}, 2 ) } ; 
if ( !exists( $my_main{"DM_MAP_MODEL"} ) ) { push ( @{$my_main{"DM_MAP_MODEL"}}, "2FOFC" ) } ; 
if ( !exists( $my_main{"DM_MAP_SOURCE"} ) ) { push ( @{$my_main{"DM_MAP_SOURCE"}}, "MAP" ) } ; 
if ( !exists( $my_main{"DM_PHEXT0"} ) ) { push ( @{$my_main{"DM_PHEXT0"}}, "" ) } ; 
if ( !exists( $my_main{"DM_PHEXT1"} ) ) { push ( @{$my_main{"DM_PHEXT1"}}, "" ) } ; 

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

my $MACRO_FILE = "dm_prep.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 's' || substr($keyword, 0,6) eq "source" ) { goto MAP_SOURCE }
    if ( $keyword eq 'i' || substr($keyword, 0,5) eq "input" ) { goto MAP_IN }
    if ( $keyword eq 'm' || substr($keyword, 0,5) eq "model" ) { goto MAP_MODEL }
    if ( $keyword eq 't' || substr($keyword, 0,6) eq "target" ) { goto MAP_TARGET }
    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]
  -s|--source)  using [MODEL/FILE/MAP] phases for map calculation [@{$my_main{"DM_MAP_SOURCE"}}]
  -i|--input)   input map [1/2/3/4/5/6/7/8/9/10] [@{$my_main{"DM_MAP_IN"}}]
  -m|--model)   model input map [3FO2FC/2FOFC/FOFC/COMB] [@{$my_main{"DM_MAP_MODEL"}}]
  -t|--target)  tagert map [1/2/3/4/5/6/7/8] [@{$my_main{"DM_MAP_TARGET"}}]
                target map should be followed by at least 3 allocatable maps
                multiple crystal target maps are asigned automatically
     --doit)    create the macro
END_OF_TEXT
    exit ;
 OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;

MAP_SOURCE: @{$my_main{"DM_MAP_SOURCE"}} = shift ( @{$commands{$keyword}} ) ; next ;
MAP_IN: @{$my_main{"DM_MAP_IN"}} = shift ( @{$commands{$keyword}} ) ; next ;
MAP_MODEL: @{$my_main{"DM_MAP_MODEL"}} = shift ( @{$commands{$keyword}} ) ; next ;
MAP_TARGET: @{$my_main{"DM_MAP_TARGET"}} = 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" ;

my ( $GROUP, $IGR, $SEGM_FROM, $SEGM, $CRYST ) ;
my @LINES = () ;
my $MAP ; my $RES_MAX ; my $RES_MIN ;

my $MAP_IN =  $my_main{"DM_MAP_IN"}[0] ;
my $MAP_TARG = $my_main{"DM_MAP_TARGET"}[0] ;
#print " map in> $MAP_IN <\n" ;

for ( $CRYST = 1; $CRYST <= $my_main{"SG_NCRYST"}[0] ; $CRYST++ )
{
    print MACRO "! crystal form ", $CRYST, "\n"  ;

    $GROUP = join ( '', "SG_CRYST" , $CRYST ) ;

    if ( length ( ${$my_main{"DM_PHEXT0"}}[0]) > 0 )
    { $RES_MAX = $my_main{"DM_PHEXT0"}[0] ; }
    else 
    { $RES_MAX = $my_main{"DFR_RES_MAX"}[0] ; }
    $RES_MIN = $my_main{"DFR_RES_MIN"}[0] ;
    $MAP = $MAP_TARG ;
    if ( $my_main{"SG_NCRYST"}[0] > 1 ) 
    { 
	$MAP = $CRYST*2 - 1 ; 
	$MAP_IN = $MAP ;
	$MAP_TARG = $MAP_IN + 1 ;
    }
    print $CRYST, " MAP : ", $MAP, " MAP_TARG ", $MAP_TARG, "\n" ;

# MODEL phase

    if (  $my_main{"DM_MAP_SOURCE"}[0] eq "MODEL" ) 
    {
	print MACRO "! MODEL phases \n\n" ;

#rfree flag	       
#	if ( $my_main{"SG_NCRYST"}[0] > 1 ) 
# 	{ main_config::write_get_diffr_data (*MACRO, \%my_main, $CRYST ) ; }

#	print MACRO  "refl key TEST_ORIG sele TEST end \n" ;
#	print MACRO  "refl key WORK_ORIG sele WORK_REFL end \n" ;
#	print MACRO  "refl key WORK_REFL sele WORK_ORIG .a resol ", 
#		   $RES_MIN, " ", $RES_MAX, " end \n" ;
#	print MACRO  "refl key TEST sele TEST_ORIG .a resol ", 
#		   $RES_MIN, " ", $RES_MAX, " end \n" ;
#	print MACRO "\n" ;

	$SEGM_FROM = join ( " ", @{$my_main{$GROUP}} ) ;
	print MACRO "set vari WORK_SEGM global = \"", $SEGM_FROM, "\"\n" ;
	
# map calculations

	if (  $my_main{"DM_MAP_IN"}[0] eq "3FO2FC" ) 
	{
	    print MACRO " <?MAIN_CMDS:re_phase.cmds  WORK_SEGM $MAP\n" ;
	    print MACRO " <?MAIN_CMDS:calc_fofc_diff_map.cmds $MAP 3.0 2.0\n" ; 
	}
	elsif (  $my_main{"DM_MAP_IN"}[0] eq "2FOFC" ) 
	{
	    print MACRO  " <?MAIN_CMDS:re_phase.cmds  WORK_SEGM $MAP\n" ;
	    print MACRO  " <?MAIN_CMDS:calc_fofc_diff_map.cmds $MAP 2.0 1.0\n" ;
	}
	elsif (  $my_main{"DM_MAP_IN"}[0] eq "FOFC" ) 
	{
	    print MACRO  " <?MAIN_CMDS:re_phase.cmds  WORK_SEGM $MAP\n" ;
	    print MACRO  " <?MAIN_CMDS:calc_fofc_diff_map.cmds $MAP 1.0 1.0\n" ;
	}
	elsif (  $my_main{"DM_MAP_IN"}[0] eq "COMB" ) 
	{
	    print MACRO << "END_OF_TEXT" ;
 <?MAIN_CMDS:re_phase.cmds  WORK_SEGM $MAP
 write over file main_fcalc.phase refl polar select WORK_REFL end
 spawn main_2_sigmaa_2_main.ccp4  > sigmma.out
 read file sigmaa.phase refl over resol $RES_MIN $RES_MAX friedel
 < ?MAIN_CMDS:calc_fo_map.cmds $MAP
 refl weight const
END_OF_TEXT
        }
	else
	{
	    print MACRO " ! create_dm_prep.pl: INPUT DM MAP [", $my_main{"DM_MAP_IN"}[0], "]\n" ;
#	print  " create_dm_prep.pl: ERROR: INPUT DM MAP [", $my_main{"DM_MAP_IN"}[0], "]\n" ;
	}
    }


# FILE phases

    elsif ( $my_main{"DM_MAP_SOURCE"}[0] eq "FILE" )
    {
	main_config::write_get_diffr_data (*MACRO, \%my_main, $CRYST ) ; 
	  print MACRO  "refl key TEST_ORIG sele TEST end \n" ;
	  print MACRO  "refl key WORK_ORIG sele WORK_REFL end \n" ;
	  print MACRO  "refl key WORK_REFL sele WORK_ORIG .a resol ", 
	  $RES_MIN, " ", $RES_MAX, " end \n" ;
	  print MACRO  "refl key TEST sele TEST_ORIG .a resol ", 
	  $RES_MIN, " ", $RES_MAX, " end \n" ;
	  print MACRO "<?MAIN_CMDS:calc_fo_map.cmds $MAP" ;
	  print MACRO "\n" ;

      }
    elsif ( $my_main{"DM_MAP_SOURCE"}[0] eq "MAP" )
    {
	print MACRO "! MAP 1 density phases \n\n\n" ;
	print MACRO "make map $MAP_TARG from $MAP_IN full init \n" ;
#	print MACRO "make map $MAP_TARG conv real \n" ;
#	print MACRO "make map $MAP_TARG set -1 1 9999 \n" ;
	print MACRO "make map $MAP_TARG from $MAP_IN copy \n" ;
    }
    else
    {
	print MACRO " create_dm_prep.pl: ERROR: map from unknown source", $my_main{"DM_MAP_SOURCE"}[0]," \n" ;
	print " create_dm_prep.pl: ERROR: map from unknown source", $my_main{"DM_MAP_SOURCE"}[0]," \n" ;
	exit ;
    }
#    my $MAP2 = $MAP + 1 ;
#print MACRO << "END_OF_DATA" ;
#
#make map $MAP2 zero
#make map $MAP2 conv real
#make map $MAP2 set -1 1 9999.
#make map $MAP2 from $MAP copy
#
#END_OF_DATA

}

if ( $my_main{"SG_NCRYST"}[0] == 1 ) 
{
    print MACRO << "END_OF_DATA" ;

! make map 2 zero
! make map 2 conv real
! make map 2 set -1 1 9999.
! make map 2 from 1 copy
make map $MAP_TARG + 1 from $MAP_TARG empty init real cell

set vari MAT_ROT int global = 3
END_OF_DATA
}
print MACRO "return\n" ;

close ( MACRO ) ;

#  LocalWords:  CRYST
