#!/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{"PLOT_BACKGROUND"} ) )  { push ( @{$my_main{"PLOT_BACKGROUND"}}, "WHITE" ) };
if ( !exists($my_main{"PLOT_DIMENSION"} ) ) { @{$my_main{"PLOT_DIMENSION"}} = (1000,1000 ) };
if ( !exists($my_main{"PLOT_RENDER"} ) ) { push ( @{$my_main{"PLOT_RENDER"}}, "POVRAY" ) };
if ( !exists($my_main{"PLOT_SERIES"} ) )  { push ( @{$my_main{"PLOT_SERIES"}}, "MONO" ) };
if ( !exists($my_main{"PLOT_SERIES_ANGLE"} ) )  { push ( @{$my_main{"PLOT_SERIES_ANGLE"}}, 3.0 ) };
if ( !exists($my_main{"PLOT_SERIES_NUMBER"} ) )  { push ( @{$my_main{"PLOT_SERIES_NUMBER"}}, 1 ) };
if ( !exists($my_main{"PLOT_MODEL"} ) )  { push ( @{$my_main{"PLOT_MODEL"}}, "ON" ) };
if ( !exists($my_main{"PLOT_MAPS"} ) )   { push ( @{$my_main{"PLOT_MAPS"}}, "ON" ) };
if ( !exists($my_main{"PLOT_FILE"} ) )   { push ( @{$my_main{"PLOT_FILE"}}, "" ) };
if ( !exists($my_main{"PLOT_SAVE"} ) )   { push ( @{$my_main{"PLOT_SAVE"}}, "image_save.pov" ) };
if ( !exists($my_main{"PLOT_VIEW"} ) )   { push ( @{$my_main{"PLOT_VIEW"}}, "SAVE_FILE.VIEW" ) };

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

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

my $YCOM = "";

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 'b' || substr($keyword, 0,10) eq "background" ) { goto BACKGROUND }
    if ( $keyword eq 'd' || substr($keyword, 0,9) eq "dimension" ) { goto DIMENSION }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "file" ) { goto FILE }
    if ( $keyword eq 'i' || substr($keyword, 0,5) eq "image" ) { goto IMAGE }
    if ( $keyword eq 'm' || substr($keyword, 0,4) eq "maps" ) { goto MAPS }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 'r' || substr($keyword, 0,6) eq "render" ) { goto RENDER }
    if ( $keyword eq 's' || substr($keyword, 0,4) eq "save" ) { goto SAVE }
    if ( $keyword eq 'x' || substr($keyword, 0,6) eq "series" ) { goto SERIES }
    if ( $keyword eq 'a' || substr($keyword, 0,5) eq "angle" ) { goto ANGLE }
    if ( $keyword eq 'n' || substr($keyword, 0,6) eq "number" ) { goto NUMBER }
    if ( $keyword eq 'y' || substr($keyword, 0,4) eq "ycom" ) { goto YCOM }
     if ( $keyword eq 'v' || substr($keyword, 0,4) eq "view" ) { goto VIEW }
    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 
 -o|--output)     set file name of the created macro [$MACRO_FILE]
 -b|--background) background of the rendered image [WHITE/BLACK] [${$my_main{"PLOT_BACKGROUND"}}[0]]
 -d|--dimension)  dimension of the rendered
 image [${$my_main{"PLOT_DIMENSION"}}[0] - ${$my_main{"PLOT_DIMENSION"}}[0]]
 -f|--file)       use file instead of current foreground image [@{$my_main{"PLOT_FILE"}}]
 -i|--image)      molecular model image [ON/OFF/RE_IMAGE] [@{$my_main{"PLOT_MODEL"}}]
 -m|--maps)       map image [ON/OFF] [@{$my_main{"PLOT_MAPS"}}]
 -r|--render)     rendering program [POVRAY/RASTER3D/LWPLOT] [@{$my_main{"PLOT_RENDER"}}]
 -s|--save)       render output save file [@{$my_main{"PLOT_SAVE"}}]
 -x|--series)     series of images [MONO/STEREO/SERIES] [@{$my_main{"PLOT_SERIES"}}]
 -a|--angle)      angle for series (stereo left and right of the middle) [@{$my_main{"PLOT_SERIES_ANGLE"}}]
 -n|--number)     number of images [@{$my_main{"PLOT_SERIES_NUMBER"}}]
 -v|--view)       view file [@{$my_main{"PLOT_VIEW"}}]
    --doit)    creates the macro
