#!/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{"CENTER_DEFINE"} ) ) { push ( @{$my_main{"CENTER_DEFINE"}}, "RESIDUE" ) } ; 

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

my $MACRO_FILE = "center_define.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 'b' || substr($keyword, 0,2) eq "by" ) { goto BY }
    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|--by)      goto the next residue by [RESIDUE/BOND/DENSITY/VDW] [@{$my_main{"CENTER_DEFINE"}}]
     --doit)    create the macro "$MACRO_FILE"
END_OF_TEXT
    exit;
OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;

BY: @{$my_main{"CENTER_DEFINE"}} = 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" ;

if ( $my_main{"CENTER_DEFINE"}[0] eq "RESIDUE" ) 
{
    system ("rm $MACRO_FILE" ) ;
}
else
{
print MACRO << "END_OF_TEXT" ; 
subroutine char SEGMENTS
anal atom init
END_OF_TEXT

if ( $my_main{"CENTER_DEFINE"}[0] eq "BOND" ) 
{
print MACRO << "END_OF_TEXT" ; 

 ener chem ( all off bond on angle on )
 ener sele segm name SEGMENTS end 
 anal sele segm name SEGMENTS end force
END_OF_TEXT

print MACRO 'open unit 66 file validate.dat write\n' ;
print MACRO "show unit 66 anal sele segm name SEGMENTS end\n" ;
print MACRO "clo unit 66\n" ;
print MACRO "ener chemistry ( all on ) dens on\n" ;

}
elsif ( $my_main{"CENTER_DEFINE"}[0] eq "VDW" ) 
{

print MACRO " ener chem ( all off vdw on ) \n" ;
print MACRO " ener sele segm name SEGMENTS end \n" ;
print MACRO " anal sele segm name SEGMENTS end force\n" ;
print MACRO " open uni 66 file validate.dat write\n" ;
print MACRO " show uni 66 anal sele segm name SEGMENTS end\n" ;
print MACRO " clo uni 66\n" ;
print MACRO " ener chemistry ( all on ) dens on\n" ;

}
elsif ( $my_main{"CENTER_DEFINE"}[0] eq "DENISTY" ) 
{

print MACRO " ener chem ( all off ) density on \n" ;
print MACRO " ener sele segm name SEGMENTS end \n" ;
print MACRO " anal sele segm name SEGMENTS end energy resid 1\n" ;
print MACRO " open unit 66 file validate.dat write\n" ;
print MACRO " show unit 66 anal sele segm name SEGMENTS end\n" ;
print MACRO " clo uni 66\n" ;
print MACRO " ener chemistry ( all on ) dens on\n" ;

}


my $macro_file = join ('', $ENV{"MAIN_CONF"}, "analize_file.pl" ) ; 
print MACRO << "END_OF_TEXT" ; 
set vari MY_MACRO = $macro_file 
shell "analize_file.pl -i validate.dat -o center_next.com --doit"
END_OF_TEXT

print MACRO "open uni 66 file center_next.com read\n" ;
print MACRO "stream uni 66\n" ;
print MACRO << "END_OF_TEXT" ; 
image cent calc sele atom numb NEXT_RESI end
image map over
return
END_OF_TEXT


close ( MACRO ) ;
}




