#!/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_BOND_STYLE"} ) ) { push ( @{$my_main{"IMAGE_BOND_STYLE"}}, "LINE" ) };
if ( !exists($my_main{"IMAGE_ATOM_STYLE"} ) ) { push ( @{$my_main{"IMAGE_ATOM_STYLE"}}, "NONE" ) };
if ( !exists($my_main{"IMAGE_STYLE"} ) ) { push ( @{$my_main{"IMAGE_STYLE"}}, "WEIGHTS" ) };
if ( !exists($my_main{"IMAGE_HYDRO"} ) ) { push ( @{$my_main{"IMAGE_HYDRO"}}, "ON" ) };
if ( !exists($my_main{"IMAGE_CHAINS"} ) ) { push ( @{$my_main{"IMAGE_CHAINS"}}, "NONE" ) };
if ( !exists($my_main{"IMAGE_GROUP_NAMES"} ) ) { push ( @{$my_main{"IMAGE_GROUP_NAMES"}}, "SEGMENT" ) };
if ( !exists($my_main{"IMAGE_COLOR_START"} ) ) 
{ 
    if ( $my_main{"SG_NMOL"}[0] > 8 ) 
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 144 ) }
    elsif( $my_main{"SG_NMOL"}[0] > 4 ) 
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 150 ) ; }
    elsif( $my_main{"SG_NMOL"}[0] > 2 ) 
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 152 ) ; }
    else
    { push ( @{$my_main{"IMAGE_COLOR_START"}}, 160 ); }
}
if ( !exists($my_main{"IMAGE_COLOR_INCR"} ) ) 
{ 
    if ( $my_main{"SG_NMOL"}[0] > 8 ) 
    { push ( @{$my_main{"IMAGE_COLOR_INCR"}}, 5 ) }
    else
    { push ( @{$my_main{"IMAGE_COLOR_INCR"}}, 5 ) ; }
}

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

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 'a' || substr($keyword, 0,5) eq "atoms" ) { goto ATOMS }
    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 'x' || substr($keyword, 0,6) eq "chains" ) { goto CHAINS }
    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 'n' || substr($keyword, 0,5) eq "names" ) { goto GROUP_NAMES }
    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" ;

# -n|--names)     list by names of segments, chains  [SEGMENT/CHAIN] [@{$my_main{"IMAGE_GROUP_NAMES"}}]

  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]
 -a|--atoms)     display atom type style [BALL/NAME/CLASS/CROSS/NONE] [@{$my_main{"IMAGE_ATOM_STYLE"}}]
 -b|--bonds)     display bond type style [LINE/STICK/NONE] [@{$my_main{"IMAGE_BOND_STYLE"}}]
 -c|--color)     starting color index for molecules [64 - 255] [@{$my_main{"IMAGE_COLOR_START"}}]
 -x|--chains)    chain display [STICK/LINE/NONE] [@{$my_main{"IMAGE_CHAINS"}}]
 -y|--hydrogens) display [ON/OFF] of hydrogen atoms [@{$my_main{"IMAGE_HYDRO"}}]
 -i|--increment) increment of next molecule color [@{$my_main{"IMAGE_COLOR_INCR"}}]
 -s|--style)     color styles [WEIGHTS/TYPES/N2C/SECONDARY/RESIDUE] of molecular images [@{$my_main{"IMAGE_STYLE"}}]
    --doit)    creates the macro
