#!/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{"IMAGE_SURF_GRID_SIZE"} ) )  { push ( @{$my_main{"IMAGE_SURF_GRID_SIZE"}} , 0.6 ) };
if ( !exists($my_main{"IMAGE_SURF_WATER"} ) )  { push ( @{$my_main{"IMAGE_SURF_WATER"}} , 1.6 ) };
if ( !exists($my_main{"IMAGE_SURF_SCALE_RADII"} ) )  { push ( @{$my_main{"IMAGE_SURF_SCALE_RADII"}} , 1.0 ) };
if ( !exists($my_main{"IMAGE_SURF_RADII"} ) )  { push ( @{$my_main{"IMAGE_SURF_RADII"}} , "VdW" ) };
if ( !exists($my_main{"IMAGE_SURF_RESTRAINT"} ) )  { push ( @{$my_main{"IMAGE_SURF_RESTRAINT"}} , 0.1 ) };
if ( !exists($my_main{"IMAGE_SURF_SMOOTH"} ) )  { push ( @{$my_main{"IMAGE_SURF_SMOOTH"}} , 5 ) };
if ( !exists($my_main{"IMAGE_SURF_HYDROGEN"} ) )  { push ( @{$my_main{"IMAGE_SURF_HYDROGEN"}} , 5 ) };
if ( !exists($my_main{"IMAGE_SURF_SELECTION"} ) )  { push ( @{$my_main{"IMAGE_SURF_SELECTION"}} , "active" ) };
if ( !exists($my_main{"IMAGE_SURF_MY_MACRO"} ) )  { push ( @{$my_main{"IMAGE_SURF_MY_MACRO"}} , "++NONE++" ) };

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

my $ MACRO_FILE="image_gen_surface.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,6) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 'g' || substr($keyword, 0,4) eq "grid" ) { goto GRID }
    if ( $keyword eq 'p' || substr($keyword, 0,9) eq "restraint" ) { goto RESTRAINT }
    if ( $keyword eq 't' || substr($keyword, 0,6) eq "smooth" ) { goto SMOOTH }
    if ( $keyword eq 'w' || substr($keyword, 0,5) eq "water" ) { goto WATER }
    if ( $keyword eq 'r' || substr($keyword, 0,5) eq "radii" ) { goto RADII }
    if ( $keyword eq 'l' || substr($keyword, 0,5) eq "scale" ) { goto SCALE_RADII }
    if ( $keyword eq 's' || substr($keyword, 0,9) eq "selection" ) { goto SELECTION }
    if ( $keyword eq 'y' || substr($keyword, 0,8) eq "hydrogen" ) { goto HYDROGEN }
    if ( $keyword eq 'm' || substr($keyword, 0,8) eq "my_macro" ) { goto MACRO }
    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 
