#!/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_MODEL_FORM"} ) ) { push ( @{$my_main{"FILE_MODEL_FORM"}}, "MAIN_PDB" ) };
if ( !exists($my_main{"FILE_MODEL_SELE"} ) ) { push ( @{$my_main{"FILE_MODEL_SELE"}}, "WORK" ) };
if ( !exists($my_main{"WAT_SEGM"} ) ) { @{$my_main{"WAT_SEGM"}} = ("W", "W2" ) };
if ( !exists($my_main{"FILE_MODEL_MY_SELE"} ) ) { push ( @{$my_main{"FILE_MODEL_MY_SELE"}}, ".n all" ) };
if ( !exists($my_main{"FILE_MODEL_HYDROGENS"} ) ) { push ( @{$my_main{"FILE_MODEL_HYDROGENS"}}, ".n all" ) };

#########################################################
my $MACRO_FILE = "save_file.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;
my @DUM = split('\.', ${$my_main{"FILE_MODEL"}}[0] ) ;
#print "dumm: ",  $DUM[0], @{$my_main{"FILE_MODEL"}}, "\n" ;
my $SAVE_FILE_ROOT = $DUM[0] ;
my $SAVE_FILE_FORM = ${$my_main{"FILE_MODEL_FORM"}}[0] ;
my $SAVE_FILE_SELE = ${$my_main{"FILE_MODEL_SELE"}}[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 'g' || substr($keyword, 0,9) eq "selection" ) { goto SELECTION}
    if ( $keyword eq 's' || substr($keyword, 0,9) eq "save_file" ) { goto SAVE_FILE }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "form" ) { goto FORM }
    if ( $keyword eq 'y' || substr($keyword, 0,9) eq "hydrogens" ) { goto HYDROGENS }
    if ( $keyword eq 'm' || substr($keyword, 0,12) eq "my_selection" ) { goto MY_SELECTION }
    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 
  -f|--form)         formats of molecular model save file [MAIN_PDB/STD_PDB/mmCIF/STD_mmCIF] [$SAVE_FILE_FORM] 
  -g|--selection)    selection rules [WORK/ALL/ACTIVE/MY_SELECTION] [$SAVE_FILE_SELE] 
  -m|--my_selection) my selection [@{$my_main{"FILE_MODEL_MY_SELE"}}]
  -h|--hydrogens)    hydrogens included [INCLUDE/EXCLUDE] [@{$my_main{"FILE_MODEL_HYDROGENS"}}] 
  -s|--save_file)    root words of files [$SAVE_FILE_ROOT] 
  -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 ;
 FORM: $SAVE_FILE_FORM = shift ( @{$commands{$keyword}} ) ; next ;
 SELECTION: $SAVE_FILE_SELE = shift ( @{$commands{$keyword}} ) ; next ;
 SAVE_FILE: $SAVE_FILE_ROOT = shift ( @{$commands{$keyword}} ) ; next ;
 HYDROGENS: @{$my_main{"FILE_MODEL_HYDROGENS"}} = @{$commands{$keyword}} ; next ;
 MY_SELECTION: @{$my_main{"FILE_MODEL_MY_SELE"}} = @{$commands{$keyword}} ; next ;
}

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

@{$my_main{"FILE_MODEL"}} = ($SAVE_FILE_ROOT . '.PDB' );
@{$my_main{"FILE_CTAB"}} = ($SAVE_FILE_ROOT . '.CTAB' );
@{$my_main{"FILE_MODEL_FORM"}} = ( $SAVE_FILE_FORM ) ;
@{$my_main{"FILE_MODEL_SELE"}} = ( $SAVE_FILE_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" ;


$SAVE_FILE_SELE = ${$my_main{"FILE_MODEL_SELE"}}[0] ;

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

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

my $FORM = "pdb" ;
my $EXTENSION = ".PDB" ;
if ( $SAVE_FILE_FORM eq "STD_PDB" ) { $FORM = "pdb stand" ; }
if ( uc($SAVE_FILE_FORM) eq "MMCIF" ) 
{ 
    $FORM = "mmcif" ; 
}
if ( uc($SAVE_FILE_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_list end
!delete atom select t_anchor end
END_OF_TEXT

if ( $SAVE_FILE_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_list end auto 0
rena seq \"\" sele $save_list end strip
make chain sele $save_list end
show temp sele weigh 0.01 1.1 .a $save_list end

END_OF_TEXT


}

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

if ( uc($SAVE_FILE_FORM) eq "MMCIF" ) 
{ 

print MACRO << "END_OF_TEXT" ;
write over file $SAVE_FILE_ROOT + ".mmCIF" select $save_list end atom $FORM
write over file $SAVE_FILE_ROOT + ".CTAB" select $save_list end ctab \\ 
           first sele  $save_list  end
write over file $SAVE_FILE_ROOT + ".HBOND" select $save_list end hbond
save over file $SAVE_FILE_ROOT + ".VIEW" view
save over file $SAVE_FILE_ROOT + ".MAP" map
save over file $SAVE_FILE_ROOT + ".CIF" refine
return
END_OF_TEXT

}
elsif ( uc($SAVE_FILE_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 $save_list end strip

write over file $SAVE_FILE_ROOT + ".mmCIF" select $save_list end atom $FORM
write over file $SAVE_FILE_ROOT + ".CTAB" select $save_list end ctab \\ 
           first sele  $save_list  end
write over file $SAVE_FILE_ROOT + ".HBOND" select $save_list end hbond
save over file $SAVE_FILE_ROOT + ".VIEW" view
save over file $SAVE_FILE_ROOT + ".MAP" map
return
END_OF_TEXT

}
else
{

print MACRO << "END_OF_TEXT" ;
write over file $SAVE_FILE_ROOT + ".PDB" select $save_list end atom $FORM
write over file $SAVE_FILE_ROOT + ".CTAB" select $save_list end ctab \\ 
           first sele  $save_list  end
write over file $SAVE_FILE_ROOT + ".HBOND" select $save_list end hbond
save over file $SAVE_FILE_ROOT + ".VIEW" view
save over file $SAVE_FILE_ROOT + ".MAP" map
save over file $SAVE_FILE_ROOT + ".CIF" refine
return
END_OF_TEXT
}
    
close ( MACRO ) ;
