#!/usr/sbin/perl
# redundant_picker.pl
#
# usage ./redundant_picker.pl g98out g98com outfile
#
# pulls out the redundant internal coordinates from
# a Gaussian 98 single point energy output file (.out)
# and cats them with the old .com file to make a 
# new .com file (outfile)
#
#
#                             D.E. VOLK  11/05/2001
$infile = shift(@ARGV);
$infile2 = shift(@ARGV);
$outfile = shift(@ARGV);
#
#
#  CREATE 3 TEMP FILES WITH BOND, ANGLE, AND DIHEDRAL CONSTRAINTS
#
open(MYIN, "$infile") || die "Cannot open file $infile";
open(OUT1, ">temp.bond") || die "Cannot open file temp.bond";
open(OUT2, ">temp.angle") || die "Cannot open file temp.angle";
open(OUT3, ">temp.dihed") || die "Cannot open file temp.dihed";
while (<MYIN>)
  {
   chomp;    #get rid of newline
   s/^\s+//;
   /\! R/ && (print OUT1 "$_\n");   # write bond restraint to temp.bond 
   /\! A/ && (print OUT2 "$_\n");   # write angle restraint to temp.angle 
   /\! D/ && (print OUT3 "$_\n");   # write dihed restraint to temp.dihed 
  }
close(MYIN);
close(OUT1);
close(OUT2);
close(OUT3);
#
#    ECHO BACK THE G98.COM FILE TO THE NEW .COM FILE
#
open(OLDCOM, "$infile2") || die "Cannot open file $infile2";
open(ALLOUT, ">$outfile") || die "Cannot open file distance.bond";
while (<OLDCOM>)
    {
     print ALLOUT "$_";
    } 
close(OLDCOM);
#
#
# 	READIN THE TEMP BOND RESTRAINT FILE, GRAB NEEDED PARTS
#       AND WRITE TO ALLOUT
#
open(TEMPIN, "temp.bond") || die "Cannot open file temp.bond";
while (<TEMPIN>)
  {
    chomp;    #get rid of newline
    s/^\s+//;
    ($v1,$v2,$v3,$v4,$v5,$v6,$v7)= split(/\s+/,$_); 
#
#  THIS PART PLUCKS 1 2 from R(1,2) from $v3
#
    ($s1,$s2) = split(/\(/,$v3);
    ($s3,$s4) = split(/\)/,$s2);
    ($s5,$s6) = split(/,/,$s3);
#
# Put values into final printing ("o") variables
    $o1=$s5;
    $o2=$s6;
    $o3="   ";
    $o4="   ";
    $o5=$v4;
    write ALLOUT; 
   }
close(TEMPIN);
#
#
# 	READIN THE TEMP ANGLE RESTRAINT FILE, GRAB NEEDED PARTS
#       AND WRITE TO ALLOUT
#
open(TEMPIN2, "temp.angle") || die "Cannot open file temp.angle";
while (<TEMPIN2>)
  {
    chomp;    #get rid of newline
    s/^\s+//;
    ($v1,$v2,$v3,$v4,$v5,$v6,$v7)= split(/\s+/,$_);
#
#  THIS PART PLUCKS 1 2 3 from A(1,2,3) from $v3
#
    ($s1,$s2) = split(/\(/,$v3);
    ($s3,$s4) = split(/\)/,$s2);
    ($s5,$s6,$s7) = split(/,/,$s3);
#
# Put values into final printing ("o") variables
    $o1=$s5;
    $o2=$s6;
    $o3=$s7;
    $o4="   ";
    $o5=$v4;
    write ALLOUT; 
   }
close(TEMPIN2);
#
#
#       READIN THE TEMP DIHED RESTRAINT FILE, GRAB NEEDED PARTS
#       AND WRITE TO ALLOUT
#
open(TEMPIN3, "temp.dihed") || die "Cannot open file temp.dihed";
while (<TEMPIN3>)
  {
    chomp;    #get rid of newline
    s/^\s+//;
    ($v1,$v2,$v3,$v4,$v5,$v6,$v7)= split(/\s+/,$_);
#
#  THIS PART PLUCKS 1 2 3 4 from D(1,2,3,4) from $v3
#
    ($s1,$s2) = split(/\(/,$v3);
    ($s3,$s4) = split(/\)/,$s2);
    ($s5,$s6,$s7,$s8) = split(/,/,$s3);
#
# Put values into final printing ("o") variables
    $o1=$s5;
    $o2=$s6;
    $o3=$s7;
    $o4=$s8;
    $o5=$v4;
    write ALLOUT;
   }
close(TEMPIN3);
#
# ADD BLANK LINE AT BOTTOM OF FINAL OUTPUT FILE
  print ALLOUT "\n";
close(ALLOUT);
#
# CLEAN UP TEMP FILES
if (-e "temp.bond") { unlink("temp.bond"); }
if (-e "temp.angle") { unlink("temp.angle"); }
if (-e "temp.dihed") { unlink("temp.dihed"); }
#
format ALLOUT =
@>>   @>>   @>>   @>>       @###.#### f
$o1,  $o2,  $o3,  $o4,        $o5
.
