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

#####################################################################
my $NCRYST = $my_main{"SG_NCRYST"}[0] ;
my $CRYST ;

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

if ( !exists( $my_main{"DM_SOLV_TREAT"} ) ) { push ( @{$my_main{"DM_SOLV_TREAT"}}, "ATOMS" ) } ; 
#if ( $my_main{"DM_SOLV_TREAT"} eq "OFF" ) { $my_main{"DM_SOLV_TREAT"} = "ATOMS" } ; 
if ( !exists( $my_main{"DM_SOLV_WAY"} ) ) { push ( @{$my_main{"DM_SOLV_WAY"}}, "FFT" ) } ; 
if ( !exists( $my_main{"DM_SOLV_METHOD"} ) ) { push ( @{$my_main{"DM_SOLV_METHOD"}}, "WANG" ) } ; 
if ( !exists( $my_main{"DM_SOLV_SPHERE"} ) ) { push ( @{$my_main{"DM_SOLV_SPHERE"}}, "0.0" ) } ; 
if ( !exists( $my_main{"DM_SOLV_CONT"} ) ) { push ( @{$my_main{"DM_SOLV_CONT"}}, "0.0" ) } ; 
if ( !exists( $my_main{"DM_SOLV_FUNCT"} ) ) { push ( @{$my_main{"DM_SOLV_FUNCT"}}, "LINEAR" ) } ; 
if ( !exists( $my_main{"DM_SOLV_FUNCT_B"} ) ) { push ( @{$my_main{"DM_SOLV_FUNCT_B"}}, "1.0" ) } ; 
if ( !exists( $my_main{"DM_SOLV_FLIP"} ) ) 
{ 
    for ( $CRYST = 0; $CRYST < $NCRYST ; $CRYST++ )
    { push ( @{$my_main{"DM_SOLV_FLIP"}}, "0.0" ) }
} ; 
if ( !exists( $my_main{"DM_DIR_MASK"} ) ) { push ( @{$my_main{"DM_DIR_MASK"}}, "" ) } ; 
if ( !exists( $my_main{"DM_DIR_RT"} ) ) { push ( @{$my_main{"DM_DIR_RT"}}, "" ) } ; 
if ( !exists( $my_main{"DM_MAP_OUT"} ) ) { push ( @{$my_main{"DM_MAP_OUT"}}, "2FOFC" ) } ; 

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

my $MACRO_FILE = "dm_next.cmds" ;
my $MACRO_FILE = "dm_loop.com" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
    if ( $keyword eq 'b' || substr($keyword, 0,4) eq "bval" ) { goto FUN_BVAL }
    if ( $keyword eq 'c' || substr($keyword, 0,6) eq "cycles" ) { goto CYCLES }
    if ( $keyword eq 'c' || substr($keyword, 0,7) eq "content" ) { goto CONTENT }
    if ( $keyword eq 'e' || substr($keyword, 0,6) eq "extend" ) { goto EXTEND }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "flip" ) { goto FLIP }
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 'm' || substr($keyword, 0,7) eq "map_out" ) { goto MAP_OUT }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 'p' || substr($keyword, 0,9) eq "procedure" ) { goto METHOD }
    if ( $keyword eq 'r' || substr($keyword, 0,6) eq "sphere" ) { goto SPHERE }
    if ( $keyword eq 's' || substr($keyword, 0,4) eq "step" ) { goto STEP }
    if ( $keyword eq 't' || substr($keyword, 0,5) eq "treat" ) { goto TREATMENT }
