#!/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_SOLV_METHOD"} ) )  { push ( @{$my_main{"DM_SOLV_METHOD"}} , "WANG" ) };
if ( !exists($my_main{"DM_SOLV_WAY"} ) )  { push ( @{$my_main{"DM_SOLV_WAY"}} , "FFT" ) };
if ( !exists($my_main{"DM_SOLV_SPHERE"} ) )  { push ( @{$my_main{"DM_SOLV_SPHERE"}} , 10.0 ) };
if ( !exists($my_main{"DM_SOLV_CONTENT"}) )  { push ( @{$my_main{"DM_SOLV_CONTENT"}} , 0.93 ) };
if ( !exists($my_main{"DM_SOLV_INP"}  ) )  { push ( @{$my_main{"DM_SOLV_INP"}} , 1 ) };
if ( !exists($my_main{"DM_SOLV_RESULT"}  ) )  { push ( @{$my_main{"DM_SOLV_RESULT"}} , 3 ) };

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

my $ MACRO_FILE="map_mask_score_map.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,4) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 'm' || substr($keyword, 0,6) eq "method" ) { goto METHOD }
    if ( $keyword eq 'c' || substr($keyword, 0,4) eq "content" ) { goto CONTENT }
    if ( $keyword eq 'i' || substr($keyword, 0,5) eq "input"  ) { goto INPUT }
    if ( $keyword eq 'r' || substr($keyword, 0,6) eq "result" ) { goto RESULT }
    if ( $keyword eq 's' || substr($keyword, 0,6) eq "sphere" ) { goto SPHERE }
    if ( $keyword eq 'w' || substr($keyword, 0,3) eq "way" ) { goto WAY }
    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 
-c|--content  solvent content [@{$my_main{"DM_SOLV_CONTENT"}}]
-i|--input)   input density map [@{$my_main{"DM_SOLV_INP"}}]
-m|--method)  solvent flattening method [WANG/SOLOMON] [@{$my_main{"DM_SOLV_METHOD"}}]
-r|--result)  resulting score map [@{$my_main{"DM_SOLV_RESULT"}}]
-s|--sphere)  solvent flattening sphere radius [@{$my_main{"DM_SOLV_SPHERE"}}]
-w|--way)     calculation way [REAL_SPACE/FFT] [@{$my_main{"DM_SOLV_WAY"}}]
-o|--output)  set file name of the created macro [$MACRO_FILE]
   --doit)    create the macro 
END_OF_TEXT
    exit ;
  OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;

  CONTENT: @{$my_main{"DM_SOLV_CONTENT"}} = shift ( @{$commands{$keyword}} ) ; next ;
  INPUT:   @{$my_main{"DM_SOLV_INP"}} = shift ( @{$commands{$keyword}} ) ; next ;
  METHOD:  @{$my_main{"DM_SOLV_METHOD"}} = uc( shift ( @{$commands{$keyword}} )) ; next ;
  RESULT:  @{$my_main{"DM_SOLV_RESULT"}} = shift ( @{$commands{$keyword}} ) ; next ;
  SPHERE:  @{$my_main{"DM_SOLV_SPHERE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  WAY:  @{$my_main{"DM_SOLV_WAY"}} = uc( 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 ) ; 

###  . ${MAIN_CONF}main_config.data.save.sh

if ( $doit eq "FALSE" )  { exit ; }

my $VARI = ${@{$my_main{"DM_SOLV_METHOD"}}}[0] ;
my $CALC_WAY = ${@{$my_main{"DM_SOLV_WAY"}}}[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 }


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

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" ;
set vari MAP_FROM = @{$my_main{"DM_SOLV_INP"}}
set vari MAP_WORK = @{$my_main{"DM_SOLV_RESULT"}}\n

! uncomment the following lines after the segment SKEL
! is approximately in the position of your assymetric unit

! SKEL = segment name to a model 
!      positioned in your asymmetric unit
! CUSHION = number grids of map around your SKEL that will
!         be skeletonized\n

! set vari MAP_DENS = MAP_FROM + 1 
! set vari CUSHION = 15
! make map MAP_DENS from MAP_FROM init 9999 \\ 
!      around CUSHION sele segm name SKEL end 
! make map MAP_DENS from MAP_ORIG copy 
! set vari MAP_FROM = MAP_DENS
END_OF_TEXT

if ( $VARI eq "WANG" ) 
{ 
    if ( $CALC_WAY eq "FFT" ) 
    { print MACRO "<?MAIN_UTILS:score_map_wang_fft MAP_FROM MAP_WORK @{$my_main{\"DM_SOLV_SPHERE\"}} \n" ; }
    elsif ( $CALC_WAY eq "REAL_SPACE" ) 
    {print MACRO "<?MAIN_UTILS:score_map_wang_realspace.com MAP_FROM MAP_WORK @{$my_main{\"DM_SOLV_SPHERE\"}}\n" ; }
}
elsif ( ${@{$my_main{"DM_SOLV_METHOD"}}}[0] eq "SOLOMON" ) 
{ 
    if ( $CALC_WAY eq "FFT" ) 
    { print MACRO "<?MAIN_UTILS:score_map_solomon_fft MAP_FROM MAP_WORK @{$my_main{\"DM_SOLV_SPHERE\"}} \n" ; }
    elsif ( $CALC_WAY eq "REAL_SPACE" ) 
    { print MACRO "<?MAIN_UTILS:score_map_solomon_realspace MAP_FROM MAP_WORK @{$my_main{\"DM_SOLV_SPHERE\"}} \n" ; }
}
else 
{ 
print MACRO << "END_OF_TEXT" ;
create_score_map.pl> error IN CALCULATION WAY 
return

END_OF_TEXT
print " create_score_map.pl> error IN CALCULATION WAY \n" ;
}

print MACRO "anal ini \n" ;
print MACRO "anal map MAP_WORK step 256 \n" ;
print MACRO "show anal = @{$my_main{\"DM_SOLV_CONTENT\"}} \n" ;
print MACRO "image map MAP_WORK dens RESULT_0 map_boxx map_boxy map_boxz \n" ;
print MACRO "return\n" ;

close(MACRO) ;
