#!/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 ) ; 


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

my $ICOLOR = 105 ;
my $ICOLOR_STEP = 5 ;

    if ( $my_main{"SG_NMOL"}[0] > 4 ) { $ICOLOR = 90 ; }
    if ( $my_main{"SG_NMOL"}[0] > 8 ) { $ICOLOR_STEP = 2 ; }

my $SYMM_SPHERE = 60.0 ;

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

my $MACRO_FILE="symmetry_ca.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 'c' || substr($keyword, 0,5) eq "color" ) { goto COLOR }
    if ( $keyword eq 'r' || substr($keyword, 0,4) eq "step" ) { goto STEP }
    if ( $keyword eq 's' || substr($keyword, 0,6) eq "radius" ) { goto RADIUS }

    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]
  -c|--color)   starting color index for molecules [$ICOLOR]
  -s|--step)    increment of next molecule color [$ICOLOR_STEP]
  -r|--radius)  sphere radius for symmetry atoms generation [$SYMM_SPHERE]
     --doit)    create the macro 
END_OF_TEXT
    exit ;
OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
COLOR: $ICOLOR = shift ( @{$commands{$keyword}} ) ; next ;
STEP: $ICOLOR_STEP = shift ( @{$commands{$keyword}} ) ; next ;
RADIUS: $SYMM_SPHERE = 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 ; }

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

if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}

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

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

print MACRO << "END_OF_TEXT" ;
subroutine char SEGMENTS
delete atom select segment name \#* end

set vari CA_SPHERE real = $SYMM_SPHERE

make bond  sele segm name SEGMENTS end from residue = CA
delet bond select bond range 4.4 1000. end

END_OF_TEXT

if ( $my_main{"SG_NMOL"} == 1 )
{
print MACRO << "END_OF_TEXT" ;
 key old select all end
symmetry sele atom name CA .a segm name SEGMENTS end -
            around center atom \$1 dist CA_SPHERE neigh 2
key symm_ sele .not old end
image sele symm_ end col $ICOLOR bond
END_OF_TEXT
}
else
{
    my $SEGM;
    foreach $SEGM ( @{$my_main{"SG_LIST"}} )
    {
print MACRO << "END_OF_TEXT" ;
key old select all end
symm select segment name $SEGM .a atom name CA end around \\ 
     center atom \$1 dist CA_SPHERE neigh 2
key SYMM_M sele .not old end
image select SYMM_M end col $ICOLOR bond

END_OF_TEXT
    $ICOLOR = $ICOLOR + $ICOLOR_STEP ;
    }
} 

print MACRO << "END_OF_TEXT" ;

delete bond sele atom name CA .a segm name SEGMENTS end
return
END_OF_TEXT

close ( MACRO ) ;

