#!/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"}}  , "DENSITY" ) ; }
if ( !exists($my_main{"MAP_CTRL_STEP_DENSITY"}  ) ) { push ( @{$my_main{"MAP_CTRL_STEP_DENSITY"}}  , 0.1 ) ; }
if ( !exists($my_main{"MAP_CTRL_STEP_BOX"}  ) )     { push ( @{$my_main{"MAP_CTRL_STEP_BOX"}}  , 5 ) ; }
if ( !exists($my_main{"MAP_CTRL_STEP_VOLUME"}  ) )  { push ( @{$my_main{"MAP_CTRL_STEP_VOLUME"}}  , 0.01 ) ; }
if ( !exists($my_main{"MAP_CTRL_STEP_COLOR"}  ) )   { push ( @{$my_main{"MAP_CTRL_STEP_COLOR"}}  , 2 ) ; }


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

my $MACRO_FILE = "image_map_scroll.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 's' || substr($keyword, 0,6) eq "scroll" ) { goto SCROLL }
    if ( $keyword eq 'd' || substr($keyword, 0,7) eq "density" ) { goto DENSITY }
    if ( $keyword eq 'b' || substr($keyword, 0,3) eq "box" ) { goto BOX }
    if ( $keyword eq 'v' || substr($keyword, 0,6) eq "volume" ) { goto VOLUME }
    if ( $keyword eq 'c' || 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 
-s|--scroll)      scroll  [DENSITY/VOLUME/BOX/COLOR/SCORE_MAP] [@{$my_main{"MAP_CTRL_SCROLL"}}]
-d|--density)     step density [@{$my_main{"MAP_CTRL_STEP_DENSITY"}}]
-b|--box)         step box [@{$my_main{"MAP_CTRL_STEP_BOX"}}]
-v|--volume)      step volume [@{$my_main{"MAP_CTRL_STEP_VOLUME"}}]
-c|--color)       step color [@{$my_main{"MAP_CTRL_STEP_COLOR"}}]
   --doit)        create the macro
END_OF_TEXT
    exit ;

  SCROLL:  @{$my_main{"MAP_CTRL_SCROLL"}} = shift ( @{$commands{$keyword}} ) ; next ;
  DENSITY:  @{$my_main{"MAP_CTRL_STEP_DENSITY"}} = shift ( @{$commands{$keyword}} ) ; next ;
  BOX:  @{$my_main{"MAP_CTRL_STEP_BOX"}} = shift ( @{$commands{$keyword}} ) ; next ;
  VOLUME:  @{$my_main{"MAP_CTRL_STEP_VOLUME"}} = shift ( @{$commands{$keyword}} ) ; next ;
  COLOR:  @{$my_main{"MAP_CTRL_STEP_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_SCROLL ;

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

$MAP_SCROLL = ${$my_main{"MAP_CTRL_SCROLL"}}[0] ;
my $MAP_DENS = ${$my_main{"MAP_CTRL_STEP_DENSITY"}}[0] ;
my $MAP_BOX = ${$my_main{"MAP_CTRL_STEP_BOX"}}[0] ;
my $MAP_VOL = ${$my_main{"MAP_CTRL_STEP_VOLUME"}}[0] ;
my $MAP_COL = ${$my_main{"MAP_CTRL_STEP_COLOR"}}[0] ;

# tasks

if ( $MAP_SCROLL eq "DENSITY" )
{

print MACRO << "END_OF_TEXT" ;
subroutine real MAP_DIR
set vari STEP = MAP_DIR * $MAP_DENS
set vari MAP_CONT global real = MAP_CONT + STEP

image map MAP_ACT dens MAP_CONT ! box map_boxx map_boxy map_boxz
return
END_OF_TEXT
}

if ( $MAP_SCROLL eq "BOX" )
{
print MACRO << "END_OF_TEXT" ;
subroutine int MAP_DIR
! show vari MAP_DIR
set vari map_boxx global = MAP_DIR * $MAP_BOX + map_boxx  
!set vari map_boxy global = map_boxy + MAP_DIR * $MAP_BOX
!set vari map_boxz global = map_boxz + MAP_DIR * $MAP_BOX
if ( map_boxx .gt. 150 ) return
 show vari map_boxx

image map MAP_ACT  box map_boxx map_boxx map_boxx
return
END_OF_TEXT
}

if ( $MAP_SCROLL eq "VOLUME" )
{
print MACRO << "END_OF_TEXT" ;
subroutine int MAP_DIR
! show vari MAP_DIR
set vari MAP_VOLUME global = MAP_DIR * $MAP_VOL + MAP_VOLUME 
image map MAP_ACT volume MAP_VOLUME
return
END_OF_TEXT
}

if ( $MAP_SCROLL eq "COLOR" )
{
print MACRO << "END_OF_TEXT" ;
subroutine int MAP_DIR
set vari MAP_DIR  = MAP_DIR * 2
set vari MAP_COLOR int  global = MAP_DIR * $MAP_COL + MAP_COLOR 
if ( MAP_COLOR .gt. 255 ) set vari MAP_COLOR int  global = 64
if ( MAP_COLOR .lt. 64 ) set vari MAP_COLOR int  global = 255
! show vari MAP_*
image col MAP_COLOR map MAP_ACT dens MAP_CONT
return
END_OF_TEXT
}

close (MACRO ) ;
exit ;
