#!/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_DIR_MASK"} ) ) { push ( @{$my_main{"DM_DIR_MASK"}}, "" ) } ; 
if ( !exists( $my_main{"DM_MASK_ATOM"} ) ) { push ( @{$my_main{"DM_MASK_ATOM"}}, "3.5" ) } ; 
if ( !exists( $my_main{"BULK_SOLV_METHOD"} ) ) { push ( @{$my_main{"BULK_SOLV_METHOD"}}, "NONE" ) } ; 
if ( !exists( $my_main{"BULK_SOLV_DENSITY"} ) ) { push ( @{$my_main{"BULK_SOLV_DENSITY"}}, "0.35" ) } ; 
if ( !exists( $my_main{"BULK_SOLV_BVALUE"} ) ) { push ( @{$my_main{"BULK_SOLV_BVALUE"}}, "-65.0" ) } ; 

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

my $MACRO_FILE = "calc_bulk_solv_corr.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 'd' || substr($keyword, 0,9) eq "directory" ) { goto DIRECTORY }
    if ( $keyword eq 'm' || substr($keyword, 0,6) eq "method" ) { goto METHOD }
    if ( $keyword eq 'q' || substr($keyword, 0,7) eq "density" ) { goto DENSITY }
    if ( $keyword eq 'b' || substr($keyword, 0,6) eq "bvalue" ) { goto BVALUE }
    if ( $keyword eq 's' || substr($keyword, 0,4) eq "size" ) { goto SIZE }
    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|--help  prints this message with available options and current status 
  -o|--output      set file name of the created macro [$MACRO_FILE]
  -b|--bvalue      Bvalue for the bulk solvent region [-40 - -80] [$my_main{"BULK_SOLV_BVALUE"}[0]]
  -d|--directory   directory for rotation and translational macros [@{$my_main{"DM_DIR_MASK"}}]
  -m|--method      method for mask generation [NONE/FILE/MODEL/MAP] [$my_main{"BULK_SOLV_METHOD"}[0]]
  -q|--density     density of bulk solvent region [0.25 - 0.40] [$my_main{"BULK_SOLV_DENSITY"}[0]]
  -s|--size        size of atoms (radii) [ $my_main{"DM_MASK_ATOM"}[0]]
     --doit        create the macro 
END_OF_TEXT
    exit ;
OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
BVALUE: @{$my_main{"BULK_SOLV_BVALUE"}} = shift ( @{$commands{$keyword}} ) ; next ;
DIRECTORY: @{$my_main{"DM_DIR_MASK"}} = shift ( @{$commands{$keyword}} ) ; next ;
DENSITY: @{$my_main{"BULK_SOLV_DENSITY"}} = shift ( @{$commands{$keyword}} ) ; next ;
METHOD: @{$my_main{"BULK_SOLV_METHOD"}} = shift ( @{$commands{$keyword}} ) ; next ;
SIZE: @{$my_main{"DM_MASK_ATOM"}} = 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 << "END_OF_TEXT" ;

subroutine int THE_MAP 

make map THE_MAP conv real
make map THE_MAP zero
make map THE_MAP empty

set vari ATOM_RAD = $my_main{"DM_MASK_ATOM"}[0] 
set vari MAP_MASK = 3

END_OF_TEXT

if ( $my_main{"BULK_SOLV_METHOD"}[0] eq "NONE" ) 
{
    print MACRO << "END_OF_TEXT" ;

set vari BULK_SOLVENT global = OFF
return

END_OF_TEXT
exit ;
} 
elsif ( $my_main{"BULK_SOLV_METHOD"}[0] eq "FILE" ) 
{

    my ( $SEGM, $MASK_FILE ) ;

    foreach $SEGM ( @{$my_main{"SG_LIST"}} )
    {
    $MASK_FILE = join ('', $my_main{"DM_DIR_MASK"}[0], 
		       "mask_", $SEGM, ".xmap" ) ;

    print MACRO << "END_OF_TEXT" ;
set vari FILE_MASK = $MASK_FILE
read file FILE_MASK map xpl over MAP_MASK
make map MAP_MASK set 999990 9999999 1.0
make map THE_MAP from MAP_MASK cell

END_OF_TEXT
}
}

    print MACRO << "END_OF_TEXT" ;
make map THE_MAP set 0 2 9999.0 
 make map THE_MAP + 1 from THE_MAP full init copy
make map THE_MAP from THE_MAP + 1 fill 5
make map MAP_MASK set 999990 9999999 1.0
make map THE_MAP set -9999990 -999990 0.0

fourier map THE_MAP 

refl sele defined end \\ 
    set ampl phase const $my_main{"BULK_SOLV_DENSITY"}[0] temp $my_main{"BULK_SOLV_BVALUE"}[0] \\ 
    scale fcalc fpart = fcalc * 1.0

set vari BULK_SOLVENT global = ON

return
END_OF_TEXT

exit ;

    print MACRO << "END_OF_TEXT" ;


<?MAIN_UTILS:make_atom_mask  SEGMENTS THE_MAP ATOM_RAD MAP_MASK  mask.xmap
make map 3 set 999990 9999999 1.0


make map THE_MAP from MAP_MASK cell

! wr over file solv_mod.fcalc refl sele WORK_REFL .a resol 6.6 3.5  end \\
!   fcalc resol


< ?MAIN_CMDS:re_phase.cmds SEGMENTS THE_MAP

refl set ampl phase fcalc = fcalc * 1. - fpart * 1.0  

 refl sele WORK_REFL end r-val 

< MAIN_CMDS:calc_fofc_diff_map.cmds THE_MAP 2.0 1.0 

END_OF_TEXT

