#!/bin/bash
version=0.1.3
rm -f tempuff1 tempuff2 tempuff3 tempuff4
cat $1.head | tr -d '\r' | sed 's/#\(.*\)/#\1 force nosym test units=au/'> tempuff1
chk=$(grep -Pom1  '^ *%chk *= *\K.+' $1.head)
test -f "$chk" && sed -i 's/#\(.*\)/#\1 guess=read/' tempuff1
echo "~~~external g16 for $1~~~"
cat tempuff1
echo "~~~~~~~~~~~~~~~~~~~~~~~~~"
cat $2 | awk '{
if(NR==1) {
  printf "\ntest\n\n%3d%3d\n",$3,$4
  n = $1 }
if(NR!=1 && NR<=(n+1)) print $1,$2,$3,$4
if(NR==(n+1)) printf " \n" }' >> tempuff1
test -f $1.tail && cat $1.tail >> tempuff1
g16 < tempuff1 > tempuff2
awk '{if($2=="Done:") printf "%20.12e%20.12e%20.12e%20.12e\n", $5,0.0,0.0,0.0}' tempuff2 |tail -n1 > tempuff3
awk '/Center     Atomic                   Forces/,/Cartesian Forces/' tempuff2 | awk '{if((NR-3)==$1) printf "%20.12e%20.12e%20.12e\n",-$3,-$4,-$5}' > tempuff4
cat tempuff3 tempuff4 > $3
rm tempuff1 tempuff2 tempuff3 tempuff4
