#!/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{"RoH_CYCLES"} ) )  { push ( @{$my_main{"RoH_CYCLES"}} , 1 ) };
if ( !exists($my_main{"RoH_PHASE_EXT"} ) )  { push ( @{$my_main{"RoH_PHASE_EXT"}} ,  @{$my_main{"DFR_RES_MAX"}}, @{$my_main{"DFR_RES_MAX"}} ) };
if ( !exists($my_main{"RoH_SECONDARY"} ) )  { push ( @{$my_main{"RoH_SECONDARY"}} , "OFF" ) };
if ( !exists($my_main{"RoH_REFINE"} ) )  { push ( @{$my_main{"RoH_REFINE"}} , "OFF" ) };
if ( !exists($my_main{"RoH_PHASE_COMB"} ) )  { push ( @{$my_main{"RoH_PHASE_COMB"}} , "OFF" ) };

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


my $MACRO_FILE = "roh_jobs.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;
my $ORIGIN = "WORK" ;
my $file_save = "SAVE_FILE" ;

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 'c' || substr($keyword, 0,6) eq "cycles" ) { goto CYCLES }
    if ( $keyword eq 'e' || substr($keyword, 0,6) eq "extend" ) { goto EXTEND }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "file" ) { goto FILE }
    if ( $keyword eq 'g' || substr($keyword, 0,6) eq "origin" ) { goto ORIGIN }
    if ( $keyword eq 'p' || substr($keyword, 0,5) eq "phase" ) { goto PHASE }
    if ( $keyword eq 'r' || substr($keyword, 0,6) eq "refine" ) { goto REFINE }
    if ( $keyword eq 's' || substr($keyword, 0,9) eq "secondary" ) { goto SECONDARY }
    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  
-c|--cycles)    number of cycles at each resolution  [@{$my_main{"RoH_CYCLES"}}]
-e|--extend)    phase extension range [@{$my_main{"RoH_PHASE_EXT"}}]
-f|--file)      save file name root [$file_save]
-g|--origin)    origin search ON/WORK/ACTIVE [$ORIGIN]
-p|--phases)    phase combination [@{$my_main{"RoH_PHASE_COMB"}}]
-r|--refine)    refine included ON/OFF  [@{$my_main{"RoH_REFINE"}}]
-s|--secondary) secondary structure search ON/OFF [@{$my_main{"RoH_SECONDARY"}}]
-o|--output)    set file name of the created macro [$MACRO_FILE]
   --doit)      create the macro
END_OF_TEXT
    exit ;
 OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
 CYCLES: @{$my_main{"RoH_CYCLES"}} = shift ( @{$commands{$keyword}} ) ;  next ;
 FILE: $file_save = shift ( @{$commands{$keyword}} ) ; next ;
 ORIGIN: $ORIGIN = shift ( @{$commands{$keyword}} ) ; next ;
 PHASE:  @{$my_main{"RoH_PHASE_COMB"}} = shift ( @{$commands{$keyword}} ) ; next ;
 EXTEND:  
    @{$my_main{"RoH_PHASE_EXT"}} = shift ( @{$commands{$keyword}} ) ; 
    push (@{$my_main{"RoH_PHASE_EXT"}}, shift ( @{$commands{$keyword}} ) ); 
    next ;
 REFINE: @{$my_main{"RoH_REFINE"}} = shift ( @{$commands{$keyword}} ) ;  next ;
 SECONDARY: @{$my_main{"RoH_SECONDARY"}} = 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 ) ; 

###  . ${MAIN_CONF}main_config.data.save.sh

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" ;


open( CELL_DAT , "< @{$my_main{\"FILE_CELL\"}} " ) || die "NO UNIT CELL DATA FILE\n" ; 
my $line = <CELL_DAT> ; 
chomp ( $line ) ; my @cell = split( ' ', $line) ; 
while ( $cell[0] eq "*" ) 
{ 
    $line = <CELL_DAT> ; 
    chomp ( $line ) ; 
    @cell = split( ' ', $line) ; 
}
close (CELL_DAT) ;
my $cell_max = $cell[0] ; 
if ( $cell[1] > $cell_max ) { $cell_max = $cell[1] ; }
if ( $cell[2] > $cell_max ) { $cell_max = $cell[2] ; }

my $i ; 
my $ires_low ;
my $ires_high ;
print "phase 01 @{$my_main{'RoH_PHASE_EXT'}} \n" ;
$ires_low = $cell_max / ${$my_main{"RoH_PHASE_EXT"}}[0] ;
if ( scalar(@{$my_main{"RoH_PHASE_EXT"}}) == 1 ) 
{ $ires_high = $ires_low }
else
{ $ires_high = $cell_max / ${$my_main{"RoH_PHASE_EXT"}}[1] ; }

my $tmp = ${$my_main{"RoH_PHASE_EXT"}}[0] ;
my $tmp_low = ${$my_main{"RoH_PHASE_EXT"}}[1] ;

print MACRO << "END_OF_TEXT" ;
set vari MAP_CONT global = 0.8

set vari RESOL_MAX global = $tmp
refl key WORK_REFL sele defined .a resol $tmp_low $tmp end

< ?MAIN_CMDS:calc_fo_map.cmds MAP_FO
END_OF_TEXT