END_OF_TEXT

    exit ;
  BACKGROUND:   @{$my_main{"PLOT_BACKGROUND"}} = uc(shift ( @{$commands{$keyword}}) ) ; next ;
 DIMENSION:   
    @{$my_main{"PLOT_DIMENSION"}} = shift ( @{$commands{$keyword}} ) ; 
    push ( @{$my_main{"PLOT_DIMENSION"}}, shift ( @{$commands{$keyword}} )) ; 
    next ;
  FILE:   @{$my_main{"PLOT_FILE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  IMAGE:  @{$my_main{"PLOT_MODEL"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  MAPS:   @{$my_main{"PLOT_MAPS"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  RENDER: @{$my_main{"PLOT_RENDER"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  SAVE:   @{$my_main{"PLOT_SAVE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  SERIES:   @{$my_main{"PLOT_SERIES"}} = shift ( @{$commands{$keyword}} ) ; next ;
  ANGLE:   @{$my_main{"PLOT_SERIES_ANGLE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  NUMBER:   @{$my_main{"PLOT_SERIES_NUMBER"}} = shift ( @{$commands{$keyword}} ) ; next ;
  VIEW:   @{$my_main{"PLOT_VIEW"}} = shift ( @{$commands{$keyword}} ) ; next ;
  YCOM:   $YCOM = shift ( @{$commands{$keyword}} ) ; next ;
  OUTPUT: $MACRO_FILE = 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" ;

my $file_image = $my_main{"PLOT_FILE"}[0] ;
my $file_save = $my_main{"PLOT_SAVE"}[0] ;
my $file_view = $my_main{"PLOT_VIEW"}[0] ;
my $method = $my_main{"PLOT_RENDER"}[0] ;
my $background_col = 0 ;
if ( $my_main{"PLOT_BACKGROUND"}[0] eq "WHITE" ) { $background_col = 1 ; }

my $file ;
my @file_list = () ;
my @xx = split('\.', $file_save) ;
# print $file_save, "xx:", @xx, "\n" ;
my $file_ext = pop(@xx) ;
my $file_base = join('', @xx ) ;
if ( $my_main{"PLOT_RENDER"}[0] eq "POVRAY" ) { $file_ext = "pov" ; }
if ( $my_main{"PLOT_RENDER"}[0] eq "RASTER3D" ) { $file_ext = "r3d" ; }


print MACRO << "END_OF_TEXT" ;
subroutine char SEGMENTS

END_OF_TEXT

if ( $file_view ne "" ) { print MACRO "< $file_view\n" ; }
my $plot_x=${$my_main{"PLOT_DIMENSION"}}[0];
my $plot_y=${$my_main{"PLOT_DIMENSION"}}[1] ;

if ( $my_main{"PLOT_SERIES"}[0] eq "MONO" ) 
{
#     print "base:", $file_base, "extension:{", $file_ext, "}\n" ;
    $file = join('', $file_base, '.', $file_ext ) ;
    push ( @file_list, $file ) ;

print MACRO << "END_OF_TEXT" ;
 open uni 24 file $file over
 image plot \( $method unit 24 background $background_col pixels $plot_x $plot_y init on \)\

END_OF_TEXT

    if ( $file_image ne "" && $file_image ne " ") 
    {
#	print "file image: [$file_image] \n" ;
	print MACRO "< $file_image\n" ; 
    }
    else
    {
#    print " model: ", $my_main{"PLOT_MODEL"}[0], " maps ", $my_main{"PLOT_MAPS"}[0], "\n" ;
	if ( $my_main{"PLOT_MODEL"}[0] eq "ON" ) { print MACRO "image over false\n" ; }
	if ( $my_main{"PLOT_MODEL"}[0] eq "RE_IMAGE" ) { print MACRO "<re_image.cmds WORK_SEGM\n" ; }
	if ( $my_main{"PLOT_MAPS"}[0] eq "ON" ) { print MACRO "image map over \n" ; }
	
	print MACRO "\n close uni 24 \n\n" ;
	
    }
}
elsif ( $my_main{"PLOT_SERIES"}[0] eq "STEREO" ) 
{
# first image - likely the left one
    print MACRO "image mat y-ax ", -$my_main{"PLOT_SERIES_ANGLE"}[0], "\n" ; 
    print MACRO "show matr 1\n" ;

    $file = join('', $file_base, '-l.', $file_ext ) ;
    push ( @file_list, $file ) ;

print MACRO << "END_OF_TEXT" ;
 open uni 24 file $file over
 image plot \( $method unit 24 init on \)\

END_OF_TEXT

    if ( $file_image ne "" && $file_image ne " ") 
    {
	print "file image: [$file_image] \n" ;
	print MACRO "< $file_image\n" ; 
    }
    else
    {
	if ( $my_main{"PLOT_MODEL"}[0] eq "ON" ) { print MACRO "image over false\n" ; }
	if ( $my_main{"PLOT_MAPS"}[0] eq "ON" ) { print MACRO "image map over \n" ; }
    }	
    print MACRO "\n close uni 24 \n\n" ;

    print MACRO "image mat y-ax ", 2*$my_main{"PLOT_SERIES_ANGLE"}[0], "\n" ; 
    print MACRO "show matr 1\n" ;

#second image
    $file = join('', $file_base, '-r.', $file_ext ) ;
    push ( @file_list, $file ) ;
print MACRO << "END_OF_TEXT" ;
 open uni 24 file $file over
 image plot \( $method unit 24 init on \)\

END_OF_TEXT

    if ( $file_image ne "" && $file_image ne " ") 
    {
	print "file image: [$file_image] \n" ;
	print MACRO "< $file_image\n" ; 
    }
    else
    {
	if ( $my_main{"PLOT_MODEL"}[0] eq "ON" ) { print MACRO "image over false\n" ; }
	if ( $my_main{"PLOT_MAPS"}[0] eq "ON" ) { print MACRO "image map over \n" ; }
    }	
    print MACRO "\n close uni 24 \n\n" ;
    if ( $file_view ne "" ) { print MACRO "< $file_view\n" ; }
}
else
{
    print "NEW SERIES \n" ;
    if ( $file_view ne "" ) { print MACRO "< $file_view\n" ; }
}


	print MACRO "image plot off \n" ;

if ( $my_main{"PLOT_RENDER"}[0] eq "POVRAY" ) 
{
    print MACRO "shell echo \#povray.log > povray.log \n" ;
    
    foreach $file ( @file_list ) 
    {
	my $string ;
	if ( $YCOM ne "" )
	{
	    print MACRO "shell cat $YCOM >> $file \n" ;
	}
	print " plot output file: $file \n" ;
#    $string = join( '', 'shell ', '"' ) ;
	my $chr = '"' ;
	print MACRO "shell ", $chr, "povray  +W", $my_main{"PLOT_DIMENSION"}[0], 
	" +H", $my_main{"PLOT_DIMENSION"}[1], " +A\[0.1\] +FN ", $file, ">&povray.log&", $chr, "\n" ;
#    print MACRO "shell", \", "povray  +H", $my_main{"PLOT_DIMENSION"}[0], 
#          " +W", $my_main{"PLOT_DIMENSION"}[1], " +A\[0.1\] +FN", $file_save, \", "\n" ;
    }
}
if ( $my_main{"PLOT_RENDER"}[0] eq "RASTER3D" ) 
{
    print MACRO "shell echo \#raster3d.log > raster3d.log \n" ;
    
    foreach $file ( @file_list ) 
    {
	my @xx = split('\.', $file) ;
	my $string ;
#    $string = join( '', 'shell ', '"' ) ;
	my $chr = '"' ;
	$file_ext = pop(@xx) ;
	$file_base = join('', @xx ) ;
	my $pl_size = join("x", $my_main{"PLOT_DIMENSION"}[0], $my_main{"PLOT_DIMENSION"}[1] ) ;
	    print " plot dimension $pl_size \n" ;
	print MACRO "shell ", $chr, "/usr/local/bin/render -png ", "-size ", $pl_size, " ", $file_base, ".png <", $file, $chr, "\n" ;
# print $file_save, "xx:", @xx, "\n" ;
	
#    print MACRO "shell", \", "povray  +H", $my_main{"PLOT_DIMENSION"}[0], 
#          " +W", $my_main{"PLOT_DIMENSION"}[1], " +A\[0.1\] +FN", $file_save, \", "\n" ;
    }
}

print MACRO "return \n" ;

close(MACRO) ;
