#!/bin/sh


# Prefer gawk - we know exactly what it can do.
# awk on Sun does not support functions, need to use nawk for this
if gawk '{print 1}'</dev/null > /dev/null 2>&1; then
   AWK=gawk
elif nawk '{print 1}'</dev/null > /dev/null 2>&1; then
   AWK=nawk
else
   AWK=awk
fi


# check_final_energy:
# $1 - expected energy
# $2 - max deviation
check_final_energy() {
[ "$2" = "" ] &&  echo "not enough parameters to check_final_energy."
$AWK 'function e(v,r){#print "check_final_energy: checking ",v, r;
return(v>r||v<-r)}
/FINAL ENERGY/{c=1; printf "computed  %15.9f\nreference %15.9f\n", $4, '$1'; exit (e($4-('$1'),'$2'))} END{if(!c) exit(1)}
' ergoscf.out
}

# check_dipole:
# $1 - x, y, or z
# $2 - expected dipole moment component
# $3 - max deviation
check_dipole() {
[ "$3" = "" ] &&  echo "not enough parameters to check_dipole."
$AWK 'function e(v,r){#print "check_dipole: checking ",v, r;
return(v>r||v<-r)}
/INSC dipole_moment_'$1'/{c=1; printf "computed  %15.9f\nreference %15.9f\n", $6, '$2'; exit (e($6-('$2'),'$3'))} END{if(!c) exit(1)}
' ergoscf.out
}

# check_mulliken_charge:
# $1 - atom index
# $2 - expected Mulliken atomic charge
# $3 - max deviation
check_mulliken_charge() {
[ "$3" = "" ] &&  echo "not enough parameters to check_mulliken_charge."
$AWK 'function e(v,r){#print "check_mulliken_charge: checking ",v, r;
return(v>r||v<-r)}
/INSC Mulliken charge of atom '$1'/{c=1; printf "computed  %15.9f\nreference %15.9f\n", $8, '$2'; exit (e($8-('$2'),'$3'))} END{if(!c) exit(1)}
' ergoscf.out
}

# check_mulliken_spin:
# $1 - atom index
# $2 - expected Mulliken atomic spin density
# $3 - max deviation
check_mulliken_spin() {
[ "$3" = "" ] &&  echo "not enough parameters to check_mulliken_spin."
$AWK 'function e(v,r){#print "check_mulliken_spin: checking ",v, r;
return(v>r||v<-r)}
/INSC Mulliken spin density of atom '$1'/{c=1; printf "computed  %15.9f\nreference %15.9f\n", $9, '$2'; exit (e($9-('$2'),'$3'))} END{if(!c) exit(1)}
' ergoscf.out
}

# check_final_ci_energy:
# $1 - expected CI energy
# $2 - max deviation
check_final_ci_energy() {
[ "$2" = "" ] &&  echo "not enough parameters to check_final_ci_energy."
$AWK 'function e(v,r){#print "check_final_energy: checking ",v, r;
return(v>r||v<-r)}
/FINAL CI ENERGY/{c=1; printf "computed  %15.9f\nreference %15.9f\n", $6, '$1'; exit (e($6-('$1'),'$2'))} END{if(!c) exit(1)}
' ergoscf.out
}

# check_final_ci_corr_energy:
# $1 - expected CI correlation energy
# $2 - max deviation
check_final_ci_corr_energy() {
[ "$2" = "" ] &&  echo "not enough parameters to check_final_ci_corr_energy."
$AWK 'function e(v,r){#print "check_final_energy: checking ",v, r;
return(v>r||v<-r)}
/FINAL CI CORRELATION ENERGY/{c=1; printf "computed  %15.9f\nreference %15.9f\n", $7, '$1'; exit (e($7-('$1'),'$2'))} END{if(!c) exit(1)}
' ergoscf.out
}

# check_final_S2:
# $1 - expected <S2>
# $2 - max deviation
check_final_S2() {
[ "$2" = "" ] &&  echo "not enough parameters to check_final_S2."
$AWK 'function e(v,r){#print "check_final_S2: checking ",v, r; 
return(v>r||v<-r)}
/FINAL <S2>/{c=1; printf "computed  %15.9f\nreference %15.9f\n", $5, '$1'; exit (e($5-('$1'),'$2'))} END{if(!c) exit(1)}
' ergoscf.out
}

# check_tdhf_dipole_at_time
# $1 - time as integer
# $2 - expected dipole value at that time
# $3 - max deviation
check_tdhf_dipole_at_time()
{
[ "$3" = "" ] &&  echo "not enough parameters for check_tdhf_dipole_at_time."
# echo check_tdhf_dipole_at_time, params: $1 $2 $3
valuewithreversedsign=`echo $2 '*' -1 | bc -l`
$AWK 'function e(v,r){return(v>r||v<-r)}
/^INSC Data for dipole plot/ && $6=='$1'{c=1; printf "computed  %15.9f\nreference %15.9f\n", $7, '$valuewithreversedsign'; exit (e($7-('$valuewithreversedsign'),'$3'))} END{if(!c) { print "Not found";exit(1)}}
' ergoscf.out
}

# check_lr_eigenvalue:
# $1 - eigenvalue number
# $2 - expected eigenvalue
# $3 - max deviation
check_lr_eigenvalue()
{
[ "$3" = "" ] &&  echo "not enough parameters to check_lr_eigenvalue."
$AWK 'function e(v,r){return(v>r||v<-r)}
/^ERMA Eigenvalue / && $3=="'$1':"{c=1; printf "computed  %15.9f\nreference %15.9f\n", $4, '$2'; exit (e($4-('$2'),'$3'))} END{if(!c) { print "Not found";exit(1)}}
' ergoscf.out
}

# check_polarisability:
# $1 - left-hand-side operator (X,Y,Z)
# $2 - right-hand-side operator (X,Y,Z)
# $3 - frequency
# $4 - reference value
# $5 - max deviation
check_polarisability()
{
[ "$5" = "" ] &&  echo "not enough parameters to check_polarizability."
$AWK 'function e(v,r){return(v>r||v<-r)}
/^REMA Response / && $4 == "'$1'" && $6 == "'$2'" && $10 ~ /^'$3'0*:/ {
c=1; #print;printf "computed  %15.9f\nreference %15.9f\n", $11, '$4';
  exit (e($11-('$4'),'$5'))} END{if(!c) { print "Not found";exit(1)}}
' ergoscf.out
}