#    if ( $keyword eq 'w' || substr($keyword, 0,3) eq "way" ) { goto WAY }
    if ( $keyword eq 'x' || substr($keyword, 0,7) eq "express" ) { goto EXPRESSION }
    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]
  -b|--bval)       mask function spread (B-value) [$my_main{"DM_SOLV_FUNCT_B"}[0]]
  -c|--content)    solvent content [@{$my_main{"DM_SOLV_CONT"}}]
  -x|--expression) expression form of protein mask [$my_main{"DM_SOLV_FUNCT"}[0]]
  -p|--procedure)  solvent flattening procedure [WANG/SOLOMON] [$my_main{"DM_SOLV_METHOD"}[0]]
  -m|--map_out)    output kind of map [FOBS/3FO2FC/2FOFC/COMB] [$my_main{"DM_MAP_OUT"}[0]]
  -f|--flip)       solvent flip (flip = value / (1 - value)) [@{$my_main{"DM_SOLV_FLIP"}}]
  -r|--sphere)     solvent flattening sphere radius [@{$my_main{"DM_SOLV_SPHERE"}}]
  -s|--step)    steps of a single reciprocal space lattice point [@{$my_main{"DM_PHEXT_STEP"}}]
  -t|--treat)      mask generation [ATOMS/STATISTICS/BOTH] [$my_main{"DM_SOLV_TREAT"}[0]]
  -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
     --doit)       create the macro 
END_OF_TEXT
    exit ;
#  -w|--way)        solvent flattening way [REAL_SPACE/FFT] [$my_main{"DM_SOLV_WAY"}[0]]

OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
DIRECT: @{$my_main{"DM_DIR"}} = shift ( @{$commands{$keyword}} ) ; next ;
METHOD: @{$my_main{"DM_SOLV_METHOD"}} =  uc( shift ( @{$commands{$keyword}} ) ) ; next ;
SPHERE: @{$my_main{"DM_SOLV_SPHERE"}} = @{$commands{$keyword}} ; next ;
CONTENT: @{$my_main{"DM_SOLV_CONT"}} = @{$commands{$keyword}} ; next ;
EXPRESSION: @{$my_main{"DM_SOLV_FUNCT"}} = shift ( @{$commands{$keyword}} ) ; next ;
FUN_BVAL: @{$my_main{"DM_SOLV_FUNCT_B"}} = shift ( @{$commands{$keyword}} ) ; next ;
TREATMENT: @{$my_main{"DM_SOLV_TREAT"}} = uc( shift ( @{$commands{$keyword}}) ) ; next ;
#WAY: @{$my_main{"DM_SOLV_WAY"}} =  uc( shift ( @{$commands{$keyword}} ) ) ; next ;
FLIP: @{$my_main{"DM_SOLV_FLIP"}} = @{$commands{$keyword}} ; next ;
MAP_OUT: @{$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 ; }

my $VARI = ${@{$my_main{"DM_SOLV_METHOD"}}}[0] ;
my $CALC_WAY = ${@{$my_main{"DM_SOLV_WAY"}}}[0] ;
my $FL_MASK_SOLVENT=0 ; 
my $FL_MASK_ATOMS=0 ; 
if ( ${@{$my_main{"DM_SOLV_TREAT"}}}[0] =~ "STATIST" || 
     ${@{$my_main{"DM_SOLV_TREAT"}}}[0] =~ "BOTH" ) { $FL_MASK_SOLVENT= 1 ; }
if ( ${@{$my_main{"DM_SOLV_TREAT"}}}[0] =~ "ATOM" || 
     ${@{$my_main{"DM_SOLV_TREAT"}}}[0] =~ "BOTH" ) { $FL_MASK_ATOMS= 1 ; }


if ( $FL_MASK_SOLVENT > 0 ) 
{
my @METHOD = grep ( m/$VARI/, ("WANG", "SOLOMON" ) ) ;
if ( @METHOD <= 0 ) 
{ print "$MACRO_FILE %ERROR: no solvent accounting method specified ?? [@{$my_main{\"DM_SOLV_METHOD\"}}] \n"; exit }
my @TEST = grep ( m/$CALC_WAY/, ("REAL_SPACE", "FFT" ) ) ;
if ( @TEST <= 0 ) 
{ print "$MACRO_FILE %ERROR: no calcuation way specified ?? [@{$my_main{\"DM_SOLV_WAY\"}}] \n"; exit }
}

##################################################################################
#
#   dm_next.com
#
#****************************************************************************

system( "create_dm_next.pl --doit" ) ;

##################################################################################
#
#
#   dm_loop.com
#
#################################################################################


system( "create_dm_loop.pl --doit" ) ;

