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

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

my $MACRO_FILE = "auto_fit.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 'e' || substr($keyword, 0,6) eq "expand" ) { goto EXPAND }
    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)  file name of the created macro [$MACRO_FILE]
  -e|--expand)  cushion around the active atoms [@{$my_main{"AUTO_FIT_EXPAND"}}]
     --doit)    create the macro "$MACRO_FILE"
END_OF_TEXT
    exit; 
OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;

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

my $AF_EXPAND = $my_main{"AUTO_FIT_EXPAND"}[0] ;

print MACRO << "END_OF_TEXT" ; 

subroutine char SEGMENTS
echo "AUTO_FIT> initial"
!< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK 
ener chem ( all off ) dens on


mini key


echo "AUTO_FIT> dens opt"

!< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK 
trans atom sele active end save $AF_EXPAND 
! set coor sele active end kick 0.3
ener chem ( bond on angle on )
mini  key
correct bond sele active end
mini  key
ener chem ( dihe on impr on )
mini  key
mini  key
ener chem ( all on )
mini key
mini key
! echo "AUTO_FIT> before peptides "
!< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK 
< ?MAIN_CMDS_NEW:auto_stuff_fix_pept.cmds WORK_SEGM
< ?MAIN_CMDS_NEW:auto_stuff_find_pept.cmds WORK_SEGM
echo "AUTO_FIT> after  peptides "
!< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK 
mini  key
mini  key

! echo "AUTO_FIT> before rigid "
!< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK 
< ?MAIN_CMDS_NEW:auto_stuff_rig_side.cmds WORK_SEGM
! echo "AUTO_FIT> after rigid  "
!< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK 
mini  key
mini  key

! echo "AUTO_FIT> before side chain "
!< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK 
< ?MAIN_CMDS_NEW:auto_stuff_side.cmds WORK_SEGM FIT
! echo "AUTO_FIT> after side chain "
!< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK 
mini  key
mini  key
mini  key
mini  key
mini  key
mini  key

! echo "AUTO_FIT> done "
!< ?MAIN_CMDS:re_phase.cmds WORK_SEGM MAP_WORK 
< ?MAIN_CMDS:re_image.cmds WORK_SEGM

return


END_OF_TEXT

close ( MACRO ) ;




