#!/bin/sh
# Copyright (c) 2010-2013 Index Data, http://www.indexdata.com
#
# pazpar2-error-statistic - generate daily reliablity statistics

# who will get the statistics. Set the environment variable mailto to 
# change the list of recipients
: ${mailto='wosch'} #test

# display ebsco peak statistics, set to "YES" or ""
: ${EBSCO=""}

# die on error
set -e

umask 002

# LANG=en_US.UTF-8 make egrep 10 times slower
export LANG=C

# full IndexData email address
mailto=`perl -e 'print join(" ", map { /\@/ ? $_ : $_ . "\@indexdata.com" } @ARGV)' $mailto`

dir=`mktemp  -d`

# use development installation if exists
if [ -d /home/indexdata/metaproxy-statistic/bin ]; then 
   cd /home/indexdata/metaproxy-statistic/bin 
else
   cd /usr/share/metaproxy-statistic/bin
fi

# overview of failed z39.50 connections
make -s week | tee /var/lib/cf-repo/.statistic/.reliability-week.csv > $dir/reliability-week.csv
make -s week-full > $dir/reliability-week-details.csv
make -s week-full-query | tee /var/lib/cf-repo/.statistic/.reliability-week-full-query.csv > $dir/reliability-week-with-queries.csv

make -s today	   > /var/lib/cf-repo/.statistic/.reliability-today.csv
make -s today-full > /var/lib/cf-repo/.statistic/.reliability-today-full.csv
make -s today-full-query | tee /var/lib/cf-repo/.statistic/.reliability-today-full-query.csv > $dir/reliability-today-with-queries.csv

make -s yesterday-full-query > /var/lib/cf-repo/.statistic/.reliability-yesterday-full-query.csv

# detailed errors
cat <<EOF > $dir/bib1.txt

----------------------------------------------------------------------
Bib-1 Diagnostics: http://www.loc.gov/z3950/agency/defns/bib1diag.html
2 	temporary system error 
3 	unsupported search 
13 	Present request out-of-range 
109 	Database unavailable 
114 	Unsupported Use attribute 
239 	Record syntax not supported 
----------------------------------------------------------------------

EOF



subject="pazpar2 statistic at `hostname`"

attachments=""
for i in $dir/*.csv
do
   if [ -f $i ]; then
	attachments="$attachments -a $i"
   fi
done

( 
  echo "nightly pazpar2 statistics: `date`" 
  cat $dir/bib1.txt
) | mutt -s "$subject" $attachments -- $mailto

rm -rf $dir

