#!/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_NCYC"} ) ) { push ( @{$my_main{"DM_MAP_IN"}}, "1" ) } ; 
if ( !exists( $my_main{"DM_PHEXT0"} ) ) { push ( @{$my_main{"DM_PHEXT0"}}, "" ) } ; 
if ( !exists( $my_main{"DM_PHEXT1"} ) ) { push ( @{$my_main{"DM_PHEXT1"}}, "" ) } ; 
if ( !exists( $my_main{"DM_PHEXT_STEP"} ) ) { push ( @{$my_main{"DM_PHEXT_STEP"}}, "2" ) } ; 

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

my $MACRO_FILE = "dm_next.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 'c' || substr($keyword, 0,6) eq "cycles" ) { goto CYCLES }
    if ( $keyword eq 'e' || substr($keyword, 0,6) eq "extend" ) { goto EXTEND }
    if ( $keyword eq 's' || substr($keyword, 0,4) eq "step" ) { goto STEP }
    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]
  -e|--extend)  phase extension range [@{$my_main{"DM_PHEXT0"}} - @{$my_main{"DM_PHEXT1"}}]
  -c|--cycles)  number of full density modification cycles at given resolution [@{$my_main{"DM_NCYC"}}]
                0 exits before re_fft
  -s|--step)    steps of phase extensions for 
                a single reciprocal space lattice point [@{$my_main{"DM_PHEXT_STEP"}}]
     --doit)    create the macro "$MACRO_FILE"
END_OF_TEXT
    exit;
OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;

CYCLES: @{$my_main{"DM_NCYC"}} = shift ( @{$commands{$keyword}} ) ; next ;
EXTEND: 
    @{$my_main{"DM_PHEXT0"}} = shift ( @{$commands{$keyword}} ) ; 
    @{$my_main{"DM_PHEXT1"}} = shift ( @{$commands{$keyword}} ) ; next ;
STEP: @{$my_main{"DM_PHEXT_STEP"}} = 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 ; }

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

if ( $my_main{"DM_PHEXT0"}[0] !~ m"[0-9]." || $my_main{"DM_PHEXT1"}[0] !~ m"[0-9]." ) 
{ 
    @{$my_main{"DM_PHEXT0"}} = "" ; 
    @{$my_main{"DM_PHEXT1"}} = "" ; 
#    print "IT DOES WORK\n" ; 
}


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 $DM_NCYC = $my_main{"DM_NCYC"}[0] ;
if ( $DM_NCYC == 0 ) { $DM_NCYC = 1 ; }

print MACRO << "END_OF_TEXT" ; 

set vari MAT_ROT int global = 3

set vari MAP_INOUT = 2
set vari N_CYCLES = $DM_NCYC

END_OF_TEXT
    
my $ITEST = 0 ;
my $I ;
my $XLOW ;
my $XHIGH ;
my $STEP = $my_main{"DM_PHEXT_STEP"}[0] ;
my $LARGE = 0 ;



if ( length ( $my_main{"DM_PHEXT0"}[0] ) > 0 )
{
    my $NAME = $my_main{"FILE_CELL"}[0] ;
    open ( FILE,  "< $NAME" ) || die "FILE OPEN ERROR: >$my_main{\"FILE_CELL\"}[0]< \n" ;
    my @cell = <FILE> ; close ( FILE ) ;
    my @cell_data = split( " ", $cell[0] ) ;
    for( $I = 0; $I < 3; $I++ ) 
    { if ( $cell_data[$I] > $LARGE ) { $LARGE = $cell_data[$I] ; } }
    $XLOW = $LARGE / $my_main{"DM_PHEXT0"}[0] ;
    $XHIGH = $LARGE / $my_main{"DM_PHEXT1"}[0] ;
    $ITEST = ( $XHIGH - $XLOW ) * $STEP ;
}


if ( $ITEST > 0 )
{
    my $TMP_RESOL ;
    for ( $I = 0 ; $I < $ITEST ; $I++ )
    {
	$TMP_RESOL = $LARGE / ( $XLOW + $I / $STEP ) ;
	$TMP_RESOL = sprintf ( "%5.2f", $TMP_RESOL );
	print MACRO << "END_OF_TEXT" ;
refl key WORK_REFL sele defined .a resol RESOL_MIN $TMP_RESOL end
shell \$MAIN_CONF/create_phase_comb.sh -r RESOL_MIN $TMP_RESOL --doit
set vari RESOL_MAX global = $TMP_RESOL
<dm_loop 0 N_CYCLES

END_OF_TEXT
    }
	printf "DM_LOOP: %2d HKL limits %5d %5d final resolution %5.2f\n", $I, $XLOW+0.5, $XHIGH+0.5, $TMP_RESOL ;
print MACRO "return\n" ;
}
else
{
    print MACRO << "END_OF_TEXT" ;

<dm_loop 0 N_CYCLES

return
END_OF_TEXT
}
close ( MACRO ) ;