-g|--grid)        grid size [@{$my_main{"IMAGE_SURF_GRID_SIZE"}}]
-p|--restraint)   atom sphere restraint shift [@{$my_main{"IMAGE_SURF_RESTRAINT"}}]
-t|--smooth)      surface smooth cycles [@{$my_main{"IMAGE_SURF_SMOOTH"}}]
-a|--radii)       atoms radii kind [VdW/TEMP/ADP/VdW+TEMP/VdW+ADP] [@{$my_main{"IMAGE_SURF_RADII"}}]
-l|--scale)       atoms radii scale [1.0 .. 0.1] [@{$my_main{"IMAGE_SURF_SCALE_RADII"}}]
-w|--water)       water sphere radius [@{$my_main{"IMAGE_SURF_WATER"}}]
-s|--selection)   atom selection [@{$my_main{"IMAGE_SURF_SELECTION"}}]
-y|--hydrogen)    use hydrogen atoms [ON/OFF] [@{$my_main{"IMAGE_SURF_HYDROGEN"}}]
-m|--my_macro)    use my own macro file [++NONE++] [@{$my_main{"IMAGE_SURF_MY_MACRO"}}]
-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 ;

  GRID: @{$my_main{"IMAGE_SURF_GRID_SIZE"}} = shift ( @{$commands{$keyword}} ) ; next ;
  RESTRAINT:  @{$my_main{"IMAGE_SURF_RESTRAINT"}} = shift ( @{$commands{$keyword}} ) ; next ;
  SMOOTH:  @{$my_main{"IMAGE_SURF_SMOOTH"}} = shift ( @{$commands{$keyword}} ) ; next ;
  WATER:  @{$my_main{"IMAGE_SURF_WATER"}} = shift ( @{$commands{$keyword}} ) ; next ;
  RADII:  @{$my_main{"IMAGE_SURF_RADII"}} = shift ( @{$commands{$keyword}} ) ; next ;
  SCALE_RADII:  @{$my_main{"IMAGE_SURF_SCALE_RADII"}} = shift ( @{$commands{$keyword}} ) ; next ;
  HYDROGEN:  @{$my_main{"IMAGE_SURF_HYDROGEN"}} = shift ( @{$commands{$keyword}} ) ; next ;
  SELECTION:  @{$my_main{"IMAGE_SURF_SELECTION"}} = @{$commands{$keyword}} ; next ;
  MACRO:  @{$my_main{"IMAGE_SURF_MY_MACRO"}} = @{$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 $GRID_NUMB = 800 ;
my $CELL_LENGTH = int( $my_main{"IMAGE_SURF_GRID_SIZE"}[0] * $GRID_NUMB ) ;
my $SOLV_RAD = $my_main{"IMAGE_SURF_WATER"}[0] ;
my $SOLV_RST = $my_main{"IMAGE_SURF_RESTRAINT"}[0] ; $SOLV_RST = 1. / $SOLV_RST ; 
my $ATOM_RAD =  $my_main{"IMAGE_SURF_RADII"}[0] ;
my $ATOM_SCAL_RAD =  $my_main{"IMAGE_SURF_SCALE_RADII"}[0] ;
my $SELECTION = join (" ", @{$my_main{"IMAGE_SURF_SELECTION"}}) ;

my $N_SMOOTH = $my_main{"IMAGE_SURF_SMOOTH"}[0] ;
my $HYDROGEN = "" ;
if (  $my_main{"IMAGE_SURF_HYDROGEN"}[0] eq "OFF" ) { $HYDROGEN = ".a .n atom name H*" ; }

print " create_image_gen_surf.pl> grid size ", $my_main{"IMAGE_SURF_GRID_SIZE"}[0], $CELL_LENGTH, "\n" ;
######################################

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 $my_macro = @{$my_main{"IMAGE_SURF_MY_MACRO"}}[0] ;

if ( $my_macro eq "++NONE++" )
{

print MACRO << "END_OF_TEXT" ;

make map init 0 unit_cell $CELL_LENGTH $CELL_LENGTH $CELL_LENGTH 90 90 90 \\
       grid $GRID_NUMB $GRID_NUMB $GRID_NUMB \\
around 20 sele $SELECTION end integer
set vari IMAP = MAP_LAST
if ( IMAP .lt. 0 ) return
make map IMAP + 1 from IMAP init 0 real
END_OF_TEXT

if ( $ATOM_RAD eq "VdW" )
{
print MACRO << "END_OF_TEXT" ;

set rad sele $SELECTION end vdw

END_OF_TEXT
}
elsif ( $ATOM_RAD eq "TEMP" )
{
print MACRO << "END_OF_TEXT" ;

set rad sele $SELECTION end temp

END_OF_TEXT
}
elsif ( $ATOM_RAD eq "ADP" )
{
print MACRO << "END_OF_TEXT" ;

set rad sele $SELECTION end displace

END_OF_TEXT
}
elsif( $ATOM_RAD eq "VdW+TEMP" )
{
print MACRO << "END_OF_TEXT" ;

set rad sele $SELECTION end vdw
set add rad sele $SELECTION end temp

END_OF_TEXT
}
elsif( $ATOM_RAD eq "VdW+ADP" )
{
print MACRO << "END_OF_TEXT" ;

set rad sele $SELECTION end vdw
set add rad sele $SELECTION end displace

END_OF_TEXT
}


print MACRO << "END_OF_TEXT" ;
scal $ATOM_SCAL_RAD rad sele $SELECTION end

dele point sele $SELECTION end 3surface
make poin from atom rad $SOLV_RAD map IMAP \\
       surface sele $SELECTION $HYDROGEN end restr $SOLV_RST smooth $N_SMOOTH

dele map IMAP
dele map IMAP + 1

return
END_OF_TEXT
}
else
{
print MACRO << "END_OF_TEXT" ;

< $my_macro

return
END_OF_TEXT

}
close(MACRO) ;
