#!/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{"FILE_DEP_MODEL"} ) )     { push ( @{$my_main{"FILE_DEP_MODEL"}}, "atom_4deposit.mmCIF" ) };
if ( !exists($my_main{"FILE_DEP_FORM"} ) )      { push ( @{$my_main{"FILE_DEP_FORM"}}, ("STD_mmCIF") ) };
if ( !exists($my_main{"FILE_DEP_SELE_ART"} ) )  { push ( @{$my_main{"FILE_DEP_SELE_ART"}}, "ALL" ) };
if ( !exists($my_main{"FILE_DEP_RENUMBER"} ) )  { push ( @{$my_main{"FILE_DEP_RENUMBER"}}, "W", "W2" ) };
if ( !exists($my_main{"FILE_DEP_MY_SELE"} ) )   { push ( @{$my_main{"FILE_DEP_MY_SELE"}}, ".n all" ) };
if ( !exists($my_main{"FILE_DEP_HYDROGENS"} ) ) { push ( @{$my_main{"FILE_DEP_HYDROGENS"}}, ".n all" ) };

#########################################################
my $MACRO_FILE = "save_file-4dep.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;
my @DUM = split('\.', ${$my_main{"FILE_DEP_MODEL"}}[0] ) ;
#print "dumm: ",  $DUM[0], @{$my_main{"FILE_MODEL"}}, "\n" ;
my $FILE_DEP_ROOT = $DUM[0] ;
my $FILE_DEP_FORM = ${$my_main{"FILE_DEP_FORM"}}[0] ;
my $SEG_ID_RENUMBER = join(" ", @{$my_main{"FILE_DEP_RENUMBER"}}) ;
my $FILE_DEP_SELE_ART = ${$my_main{"FILE_DEP_SELE_ART"}}[0] ; 
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 'f' || substr($keyword, 0,4) eq "form" ) { goto FORM }
    if ( $keyword eq 'g' || substr($keyword, 0,9) eq "selection" ) { goto SELECTION}
    if ( $keyword eq 'y' || substr($keyword, 0,9) eq "hydrogens" ) { goto HYDROGENS }
    if ( $keyword eq 'l' || substr($keyword, 0,12) eq "my_selection" ) { goto MY_SELECTION }
    if ( $keyword eq 'r' || substr($keyword, 0,8) eq "renumber" ) { goto RENUMBER }
    if ( $keyword eq 'm' || substr($keyword, 0,9) eq "macro" ) { goto MACRO }
    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 root of output file [ $FILE_DEP_ROOT]
  -f|--form)         formats of molecular model deposition [STD_PDB/STD_mmCIF] [$FILE_DEP_FORM] 
  -g|--selection)    selection art [WORK/ALL/ACTIVE/MY_SELECTION] [$FILE_DEP_SELE_ART] 
  -l|--my_selection) my selection [@{$my_main{"FILE_DEP_MY_SELE"}}]
  -h|--hydrogens)    hydrogens [INCLUDE/EXCLUDE] [@{$my_main{"FILE_DEP_HYDROGENS"}}] 
  -r|--renumber)     renumber following segment IDs [$SEG_ID_RENUMBER] 
  -m|--macro)        set file name of the created macro [$MACRO_FILE]
     --doit)         create the macro 
END_OF_TEXT
    exit;
 OUTPUT: $FILE_DEP_ROOT = shift ( @{$commands{$keyword}} ) ; next ;
 FORM: $FILE_DEP_FORM = shift ( @{$commands{$keyword}} ) ; next ;
 SELECTION: $FILE_DEP_SELE_ART = shift ( @{$commands{$keyword}} ) ; next ;
 HYDROGENS: @{$my_main{"FILE_DEP_HYDROGENS"}} = shift (@{$commands{$keyword}} ); next ;
 MY_SELECTION: @{$my_main{"FILE_DEP_MY_SELE"}} = @{$commands{$keyword}} ; next ;
 RENUMBER: @{$my_main{"FILE_DEP_RENUMBER"}} = @{$commands{$keyword}} ; next ;
 MACRO: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
}

if ( $PROC_ERROR eq "TRUE" ) { exit ; }

@{$my_main{"FILE_DEP_MODEL"}} = ($FILE_DEP_ROOT . '.PDB' );
@{$my_main{"FILE_DEP_CTAB"}} = ($FILE_DEP_ROOT . '.CTAB' );
@{$my_main{"FILE_DEP_FORM"}} = ( $FILE_DEP_FORM ) ;
@{$my_main{"FILE_DEP_SELE_ART"}} = ( $FILE_DEP_SELE_ART ) ;
my $FILE_DEP_MY_SELE = join ( " ", @{$my_main{"FILE_DEP_MY_SELE"}} ) ;

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

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


