#!/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 %color_list = ("WHITE"=> 32, "RED"=>128, "ORANGE"=>144,"YELLOW"=>160,  "GREEN"=>196, "CYAN"=>220, "BLUE"=>244, "AS_IS"=>-1 ) ;


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

if ( !exists($my_main{"MAP_CTRL_SCROLL"}  ) )  { push ( @{$my_main{"MAP_CTRL_SCROLL"}}  , "" ) ; }
if ( !exists($my_main{"MAP_CTRL_TARGET"}  ) )  { push ( @{$my_main{"MAP_CTRL_TARGET"}}, 1 ) ; } 
if ( !exists($my_main{"MAP_CTRL_CONTOUR"}  ) )  { push ( @{$my_main{"MAP_CTRL_CONTOUR"}}  , 1 ) ; }
if ( !exists($my_main{"MAP_CTRL_BOX"}  ) )  { push ( @{$my_main{"MAP_CTRL_BOX"}}  , 25 ) ; }
if ( !exists($my_main{"MAP_CTRL_LEVEL"}  ) )  { push ( @{$my_main{"MAP_CTRL_LEVEL"}}  , "CONTOUR" ) ; }
if ( !exists($my_main{"MAP_CTRL_COLOR"}  ) )  { push ( @{$my_main{"MAP_CTRL_COLOR"}}  , "CYAN" ) ; }
if ( !exists($my_main{"MAP_CTRL_STYLE"}  ) )  { push ( @{$my_main{"MAP_CTRL_STYLE"}}  , "LINES" ) ; }


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

my $MACRO_FILE = "map_ctrl.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 't' || substr($keyword, 0,6) eq "target" ) { goto TARGET }
    if ( $keyword eq 's' || substr($keyword, 0,6) eq "scroll" ) { goto SCROLL }
    if ( $keyword eq 'y' || substr($keyword, 0,5) eq "style" ) { goto STYLE }
    if ( $keyword eq 'v' || substr($keyword, 0,6) eq "volume" ) { goto VOLUME }
    if ( $keyword eq 'c' || substr($keyword, 0,7) eq "contour" ) { goto CONTOUR }
    if ( $keyword eq 'b' || substr($keyword, 0,3) eq "box" ) { goto BOX }
    if ( $keyword eq 'd' || substr($keyword, 0,7) eq "density" ) { goto DENSITY }
    if ( $keyword eq 'r' || substr($keyword, 0,5) eq "color" ) { goto COLOR }

    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 
-t|--target)      target MAP number [1/2/3/4/5/6/7/8/9/10] [@{$my_main{"MAP_STRL_TARGET"}}]
-y|--style)       style of presentation [LINE/STICK/X-AX/Y-AX/Z-AX/SURF] [@{$my_main{"MAP_CTRL_STYLE"}}]
-c|--contour)     contour at [DENISTY/VOLUME] [@{$my_main{"MAP_CTRL_CONTOUR"}}]
-d|--density)     density contouri [@{$my_main{"MAP_CTRL_DENSITY"}}]
-v|--volume)      contour at volume portion [0..1] [@{$my_main{"MAP_CTRL_VOLUME"}}]
-b|--box)         box of the target map [@{$my_main{"MAP_CTRL_BOX"}}]
-r|--color)       color of the target map [RED/ORANGE/YELLOW/GREEN/CYAN/BLUE/WHITE] [@{$my_main{"MAP_CTRL_COLOR"}}]
-s|--scroll)      scroll  [DENSITY/VOLUME/BOX/COLOR] [@{$my_main{"MAP_CTRL_SCROLL"}}]
   --doit)        create the macro
