#!/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_MAP_OUT"} ) ) { push ( @{$my_main{"DM_MAP_OUT"}}, "2FOFC" ) } ; 
if ( !exists( $my_main{"DM_SOLV_FLIP"} ) ) { push ( @{$my_main{"DM_SOLV_FLIP"}}, "0.0" ) } ; 

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

my $MACRO_FILE = "re_fft_map.com" ;
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 'f' || substr($keyword, 0,4) eq "flip" ) { goto FLIP }
    if ( $keyword eq 'm' || substr($keyword, 0,3) eq "map" ) { goto MAP }
    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)   sets file name of the created macro [$MACRO_FILE]
  -m|--map_out)  output kind of map [FOBS/3FO2FC/2FOFC/SIGMA_A/COMB] [$my_main{"DM_MAP_OUT"}[0]]
  -f|--flip)    solvent flip (flip = value / (1 - value)) [$my_main{"DM_SOLV_FLIP"}[0]]
     --doit)    create the macro 
END_OF_TEXT
    exit ;
OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
FLIP: @{$my_main{"DM_SOLV_FLIP"}} = shift ( @{$commands{$keyword}} ) ; next ;
MAP: @{$my_main{"DM_MAP_OUT"}} = 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 "subroutine int MAP_FROM int MAP_TO " ;

if ( $my_main{"DM_SOLV_FLIP"}[0] == 0.0 ) 
{
    print MACRO << "END_OF_TEXT" ;

!The zero flat
! make map MAP_FROM set -10000 -9900 9999.0
! make map MAP_FROM from MAP_TO invert reshift rescale 0
make map MAP_FROM set -10000 -9900 0.0

END_OF_TEXT
}
else
{
    my $FLIP = $my_main{"DM_SOLV_FLIP"}[0] ;
    my $GAMMA = - $FLIP / ( 1.0 - $FLIP ) ;
    print MACRO << "END_OF_TEXT" ;

! The flipping - inverting the rest - the solomon way
make map MAP_FROM set -10000 -9900 9999.0
make map MAP_FROM from MAP_TO invert reshift rescale scale $GAMMA

END_OF_TEXT
}

print MACRO "fourier map MAP_FROM \n" ;

if ( @{$my_main{"DFR_RFREE"}}[0] eq "OFF" )
{ print MACRO "reflect sele WORK_REFL end shell 10  r-values\n" ; }
else
{
    print MACRO
	"reflect sele WORK_REFL end shell 10  r-values rescale\n", 
	"reflect sele TEST end shell 10 r-values fixed\n" ;
}

    print MACRO << "END_OF_TEXT" ;
 
! ! ! At this point you can call the MAIN atomic probability function
! ! if ( HIST_TEMP .gt. 2.0 ) then
! ! refl select WORK_REFL end set ampl phase fwork = fcalc * 1
! ! <>utils/hist_match MAP_FROM HIST_TEMP
! ! end_if

END_OF_TEXT

if ( $my_main{"DM_MAP_OUT"}[0] eq "FOBS" )
{ print MACRO "<?MAIN_CMDS:calc_fo_phase_fc_map.cmds MAP_TO \n" ; }

elsif ( $my_main{"DM_MAP_OUT"}[0] eq "3FO2FC" )
{ print MACRO "<?MAIN_CMDS:calc_fofc_diff_map.cmds MAP_TO 3.0 2.0\n" ; }

elsif ( $my_main{"DM_MAP_OUT"}[0] eq "2FOFC" )
{ print MACRO "<?MAIN_CMDS:calc_fofc_diff_map.cmds MAP_TO 2.0 1.0\n" ; }

#elsif ( $my_main{"DM_MAP_OUT"}[0] eq "SIGMA_A" )
#{ print MACRO "<?MAIN_CMDS:calc_2fofc_sigma_a_map.cmds MAP_TO\n" ; }

elsif ( $my_main{"DM_MAP_OUT"}[0] eq "COMB" )
{
my @HKL = ( $my_main{"DFR_LIMIT_H"}[0], $my_main{"DFR_LIMIT_K"}[0], $my_main{"DFR_LIMIT_L"}[0] ) ;

print MACRO << "END_OF_TEXT" ;
! phase combination using sigma_a
write over file main_fcalc.phase refl select WORK_REFL end fcalc polar 
spawn main_2_sigmaa_2_main.ccp4  > sigmma.out
read file sigmaa.phase refl init limit @HKL re_read \\ 
     reso RESOL_MIN RESOL_MAX friedel 
refl key TEST sele .n all end
refl key WORK_REFL sele defined end
< ?MAIN_CMDS:calc_fo_map.cmds MAP_TO

END_OF_TEXT

}
else
{ 
    print "  CREATE_RE_FFT> %ERROR: OUT MAP NOT RECOGNIZED [", @{$my_main{"DM_MAP_OUT"}}, "] \n" ; 
    print MACRO "  CREATE_RE_FFT> %ERROR: OUT MAP NOT RECOGNIZED [", @{$my_main{"DM_MAP_OUT"}}, "] \n" ; 
}

print MACRO " return\n" ;
close ( MACRO ) ;
