#!/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
. /usr/locaal/etc/testforge/testforge.bash
TOXCORE_LOG_DIR=$PREFIX/var/log
[ -d $TOXCORE_LOG_DIR ] || mkdir -p $TOXCORE_LOG_DIR

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=hourly
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

find "$TOXCORE_LOG_DIR"/$ly/ -type f -name W${prog}*.log  -o -name E${prog}*.log -mtime +1 -delete

if virsh list | grep -q Whonix-Gateway ; then
  /usr/local/bin/toxcore_libvirt_test_ga.bash
fi

# -%d
if ls /var/log/libvirt/qemu/*.log 2>/dev/null ; then
  sudo grep ^`date +%Y-%m`.*warning /var/log/libvirt/qemu/*.log | tee -a $WLOG
fi


warns=`grep -c WARN: "$WLOG"`
[ $warns -ne 0 ] && \
  WARN "$prog $ly $warns warnings in $WLOG"

errs=`grep -c ERROR: "$ELOG"`
[ $errs -ne 0 ] && \
  ERROR "$prog $ly $errs errors in $ELOG" && \
  exit -$errs

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

exit 0
