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

#print " map kicked random: {", @{$my_main{"MAP_KICK_SERIES"}}, "}\n" ;
#####################################################################

if ( !exists($my_main{"IMAGE_MAP_MASK_BY"}  ) )        { push ( @{$my_main{"IMAGE_MAP_MASK_BY"}}  , "GRADIENT" ) ; }
if ( !exists($my_main{"IMAGE_MAP_MASK_INPUT"}  ) )    { push ( @{$my_main{"IMAGE_MAP_MASK_INPUT"}}  , 1 ) ; }
if ( !exists($my_main{"IMAGE_MAP_MASK_OUTPUT"}  ) )    { push ( @{$my_main{"IMAGE_MAP_MASK_OUTPUT"}}  , 2 ) ; }
if ( !exists($my_main{"IMAGE_MAP_MASK_CONTOUR"}  ) )   { push ( @{$my_main{"IMAGE_MAP_MASK_CONTOUR"}}  , 1 ) ; }
if ( !exists($my_main{"IMAGE_MAP_MASK_CUSHION"}  ) )   { push ( @{$my_main{"IMAGE_MAP_MASK_CUSHION"}}  , 8 ) ; }
if ( !exists($my_main{"IMAGE_MAP_MASK_SELECTION"}  ) ) { push ( @{$my_main{"IMAGE_MAP_MASK_SELECTION"}}  , "active" ) ; }
if ( !exists($my_main{"IMAGE_MAP_MASK_EXCLUDE"}  ) ) { push ( @{$my_main{"IMAGE_MAP_MASK_EXCLUDE"}}  , ".not. all" ) ; }
if ( !exists($my_main{"IMAGE_MAP_MASK_RADIUS"}  ) )    { push ( @{$my_main{"IMAGE_MAP_MASK_RADIUS"}}  , 1.6 ) ; }
if ( !exists($my_main{"IMAGE_MAP_MASK_BOX"}  ) )    { push ( @{$my_main{"IMAGE_MAP_MASK_BOX"}}  , 20 ) ; }
if ( !exists($my_main{"IMAGE_MAP_MASK_LABEL"}  ) )    { push ( @{$my_main{"IMAGE_MAP_MASK_LABEL"}}  , "MASK" ) ; }

######################################################
#print " map after setup kicked random: {", @{$my_main{"MAP_KICK_SERIES"}}, "}\n" ;

my $MACRO_FILE = "image_map_mask.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
#    print "option >$keyword<\n" ; 
    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 'i' || substr($keyword, 0,5) eq "input" ) { goto INPUT }
    if ( $keyword eq 'c' || substr($keyword, 0,7) eq "contour" ) { goto CONTOUR }
    if ( $keyword eq 'u' || substr($keyword, 0,7) eq "cushion" ) { goto CUSHION }
    if ( $keyword eq 'b' || substr($keyword, 0,3) eq "box" ) { goto BOX }
    if ( $keyword eq 's' || substr($keyword, 0,9) eq "selection" ) { goto SELECTION }
    if ( $keyword eq 'e' || substr($keyword, 0,7) eq "exclude" ) { goto EXCLUDE }
    if ( $keyword eq 'b' || substr($keyword, 0,2) eq "by" ) { goto BY }
    if ( $keyword eq 'r' || substr($keyword, 0,6) eq "radius" ) { goto RADIUS }
    if ( $keyword eq 'l' || substr($keyword, 0,5) eq "label" ) { goto LABEL }

    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 their current status 
-b|--by)          make mask by [GRADIENT/RADII/AUTO_RAD] [@{$my_main{"IMAGE_MAP_MASK_BY"}}]
-i|--input)       input  MAP number [1/2/3/4/5/6/7/8/9/10] [@{$my_main{"IMAGE_MAP_MASK_INPUT"}}]
-o|--output)      output MAP number [1/2/3/4/5/6/7/8/9/10] [@{$my_main{"IMAGE_MAP_MASK_OUTPUT"}}]
-r|--radius)      mask atom radious [@{$my_main{"IMAGE_MAP_MASK_RADIUS"}}]
-u|--cushion)     cushion grid of target map [@{$my_main{"IMAGE_MAP_MASK_CUSHION"}}]
-c|--contour)     contour target map at [@{$my_main{"IMAGE_MAP_MASK_CONTOUR"}}]
-x|--box)         map displayed box size [@{$my_main{"IMAGE_MAP_MASK_BOX"}}]
-l|--label)       map label [@{$my_main{"IMAGE_MAP_MASK_LABEL"}}]
-s|--selection)   mask in selection [@{$my_main{"IMAGE_MAP_MASK_SELECTION"}}]
-e|--exclude)     mask excluded selection [@{$my_main{"IMAGE_MAP_MASK_EXCLUDE"}}]
   --doit)        create the macro