$FILE_DEP_SELE_ART = ${$my_main{"FILE_DEP_SELE_ART"}}[0] ;

my $save_selection ;
if ( $FILE_DEP_SELE_ART eq "WORK" ) 
{
$save_selection = "segment name SEGMENTS" ;
}
elsif ( $FILE_DEP_SELE_ART eq "ALL" ) 
{
$save_selection = "all" ;
}
elsif ( $FILE_DEP_SELE_ART eq "ACTIVE" ) 
{
$save_selection = "active" ;
}
elsif ( $FILE_DEP_SELE_ART eq "MY_SELECTION" ) 
{
$save_selection = join (" ", @{$my_main{"FILE_DEP_MY_SELE"}} ) ;
}
else 
{
$save_selection = "segment name SEGMENTS" ;
}

my $renumber_segments =  join (" ", "segm name",  @{$my_main{"FILE_DEP_RENUMBER"}} ) ; 



if ( $my_main{"SG_NCRYST"}[0] > 1 ) { $save_selection = ".n segm name #*" ; }

my $FORM = "pdb" ;
my $EXTENSION = ".PDB" ;
if ( $FILE_DEP_FORM eq "STD_PDB" ) { $FORM = "pdb stand" ; }
if ( uc($FILE_DEP_FORM) eq "MMCIF" ) 
{ 
    $FORM = "mmcif" ; 
}
if ( uc($FILE_DEP_FORM) eq "STD_MMCIF" ) { $FORM = "mmcif stand" ; }

my ( $WAT_SEGM, $WAT_SEGM_D) = @{$my_main{"WAT_SEGM"}} ;

print MACRO << "END_OF_TEXT" ;
subroutine char SEGMENTS
rena segm  $WAT_SEGM sele segm name $WAT_SEGM_D .a $save_selection end
!delete atom select t_anchor end
END_OF_TEXT

if ( $FILE_DEP_FORM eq "STD_PDB" ) 
{
print MACRO << "END_OF_TEXT" ;
set temp sele weigh -100.0 0.0 end = 0
rena seq \"\" sele segm name $WAT_SEGM .o resi name HOH \\
     .a $save_selection end auto 0
rena seq \"\" sele $save_selection end strip
make chain sele $save_selection end
show temp sele weigh 0.01 1.1 .a $save_selection end

END_OF_TEXT


}

if (   ${$my_main{"FILE_DEP_HYDROGENS"}}[0] eq "EXCLUDE" )
{
    print MACRO "dele atom sele atom name H* end\n\n" ;
}

if ( uc($FILE_DEP_FORM) eq "STD_MMCIF" ) 
{ 

my $STRATEGY;
my $GROUP ;
my $IGR ;

my $NCS_START = "FALSE" ;

for ( $IGR = 1; $IGR <= @{$my_main{"SG_NGROUP"}}[0] ; $IGR++ )
{
    $GROUP= join ( '', "SG_GROUP" , $IGR ) ;
    $STRATEGY = join ( '', "SG_GROUP" , $IGR, "_STRATEGY" ) ;
#    print " my group strategy: >",  $my_main{$STRATEGY}[0], "<\n" ;
    if ( $my_main{$STRATEGY}[0] eq "COMPOSIT" ) 
    { 
#    print " groups: $IGR, <$GROUP> \n" ;
	if ( $NCS_START eq "FALSE" )
	{ 
	    print MACRO "\n define constr occ init\n" ;
	    $NCS_START = "TRUE" ;
	}
	print MACRO " define constr occ \(auto \\\n" ;
	my @lines = () ;
	my $MEMBER ; my $NC = 0; 
	foreach $MEMBER ( @{$my_main{$GROUP}} )
	{
	    $NC++ ;
#	print " member: $MEMBER> ", $NC, scalar(@{$my_main{$GROUP}}), "\n" ;
	    if ( $NC < @{$my_main{$GROUP}} )
	    {
		print MACRO "     sele segm name $MEMBER end \\\n" ;
	    }
	    else
	    {
		print MACRO "     sele segm name $MEMBER end \)\n\n" ;
	    }
	}
    }
}

print MACRO << "END_OF_TEXT" ;

rena seq \"\" sele $renumber_segments end strip
rena chain W sele  $renumber_segments end

write over file $FILE_DEP_ROOT + ".mmCIF" select $save_selection end atom $FORM

return
END_OF_TEXT

}
else
{

print MACRO << "END_OF_TEXT" ;
write over file $FILE_DEP_ROOT + ".PDB" select $save_selection end atom $FORM
save over file $FILE_DEP_ROOT + ".CIF" refine
return
END_OF_TEXT
}
    
close ( MACRO ) ;