END_OF_TEXT
    exit ;

  TARGET:  @{$my_main{"MAP_CTRL_TARGET"}} = shift ( @{$commands{$keyword}} ) ; next ;
  SCROLL:  @{$my_main{"MAP_CTRL_SCROLL"}} = shift ( @{$commands{$keyword}} ) ; next ;
  STYLE:  @{$my_main{"MAP_CTRL_STYLE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  CONTOUR:  @{$my_main{"MAP_CTRL_CONTOUR"}} = shift ( @{$commands{$keyword}} ) ; next ;
  VOLUME:  @{$my_main{"MAP_CTRL_VOLUME"}} = join (" ", @{$commands{$keyword}} ) ; next ;
  DENSITY:  @{$my_main{"MAP_CTRL_DENSITY"}} = shift ( @{$commands{$keyword}} ) ; next ;
  BOX:  @{$my_main{"MAP_CTRL_BOX"}} = shift ( @{$commands{$keyword}} ) ; next ;
  COLOR:  @{$my_main{"MAP_CTRL_COLOR"}} = 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 ; }

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

print "creating \"$MACRO_FILE\" \n" ;
if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}
my $MAP_TARGET ;
my $MAP_SCROLL ;
my $MAP_SYTLE;
my $MAP_DENSITY ;
my $MAP_VOLUME ;
my $MAP_CONTOUR ;
my $MAP_BOX ; 
my $MAP_COLOR;
open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ;

$MAP_TARGET = ${$my_main{"MAP_CTRL_TARGET"}}[0] ;
$MAP_STYLE = ${$my_main{"MAP_CTRL_SYTLE"}}[0] ;
$MAP_CONTOUR =  ${$my_main{"MAP_CTRL_CONTOUR"}}[0] ;
$MAP_VOLUME = ${$my_main{"MAP_CTRL_VOLUME"}}[0] ;
$MAP_DENSITY = ${$my_main{"MAP_CTRL_DENSITY"}}[0] ;
$MAP_COLOR = ${$my_main{"MAP_CTRL_COLOR"}}[0] ;
$MAP_BOX =  ${$my_main{"MAP_CTRL_BOX"}}[0] ;


# tasks

if ( ${$my_main{"MAP_CTRL_TASK"}}[0] eq "COPY" )
{
print MACRO << "END_OF_TEXT" ;
make map $TO_MAP from $FROM_MAP full init copy
image $color_text map $TO_MAP dens $CONTOUR $BOX $BOX $BOX
return
END_OF_TEXT
}

if ( ${$my_main{"MAP_CTRL_TASK"}}[0] eq "MASK_IN" )
{
print MACRO << "END_OF_TEXT" ;
make map  $TO_MAP from $FROM_MAP empty init 
make map $TO_MAP atom ( mask $MASK_RADIUS ) sele $MY_SELECTION end
make map  $TO_MAP from $FROM_MAP copy
image $color_text map $TO_MAP dens $CONTOUR $BOX $BOX $BOX
return
END_OF_TEXT
}

if ( ${$my_main{"MAP_CTRL_TASK"}}[0] eq "MASK_OUT" )
{
print MACRO << "END_OF_TEXT" ;
make map  $TO_MAP from $FROM_MAP empty init 
make map $TO_MAP atom ( mask  $MASK_RADIUS ) sele $MY_SELECTION end
make map  $TO_MAP set 999990 999999 999
make map $TO_MAP scale -1.0
make map  $TO_MAP set 900 1111 999999
make map  $TO_MAP from $FROM_MAP copy
image $color_text map $TO_MAP dens $CONTOUR $BOX $BOX $BOX
return
END_OF_TEXT
}

if ( ${$my_main{"MAP_CTRL_TASK"}}[0] eq "READ" )
{
print MACRO << "END_OF_TEXT" ;
read file $MAP_CTRL_FILE map xpl over $TO_MAP 
image $color_text map $TO_MAP dens $CONTOUR $BOX $BOX $BOX
return
END_OF_TEXT
}
if ( ${$my_main{"MAP_CTRL_TASK"}}[0] eq "SAVE_AS" )
{
print MACRO << "END_OF_TEXT" ;
write over file $MAP_CTRL_FILE map $FROM_MAP xpl
return
END_OF_TEXT
}

close (MACRO ) ;
exit ;
