#!/bin/sh
# -*- mode: sh; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-

# The idea here is to run ansible_local.bash --tags daily
# and then use this to do the parsing and throwing errors based on the output.
# This way the ansible run can be free from erroring and this can be
# run repeatedly anytime outside of ansible to deal with the issues raised.
# It is also run at the end of ansible_local.bash --tags daily to raise the issues.

prog=`basename $0 .bash`
PREFIX=/usr/local
ROLE=toxcore
[ -f /usr/local/etc/testforge/testforge.bash ] && \
    . /usr/local/etc/testforge/testforge.bash
TOXCORE_LOG_DIR=$PREFIX/var/log
[ -d $TOXCORE_LOG_DIR ] || mkdir -p $TOXCORE_LOG_DIR
. /usr/local/src/usr_local_src.bash

TOHOST=files.pythonhosted.org
# mirrors.dotsrc.org

. /usr/local/bin/proxy_export.bash
PL=/usr/local/bin/proxy_ping_lib.bash
. $PL
PL=

MYID=`id -u`
[ $MYID -eq 0 ] || WARN $prog should be run as root $MYID

which ansifilter >/dev/null 2>&1  && ansifilter=ansifilter || ansifilter=cat

ly=daily
i=0
errs=0
warns=0
WLOG="$TOXCORE_LOG_DIR"/$ly/W$prog$$.log
ELOG="$TOXCORE_LOG_DIR"/$ly/E$prog$$.log

#?ols_make_testforge_logs $TOXCORE_LOG_DIR

[ -d /usr/local/share/doc ] || mkdir -p /usr/local/share/doc
[ -d /var/local/share/doc/txt ] && [ ! -d /usr/local/share/doc/txt ] && \
    mv /var/local/share/doc/txt /usr/local/share/doc/txt && \
    ln -s /usr/local/share/doc/txt /var/local/share/doc/txt

find "$TOXCORE_LOG_DIR"/$ly/ -type f \
     -name W${prog}*.log  -o -name E${prog}\*.log -mtime +8 -delete >/dev/null

[ -z "$MODE" ] || MODE=`proxy_ping_mode`

if [ -d /etc/libvirt/qemu ] ; then
    elt=qemu
    DBUG elt=$elt
    # -%d
    if ls /var/log/libvirt/qemu/*.log 2>/dev/null >/dev/null ; then
	sudo grep ^`date +%Y-%m`.*warning /var/log/libvirt/qemu/*.log | \
	    grep -v 'Failed to open SPICE sockets\|Spice: Connection reset by peer' | \
	    tee -a $WLOG
    fi

    # FixMe missing
    [ -x $PREFIX/bin/toxcore_libvirt_test_xml.bash ] && \
	$PREFIX/bin/toxcore_libvirt_test_xml.bash 2>&1 | \
	grep WARN: |tee -a $WLOG

    if which virt-host-validate 2>/dev/null ; then
	[ -f $TOXCORE_LOG_DIR/daily/virt-host-validate.log ] || \
	    sudo virt-host-validate > $TOXCORE_LOG_DIR/daily/virt-host-validate.log 2>&1
	b=`grep FAIL $TOXCORE_LOG_DIR/daily/virt-host-validate.log|wc -l|sed -e 's/ .*//'`
	[ $? -eq 0 -a -n "$b" -a $b -gt 0 ]
	b=`grep WARN $TOXCORE_LOG_DIR/daily/virt-host-validate.log|wc -l|sed -e 's/ .*//'`
	[ $? -eq 0 -a -n "$b" -a $b -gt 0 ] && \
	    WARN $b WARN in $TOXCORE_LOG_DIR/$ly/virt-host-validate.log $warns | tee -a  $WLOG
    fi

    if /etc/init.d/libvirtd status ; then
if [ "$MODE" = whonix ] ; then
    elt=toxcore_libvirt_test_ga
    DBUG $elt
    if virsh net-list | grep -q External ; then
	/usr/local/bin/toxcore_libvirt_test_ga.bash
    fi
fi
	virsh list | grep '^ [0-9]' | while read id elt rest ; do
	    [ $rest = running ] || continue
	    virsh dumpxml $elt | grep -q org.qemu.guest_agent.0.*connected || \
		WARN org.qemu.guest_agent not connected for $elt |tee -a $WLOG
	    #      <target type='virtio' name='com.redhat.spice.0' state='connected'/>
	    #      <target type='virtio' name='org.qemu.guest_agent.0' state='connected'/>
	done
    fi
fi

export SSLTEST_CERTS="/etc/ssl/certs/ca-certificates.crt"
export SSLTEST_TESTS="testssl nmap"
if route | grep -q def ; then
    elt=testforge_ssl_test
    DBUG $elt
    $PREFIX/bin/testforge_ssl_test.bash -v 3 $TOHOST
    retval=$?
    if [ $retval -ne 0 ] ; then
	ERROR retval=$retval testforge_ssl_test.bash -v 3 $TOHOST|tee -a $ELOG
    else
	INFO testforge_ssl_test.bash -v 3 $TOHOST
    fi
fi

elt=testforge_dirmngr_test
if route | grep -q default ; then
    DBUG $elt
    $PREFIX/bin/testforge_dirmngr_test.bash
    retval=$?
    if [ $retval -ne 0 ] ; then
	ERROR retval=$retval testforge_dirmngr_test.bash | tee -a $ELOG
    else
	INFO testforge_dirmngr_test.bash
    fi
fi

if [ -s $ELOG ] ; then
errs=`grep -c ERROR: "$ELOG"`
[ $errs -ne 0 ] && \
  ERROR "$prog $ly $errs errors in $ELOG" && \
  exit -$errs
fi
if [ -s $WLOG ] ; then
warns=`grep -c WARN: "$WLOG"`
[ $warns -ne 0 ] && \
  WARN "$prog $ly $warns warnings in $WLOG"
fi

[ $warns -eq 0 -a $errs -eq 0 ] && \
    ols_clean_testforge_logs $TOXCORE_LOG_DIR && \
    INFO  "No $ly errors in $TOXCORE_LOG_DIR"

exit 0
