#!/usr/bin/perl -w
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_BOND_STYLE"} ) ) { push ( @{$my_main{"IMAGE_BOND_STYLE"}}, "LINE" ) };
if ( !exists($my_main{"IMAGE_STYLE"} ) ) { push ( @{$my_main{"IMAGE_STYLE"}}, "WEIGHTS" ) };
if ( !exists($my_main{"IMAGE_HYDRO"} ) ) { push ( @{$my_main{"IMAGE_HYDRO"}}, "ON" ) };
my $ICOLOR = 160 ;
my $ICOLOR_INCREM = 5 ;

if ( $my_main{"SG_NMOL"}[0] > 2 ) { $ICOLOR = 150 ; }
if ( $my_main{"SG_NMOL"}[0] > 4 ) { $ICOLOR = 140 ; }
if ( $my_main{"SG_NMOL"}[0] > 8 ) { $ICOLOR_INCREM = 2 ; }


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

my $BN_STYLE ;

my $MACRO_FILE = "re_image.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 'b' || substr($keyword, 0,5) eq "bonds" ) { goto BONDS }
    if ( $keyword eq 'c' || substr($keyword, 0,5) eq "color" ) { goto COLOR }
    if ( $keyword eq 'y' || substr($keyword, 0,9) eq "hydrogens" ) { goto HYDROGENS }
    if ( $keyword eq 'i' || substr($keyword, 0,9) eq "increment" ) { goto INCREMENT }
    if ( $keyword eq 's' || substr($keyword, 0,5) eq "style" ) { goto STYLE }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    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|--bonds)     display bond type style [LINE/STICK] [@{$my_main{"IMAGE_BOND_STYLE"}}]
 -c|--color)     starting color index for molecules [$ICOLOR]
 -y|--hydrogens) display [ON/OFF] of hydrogen atoms [@{$my_main{"IMAGE_HYDRO"}}]
 -i|--increment) increment of next molecule color [$ICOLOR_INCREM]
 -s|--style)     styles [WEIGHTS/TYPES/CHAINS] of molecular images [@{$my_main{"IMAGE_STYLE"}}]
    --doit)    creates the macro
END_OF_TEXT

    exit ;
  BONDS:   @{$my_main{"IMAGE_BOND_STYLE"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  COLOR:  $ICOLOR= shift ( @{$commands{$keyword}} ) ; next ;
  HYDROGENS:   @{$my_main{"IMAGE_HYDRO"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  INCREMENT:   $ICOLOR_INCREM= shift ( @{$commands{$keyword}} ) ; next ;
  STYLE:   @{$my_main{"IMAGE_STYLE"}} = uc(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" ;

print MACRO << "END_OF_TEXT" ;
subroutine char SEGMENTS
  image erase
delete atom sele segm name #* end
key image sele .not all end

 set color
END_OF_TEXT

# style = ATOM WEIGHTS
if ( @{$my_main{"IMAGE_STYLE"}}[0] eq "WEIGHTS" )
{

print MACRO << "END_OF_TEXT" ;
 sele segm name SEGMENTS end = 200
END_OF_TEXT

if ( @{$my_main{"SG_NMOL"}}[0] == 1 )
{
    print MACRO
	" sele segm name SEGMENTS .a weigh 0.1 1.1 end = 160\n",
	" sele segm name SEGMENTS .a \\ \n", 
	"     atom name H* .a by bond weigh 0.1 1.1 end = 160\n", 
	" sele segm name WAT2 end = 140\n" ;
}
else
{
    print MACRO
	"sele segm name SEGMENTS .a weigh 0.1 1.1 end = $ICOLOR\n",
	"sele segm name SEGMENTS .a \\ \n",
	"     atom name H* .a by bond weigh 0.1 1.1 end = $ICOLOR\n" ;
    my $I ;
    foreach $I ( @{$my_main{"SG_LIST"}} )
    {
	print MACRO
	    " sele segm name $I .a weigh 0.1 1.1 end = $ICOLOR\n",
	    " sele segm name $I .and. \\ \n",
	    "    atom name H* .a by bond weigh 0.1 1.1 end = $ICOLOR\n" ;
	$ICOLOR = $ICOLOR + $ICOLOR_INCREM ;
    }
}

print MACRO 
    "sele segm name WAT2 end = $ICOLOR \n",
    "exit\n\n" ;
}
# style = ATOM TYPES

if ( @{$my_main{"IMAGE_STYLE"}}[0] eq "TYPES" )
{
if ( @{$my_main{"SG_NMOL"}}[0] == 1 )
{
    print MACRO << "END_OF_TEXT" ;
 sele segm name SEGMENTS end default
 sele segm name SEGMENTS .a atom name C* end = 160

END_OF_TEXT
}
else
{
    print MACRO << "END_OF_TEXT" ;
   sele segm name SEGMENTS end default
END_OF_TEXT

    my $I ;
    foreach $I ( @{$my_main{"SG_LIST"}} )
    {
	print MACRO
	    " sele segm name $I .a atom name C* end = $ICOLOR\n" ;
	$ICOLOR = $ICOLOR + $ICOLOR_INCREM ;
    }
}

print MACRO 
    "exit\n\n" ;

}

# style = CHAINS

if ( @{$my_main{"IMAGE_STYLE"}}[0] eq "CHAINS" )
{
    print MACRO << "END_OF_TEXT" ;

! sele segm name SEGMENTS end chain 140 190
 sele segm name SEGMENTS end residue 128 255 8
exit

key xx_active sele active end
key active sele segm name WORK_SEGM end
 <?MAIN_CMDS:image_color_set.cmds
 <?MAIN_CMDS_NEW:image_do_rib_stick.cmds 
key active sele xx_active end
key xx_active drop

END_OF_TEXT
}

if ( @{$my_main{"IMAGE_BOND_STYLE"}}[0] eq "LINE" )
{ $BN_STYLE="bond" ; } 
else
{ $BN_STYLE="bond stick" ; } 
# print  " bn_style: $BN_STYLE \n" ;

if ( @{$my_main{"IMAGE_HYDRO"}}[0] eq "ON" )
{

print MACRO << "END_OF_TEXT" ;

  image sele segment name SEGMENTS end set $BN_STYLE
  image sele segment name SEGMENTS .a .not by bond all end atom cros
  image sele segment name SEGMENTS end col 85 hbond
  image sele t_anchor end col 200 atom cros
  image sele t_anchor .o by pair t_anchor end col 200 $BN_STYLE
END_OF_TEXT
}
else
{
#    print " in 204 -- $BN_STYLE \n" ;
print MACRO << "END_OF_TEXT" ;
key no_hydro sele .not. atom name H* end
  image sele segment name SEGMENTS .a no_hydro end set $BN_STYLE
  image sele segment name SEGMENTS .a no_hydro \\
        .a .not by bond no_hydro end atom cros
  image sele t_anchor end col 200 atom cros
  image sele t_anchor .o by pair t_anchor end col 200 $BN_STYLE
key no_hydro drop

END_OF_TEXT

}

print MACRO 
    "return\n\n" ;

close(MACRO) ;
