#!/usr/bin/perl
#This routine updates the geometry section in Molpro input file.

if( $ARGV[0] eq '-h' || $ARGV[0] eq '-help')
{
help();
exit;
}
sub help {
  print "\nUpdate the geometry section of the Molpro input file from a pxyz or xyz file.\n"; 
  print "Please find an example name_translation file in the manual or translation_file directory.\n";
  print "Usage: pxyz_molpro_upd_geom  template.inp  name_translation  [file.pxyz] \n\n";
}



$templ=$ARGV[0];shift; #This is Molpro file
$nametrans=$ARGV[0];shift; #This is name translation information

die "ERROR: Molpro template file: $templ expected!\n" unless -e $templ;
die "ERROR: NameTranslation file: $nametrans expected!\n" unless -e $nametrans;
$shlwarn=0;

open (nameinfo,"<", $nametrans);
while (<nameinfo>){
chomp;
if ((substr $_, 0, 1) eq "#") {next;}
($name,$QMnam)=split;
$trans{$name} = $QMnam;
}


open (molpro,"<", $templ); 
while (<molpro>) {
 unless (/GEOMETRY/) {print ;}
 else {
   print $_;
   $_=<>;chomp;$natms=$_;
   $_=<>;$pbc=$_;
   print $natms."\n";
   print "comment\n";
   for($i=0;$i<$natms;$i++){
     $_=<>; chomp; ($nam,$xxx,$yyy,$zzz,$mol,$mar)=split;
     if ( $trans{$nam} ){
     $tmpname=$trans{$nam} }
     else {$tmpname = $nam;$shlwarn=1};
     print "$tmpname,   $xxx,   $yyy,   $zzz\n";
   }
   do {$_=<molpro>} until ( $_ =~ /}/);
   print $_;
 }
} 

print STDERR "Warning: At least one atom name is not in the NameTranslation file.\n" if ($shlwarn);

