#!/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 $MACRO_FILE = "gen_solv_rem_shift.com" ;
my $PAIR_FILE = "pair_list.dat" ;
my $TOO_CLOSE = 1.5 ;
my $TOO_FAR = 4.5 ;
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 'i' || substr($keyword, 0,5) eq "input" ) { goto INPUT }
    if ( $keyword eq 'c' || substr($keyword, 0,5) eq "close" ) { goto CLOSE }
    if ( $keyword eq 'f' || substr($keyword, 0,3) eq "far" ) { goto FAR }
    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]
  -i|--input)   set file name of the pair distance list [$PAIR_FILE]
  -c|--close)   close cutoff value [$TOO_CLOSE]
  -f|--far)     far cutoff value [$TOO_FAR]
     --doit)    create the macro
END_OF_TEXT
    exit;

OUTPUT: $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
INPUT: $PAIR_FILE = shift ( @{$commands{$keyword}} ) ; next ;
CLOSE: $TOO_CLOSE = shift ( @{$commands{$keyword}} ) ; next ;
FAR: $TOO_FAR = shift ( @{$commands{$keyword}} ) ; next ;
}
if ( $PROC_ERROR eq "TRUE" ) { exit ; }

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

if ( $doit eq "FALSE" )  { exit ; }

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

do_pair_list ( $PAIR_FILE, $MACRO_FILE, $TOO_CLOSE, $TOO_FAR ) ;

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

sub do_pair_list 
{
    my ( $PDB_FILE, $MACRO_FILE, $TOO_NEAR, $TOO_FAR ) = @_ ;
    my @records = () ;

print "reading LIST file \"$PDB_FILE\" \n" ;
open ( LIST,  "< $PDB_FILE" ) || die "FILE OPEN ERROR: >$PDB_FILE< \n" ;


while (<LIST> ) { push(@records, $_) ; } close ( LIST ) ;


    my $line;
    my $sub_line ;
    my $i1 ; my $i2; my $sq; my $ds ;

    shift (@records) ;
    shift (@records) ;
    shift (@records) ;

    my %seq_list ;

    foreach $line ( @records )
    {
	chomp ($line) ;
#	print "[$line]\n" ;
	$sub_line = $line ;
	$i1 = substr($sub_line, 0, 6 ); 
	$i2 = substr($sub_line, 29, 6 ); 
	$sq = substr($sub_line, 22, 5 ); 
	$ds = substr($sub_line, 56, 7 ); 
#	print "args: $i1 $i2 <$sq> $ds \n" ;
	push( @{$seq_list{$sq}{"i1"}}, $i1 ) ;
	push( @{$seq_list{$sq}{"i2"}}, $i2 ) ;
	push( @{$seq_list{$sq}{"ds"}}, $ds ) ;
    }

    my @macro_lines = () ;
    push ( @macro_lines, "key del_atom sele .n all end \n" ) ; 
    foreach $sq ( sort ( keys %seq_list ))
    {
#	print $sq, @{$seq_list{$sq}{"ds"}}, "\n" ;
	my @dists =  @{$seq_list{$sq}{"ds"}} ;
	my $iatom = $seq_list{$sq}{"i1"}[0] ;
	my $nn = scalar(  @{$seq_list{$sq}{"ds"}} ) ;
	my %list = () ;
	for ( my $i = 0; $i<$nn; $i++ )
	{
	    $list{$dists[$i]} = $seq_list{$sq}{"i1"}[$i] ;
	}
	foreach $ds ( sort keys %list )
	{
#	    print $ds, $list{$ds}, "\n" ;
	    if ( $ds < $TOO_NEAR || $ds > $TOO_FAR ) 
	    { push ( @macro_lines, "key del_atom sele del_atom .o by resi atom numb $iatom end \n" ) ; }
	    elsif (  $list{$ds} ne $iatom )
	    {
	    push (@macro_lines, "copy coor sele by res atom numb $list{$ds} end sele by res atom numb $iatom end \n" );
	    }
	    last ;
	}
    }

open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ;
print MACRO @macro_lines ;
close ( MACRO ) ;
}