if ( $ORIGIN eq "ON" )
{
print MACRO << "END_OF_TEXT" ;
< ?MAIN_CMDS:trace_origin.cmds WORK_SEGM INITIALIZE
< ?MAIN_CMDS:map_mask_skel_gen.cmds 
< ?MAIN_CMDS:map_mask_skel_gen.cmds 
END_OF_TEXT
}
elsif ( $ORIGIN eq "ACTIVE" )
{
print MACRO << "END_OF_TEXT" ;
< ?MAIN_CMDS:use_atoms.cmds ACTIVE 

END_OF_TEXT
}
#elsif ( $ORIGIN eq "WORK" || $ORIGIN eq "OFF" )
else
{
print MACRO << "END_OF_TEXT" ;
< ?MAIN_CMDS:use_atoms.cmds WORK 

END_OF_TEXT
}

my $secnd = 0 ;

for ($i = $ires_low; $i<=$ires_high; $i++ )
{
    my $tmp_resol = $cell_max / $i ;
    $tmp = substr ($tmp_resol, 0, 4) ;
    my $icyc ;
    my $ncyc = ${$my_main{"RoH_CYCLES"}}[0] ;

    print MACRO << "END_OF_TEXT" ;

! new resolution limits $i

set vari RESOL_MAX global = $tmp
refl key WORK_REFL sele defined .a resol $tmp_low $tmp end

END_OF_TEXT



    for ($icyc = 0; $icyc < $ncyc ; $icyc++ )
    {
    print "cycles $icyc $ncyc $tmp \n" ;
    print MACRO << "END_OF_TEXT" ;

< ?MAIN_CMDS:map_mask_skel_gen.cmds 
< ?MAIN_CMDS:trace_map.cmds ACTIVE GRADIENT 

END_OF_TEXT

# # if ( ${$my_main{"RoH_SECONDARY"}}[0] eq "ON" && $secnd > 0 ) 
# # {
# # 	print MACRO << "END_OF_TEXT" ;
# # 
# # dele atom sele active .a around key protein dist 1.6 end
# # rena segm SKEL sele skeleton end
# # 
# # set vari WORK_SEGM global = "SKEL NEW"
# # <?MAIN_CMDS:use_atoms.cmds WORK 
# # 
# # END_OF_TEXT
# # }

    print MACRO << "END_OF_TEXT" ;

! initial hybridization

< ?MAIN_CMDS:trace_3angl.cmds WORK_SEGM 
< ?MAIN_CMDS:trace_chain.cmds WORK_SEGM 

! symmetry overlap check

< ?MAIN_CMDS:trace_symm_overlaps.cmds WORK_SEGM 
< ?MAIN_CMDS:trace_map.cmds WORK_SEGM GRADIENT 

dele atom sele segm name WORK_SEGM .a weigh -0.1 0.1 end

< ?MAIN_CMDS:trace_3angl.cmds WORK_SEGM 
< ?MAIN_CMDS:trace_chain.cmds WORK_SEGM 


END_OF_TEXT

if ( ${$my_main{"RoH_SECONDARY"}}[0] eq "ON" ) 
{
    $secnd++ ;

    print MACRO << "END_OF_TEXT" ;

! secondary search included

< ?MAIN_CMDS:trace_chain.cmds WORK_SEGM 
< ?MAIN_CMDS:trace_secondary.cmds WORK_SEGM 
< ?MAIN_CMDS:trace_secondary_merge.cmds WORK_SEGM 
< ?MAIN_CMDS:trace_secondary_clean.cmds WORK_SEGM 

< ?MAIN_CMDS:trace_map.cmds WORK_SEGM GRADIENT 
< ?MAIN_CMDS:trace_3angl.cmds WORK_SEGM 
< ?MAIN_CMDS:trace_chain.cmds WORK_SEGM 

END_OF_TEXT
}

    print MACRO << "END_OF_TEXT" ;

< ?MAIN_CMDS:trace_3angl.cmds WORK_SEGM 
< ?MAIN_CMDS:trace_chain.cmds WORK_SEGM 

END_OF_TEXT

if ( ${$my_main{"RoH_REFINE"}}[0] eq "ON" && $icyc == $ncyc - 1 ) 
{
    print MACRO << "END_OF_TEXT" ;

! refine with refine.cmds

set temp sele segm name WORK_SEGM = 24

< ?MAIN_CMDS:refine_target.cmds REF_XLSQ 
< ?MAIN_CMDS:refine.cmds WORK_SEGM MAP_WORK 

END_OF_TEXT
}

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

shell create_phase_comb.pl -r ${$my_main{'DFR_RES_MIN'}}[0] $tmp 

< ?MAIN_CMDS:phase_comb.cmds WORK_SEGM MAP_2FOFC

END_OF_TEXT
}


    }

if ( $file_save ) 
{
    my $file_pdb = join ( '', $file_save, '_', $tmp, '.pdb' ) ;
    my $file_ctab = join ( '', $file_save, '_', $tmp, '.ctab' ) ;
    print " saving: $file_pdb $file_ctab \n";


    print MACRO << "END_OF_TEXT" ;

! save each big cycle

write over file $file_pdb sele segm name WORK_SEGM end coor pdb
write over file $file_ctab sele segm name WORK_SEGM end ctab \\ 
    first sele segm name WORK_SEGM end

END_OF_TEXT
}

}


print MACRO << "END_OF_TEXT" ;

return

END_OF_TEXT

close(MACRO) ;