END_OF_TEXT
    exit ;

  BY:   @{$my_main{"IMAGE_MAP_MASK_BY"}} = shift ( @{$commands{$keyword}} ) ; next ;
  OUTPUT:  @{$my_main{"IMAGE_MAP_MASK_OUTPUT"}} = shift ( @{$commands{$keyword}} ) ; next ;
  INPUT:  @{$my_main{"IMAGE_MAP_MASK_INPUT"}} = shift ( @{$commands{$keyword}} ) ; next ;
  CONTOUR:  @{$my_main{"IMAGE_MAP_MASK_CONTOUR"}} = shift ( @{$commands{$keyword}} ) ; next ;
  CUSHION:  @{$my_main{"IMAGE_MAP_MASK_CUSHION"}} = shift ( @{$commands{$keyword}} ) ; next ;
  SELECTION:  @{$my_main{"IMAGE_MAP_MASK_SELECTION"}} = join (" ", @{$commands{$keyword}} ) ; next ;
  EXCLUDE:  @{$my_main{"IMAGE_MAP_MASK_EXCLUDE"}} = join (" ", @{$commands{$keyword}} ) ; next ;
  RADIUS:  @{$my_main{"IMAGE_MAP_MASK_RADIUS"}} = join (" ", @{$commands{$keyword}} ) ; next ;
  BOX:  @{$my_main{"IMAGE_MAP_MASK_BOX"}} = join (" ", @{$commands{$keyword}} ) ; next ;
  LABEL:  @{$my_main{"IMAGE_MAP_MASK_LABEL"}} = join (" ", @{$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 ; }

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

print "creating \"$MACRO_FILE\" \n" ;
if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}
my $TO_MAP = $my_main{"IMAGE_MAP_MASK_OUTPUT"}[0];
my $FROM_MAP = $my_main{"IMAGE_MAP_MASK_INPUT"}[0];
my $MY_CUSHION = $my_main{"IMAGE_MAP_MASK_CUSHION"}[0];
my $MY_CONTOUR = $my_main{"IMAGE_MAP_MASK_CONTOUR"}[0];
my $MY_SELECTION = join(" ", @{$my_main{"IMAGE_MAP_MASK_SELECTION"}} ) ;
my $MY_EXCLUDE = join(" ", @{$my_main{"IMAGE_MAP_MASK_EXCLUDE"}} ) ;
my $MASK_RADIUS = $my_main{"IMAGE_MAP_MASK_RADIUS"}[0];
my $MY_BOX = $my_main{"IMAGE_MAP_MASK_BOX"}[0];
my $MAP_LABEL = ${$my_main{"IMAGE_MAP_MASK_LABEL"}}[0] ;

open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ;

if ( ${$my_main{"IMAGE_MAP_MASK_BY"}}[0] eq "GRADIENT" )
{
print MACRO << "END_OF_TEXT" ;
set lev 0
set vari MAP_ENVEL = $TO_MAP
set vari CUT_OFF = -1.0
set vari MAP_OF_ORI = $FROM_MAP

! -------- not to be edited --------------

make map $TO_MAP from MAP_OF_ORI around $MY_CUSHION sele $MY_SELECTION end \\
     full init real
make map from MAP_OF_ORI around $MY_CUSHION sele $MY_SELECTION end init integer 
show map
if ( MAP_LAST .lt. 0 ) return
set vari MAP_ENVEL2 int = MAP_LAST
show map MAP_LAST

make map MAP_ENVEL2 from MAP_OF_ORI atom ( index  CUT_OFF 999990. )
make map MAP_ENVEL from MAP_ENVEL2 copy
make map MAP_ENVEL set 1 1000000 1999999
make map MAP_ENVEL set -1000000 0 0
make map MAP_ENVEL from MAP_OF_ORI copy
 delete map MAP_ENVEL2
image map MAP_ENVEL label $MAP_LABEL
image map MAP_ENVEL dens $MY_CONTOUR  $MY_BOX $MY_BOX $MY_BOX
return
END_OF_TEXT
}

if ( ${$my_main{"IMAGE_MAP_MASK_BY"}}[0] eq "RADII" )
{
print MACRO << "END_OF_TEXT" ;
make map $TO_MAP from $FROM_MAP around 8 sele $MY_SELECTION end init empty
make map $TO_MAP from $FROM_MAP atom mask $MASK_RADIUS sele $MY_SELECTION end
make map $TO_MAP from $FROM_MAP copy
make map $TO_MAP from $FROM_MAP atom mask $MASK_RADIUS * 0.8 sele $MY_EXCLUDE end
 make map $TO_MAP set 99 10000000 0
 make map $TO_MAP set -10000000 -10 0
image map $TO_MAP dens $MY_CONTOUR  $MY_BOX $MY_BOX $MY_BOX

return
END_OF_TEXT
}

if ( ${$my_main{"IMAGE_MAP_MASK_BY"}}[0] eq "AUTO_RAD" )
{
print MACRO << "END_OF_TEXT" ;
key MY_SELECTION sele $MY_SELECTION end
set rad sele MY_SELECTION end sele $MY_EXCLUDE end overlap 1.5 $MASK_RADIUS
make map $TO_MAP from $FROM_MAP around 8 sele MY_SELECTION end init empty
make map $TO_MAP from $FROM_MAP atom mask $MASK_RADIUS sele MY_SELECTION end
make map $TO_MAP from $FROM_MAP copy
make map $TO_MAP from $FROM_MAP atom mask $MASK_RADIUS * 0.8 sele $MY_EXCLUDE end
 make map $TO_MAP set 99 10000000 0
 make map $TO_MAP set -10000000 -10 0
image map $TO_MAP dens $MY_CONTOUR $MY_BOX $MY_BOX $MY_BOX
key MY_SELECTION drop
return
END_OF_TEXT
}


close (MACRO ) ;
exit ;
