#!/bin/csh -f
set fileid = `echo $$`
foreach log ($argv)
if (-e $log) then
set log1 = 'temp1_'"$fileid"
set log2 = 'temp2_'"$fileid"
@ ier = 0
set cm5 = `grep 'Center     Atomic      CM5         Hirshfeld' $log `
set natom = `grep ' NAtoms=' $log `
set coord = `grep '                        Standard orientation:' $log `
set hir1 = `grep ' Hirshfeld spin densities, charges and dipoles using' $log `
set hir2 = `grep ' Hirshfeld charges, spin densities, dipoles, and CM5 charges using' $log `
@ natom1 = $natom[2] +  4
@ natom2 = $natom[2] +  1
if ($#cm5 != 0) then
echo "File $log is already updated. Skipping"
@ ier = 1
endif
if ($#natom == 0) then
echo "File $log does not contain NAtoms=. Skipping"
@ ier = 2
endif
if ($#coord != 2) then
echo "File $log does not contain a set of Cartesian coordinates in standard orientation, or it contains more than one set. Skipping"
@ ier = 3
endif
if ($#hir1 == 0 && $#hir2 == 0) then
echo "File $log does not contain Hirshfeld charges or their output is not recognized. Skipping"
@ ier = 4
endif
if ($ier == 0) then
echo "$natom[2]" > $log1
grep -A $natom1 '                        Standard orientation:' $log | sed '1,5d' | awk '{print $2" "$4" "$5" "$6}' >> $log1
if ($#hir1 != 0) then
grep -A $natom2 ' Hirshfeld spin densities, charges and dipoles using' $log | sed '1,2d' | awk '{print $4}' >> $log1
endif
if ($#hir2 != 0) then
grep -A $natom2 ' Hirshfeld charges, spin densities, dipoles, and CM5 charges using' $log | sed '1,2d' | awk '{print $3}' >> $log1
endif
cm5pac.exe < $log1 > $log2
set err = `grep 'CM5ERROR' $log2 `
if ($#err != 0) then
echo "Unexpected error in file $log. Skipping"
else
cat $log2 >> $log
endif
rm -f $log1 $log2
endif
endif
end