END_OF_TEXT

    exit ;
  ATOMS:   @{$my_main{"IMAGE_ATOM_STYLE"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  BONDS:   @{$my_main{"IMAGE_BOND_STYLE"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  COLOR:  @{$my_main{"IMAGE_COLOR_START"}} = shift ( @{$commands{$keyword}} ) ; next ;
  CHAINS:  @{$my_main{"IMAGE_CHAINS"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  HYDROGENS:   @{$my_main{"IMAGE_HYDRO"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;
  GROUP_NAMES:  @{$my_main{"IMAGE_GROUP_NAMES"}} = shift ( @{$commands{$keyword}} ) ; next ;
  INCREMENT:   @{$my_main{"IMAGE_COLOR_INCR"}}= 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 ; }

######################################
my $ICOLOR = ${$my_main{"IMAGE_COLOR_START"}}[0] ;
my $ICOLOR_INCREM = ${$my_main{"IMAGE_COLOR_INCR"}}[0];


print "creating \"$MACRO_FILE\" \n" ;
if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}
open ( MACRO,  "> $MACRO_FILE" ) ; # || print "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

END_OF_TEXT

print MACRO << "END_OF_TEXT" ;
 set color
END_OF_TEXT


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

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.02 1.1 end = $ICOLOR\n",
	" sele segm name SEGMENTS .a \\ \n", 
	"     atom name H* .a by bond weigh 0.02 1.1 end = $ICOLOR\n", 
	" sele segm name WAT2 end = 140\n" ;
}
else
{
    print MACRO
	"sele segm name SEGMENTS .a weigh 0.02 1.1 end = $ICOLOR\n",
	"sele segm name SEGMENTS .a \\ \n",
	"     atom name H* .a by bond weigh 0.02 1.1 end = $ICOLOR\n" ;
    my $I ;
    foreach $I ( @{$my_main{"SG_LIST"}} )
    {
	print MACRO
	    " sele segm name $I .a weigh 0.02 1.1 end = $ICOLOR\n",
	    " sele segm name $I .and. \\ \n",
	    "    atom name H* .a by bond weigh 0.02 1.1 end = $ICOLOR\n" ;
	$ICOLOR = $ICOLOR + $ICOLOR_INCREM ;
    }
}
print MACRO 
    "sele segm name WAT2 end = $ICOLOR \n" ;

if ( @{$my_main{"IMAGE_STYLE"}}[0] eq "WEIGHTS" )
{
print MACRO 
    "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  N2C

if ( @{$my_main{"IMAGE_STYLE"}}[0] eq "N2C" )
{
    print MACRO << "END_OF_TEXT" ;
 sele segm name SEGMENTS end residue 128 255 8
END_OF_TEXT
print MACRO 
    "exit\n\n" ;
}

# style  N2C

if ( @{$my_main{"IMAGE_STYLE"}}[0] eq "SECONDARY" )
{
    print MACRO << "END_OF_TEXT" ;
 sele segm name SEGMENTS .a sec alpha end col 110
 sele segm name SEGMENTS .a sec beta end col 220
END_OF_TEXT
print MACRO 
    "exit\n\n" ;
}



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

sele segm name SEGMENTS  .a resi name GLY ILE VAL LEU ALA PRO MET end = 32
sele segm name SEGMENTS  .a resi name ASP GLU end = 110
sele segm name SEGMENTS  .a resi name LYS ARG end = 240
sele segm name SEGMENTS  .a resi name TYR PHE TRP HIS* end = 200
sele segm name SEGMENTS  .a resi name SER THR ASN GLN end = 220
sele segm name SEGMENTS  .a resi name CYS end = 160
END_OF_TEXT
print MACRO 
    "exit\n\n" ;
}


my $segm ; my $swat ;
foreach $segm ( @{$my_main{"SG_LIST"}} )
{
    if ( $segm eq "WAT" ) { $swat = "WAT*" ; } else { $swat = $segm ; }
    print MACRO " image group name SEG_", $swat, "\n" ;

if ( @{$my_main{"IMAGE_HYDRO"}}[0] eq "ON" )
{
    print MACRO << "END_OF_TEXT" ;
key display sele segm name $swat .a segm name SEGMENTS end
END_OF_TEXT
}
else
{
    print MACRO << "END_OF_TEXT" ;
key display sele segm name $swat .a .n atom name H* .a segm name SEGMENTS end
END_OF_TEXT
}

# CHAINS

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

key xx_active sele active end
key active sele display 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
}
elsif ( @{$my_main{"IMAGE_CHAINS"}}[0] eq "LINE" )
{
    print MACRO << "END_OF_TEXT" ;

key xx_active sele active end
key active sele display end
 <?MAIN_CMDS:image_color_set.cmds
 <?MAIN_CMDS_NEW:image_do_rib_line.cmds 
key active sele xx_active end
key xx_active drop

END_OF_TEXT
}

# ATOMS

if ( @{$my_main{"IMAGE_ATOM_STYLE"}}[0] eq "BALL" )
{
    print MACRO << "END_OF_TEXT" ;
image sele display end set atom ball

END_OF_TEXT
}
elsif ( @{$my_main{"IMAGE_ATOM_STYLE"}}[0] eq "NAME" )
{
    print MACRO << "END_OF_TEXT" ;

image sele display end set atom name

END_OF_TEXT
}
elsif ( @{$my_main{"IMAGE_ATOM_STYLE"}}[0] eq "CLASS" )
{
    print MACRO << "END_OF_TEXT" ;
image sele display end set atom class

END_OF_TEXT
}
elsif ( @{$my_main{"IMAGE_ATOM_STYLE"}}[0] eq "CROSS" )
{
    print MACRO << "END_OF_TEXT" ;
image sele display end set atom cross

END_OF_TEXT
}

# BONDS
if ( @{$my_main{"IMAGE_BOND_STYLE"}}[0] eq "LINE" )
{ 
    print MACRO << "END_OF_TEXT" ;
image sele display end set bond line
image sele display .a .n by bond display end set atom cros
image sele display .a .n by bond all .a .n atom name O* end set atom ball
END_OF_TEXT
}
elsif ( @{$my_main{"IMAGE_BOND_STYLE"}}[0] eq "STICK" )
{ 
    print MACRO << "END_OF_TEXT" ;
image sele display end set bond stick
image sele display .a .n by bond display end set atom cros
image sele display .a .n by bond all .a .n atom name O* end set atom ball

END_OF_TEXT
} 

}

print MACRO << "END_OF_TEXT" ;

  image group name MISCELLANEOUS
!  image sele display .a .not by bond all end atom cros
  image sele segm name WORK_SEGM 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 pair 

END_OF_TEXT

print MACRO << "END_OF_TEXT" ;

key display drop
return

END_OF_TEXT

close(MACRO) ;
