#!/bin/sh
# PCP QA Test No. 566
# basic pmlogcheck workout
#
# Copyright (c) 2013 Ken McDonell.  All Rights Reserved.
# Copyright (c) 2015 Red Hat Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

status=1	# failure is the default!
trap "cd $here; rm -rf $tmp $tmp.*; exit \$status" 0 1 2 3 15
mkdir $tmp

_filter_hostnames()
{
    sed -e 's/host \".*\"/host \"HOST\"/g' $tmp.out > $tmp.out.1
    mv $tmp.out.1 $tmp.out
    sed -e 's/host \".*\"/host \"HOST\"/g' $tmp.err > $tmp.err.1
    mv $tmp.err.1 $tmp.err
}

_filter_metalabel()
{
    # sometimes a pmcd_label_change event occurs during recording (.meta)
    sed -e 's/found 35 records$/found 34 records/g' $tmp.err > $tmp.err.1
    mv $tmp.err.1 $tmp.err
}

_filter()
{
    _filter_hostnames
    _filter_metalabel
    echo >>$seq_full
    echo "stdout" >>$seq_full
    cat $tmp.out >>$seq_full
    echo "stderr" >>$seq_full
    cat $tmp.err >>$seq_full
    sed -e "s;$tmp;TMP;g" $tmp.out
    sed -e "s;$tmp;TMP;g" $tmp.err
}

# real QA test starts here
export LC_COLLATE=POSIX
echo "== new diagnostics ==" >> $seq.full
pmdumplog -dehIL tmparch/foo >> $seq.full
echo "== pre diagnostic0 ==" >> $seq.full
pmlogcheck -z -v tmparch/foo >> $seq.full 2>&1
echo "== pre diagnostic1 ==" >> $seq.full
$here/src/scanmeta -a tmparch/foo.meta >> $seq.full
echo "== end diagnostics ==" >> $seq.full

echo "=== directory traversal and file checks ===" | tee -a $seq_full
cp tmparch/foo.index $tmp
( echo; echo "TMP/foo.index alone:" ) | tee -a $seq_full
pmlogcheck -z -v $tmp/foo >$tmp.out 2>$tmp.err
_filter
cp tmparch/foo.0 $tmp
( echo; echo "TMP/foo.meta missing:" ) | tee -a $seq_full
pmlogcheck -z -v $tmp/foo >$tmp.out 2>$tmp.err
_filter
rm $tmp/foo.0
cp tmparch/foo.meta $tmp
( echo; echo "TMP/foo.0 missing:" ) | tee -a $seq_full
pmlogcheck -z -v $tmp/foo >$tmp.out 2>$tmp.err
_filter
rm $tmp/foo.index
cp tmparch/foo.0 $tmp
( echo; echo "TMP/foo.index missing:" ) | tee -a $seq_full
pmlogcheck -z -v $tmp/foo >$tmp.out 2>$tmp.err
_filter
cp tmparch/foo.index $tmp

touch $tmp/foo
touch $tmp/foo.
touch $tmp/foobar.0
touch $tmp/foo.999
touch $tmp/foo.1
chmod 0 $tmp/foo.1
touch $tmp/foo.bar
touch $tmp/foo.9x9
( echo; echo "TMP/bar:" ) | tee -a $seq_full
pmlogcheck -z -v $tmp/bar >$tmp.out 2>$tmp.err
_filter
( echo; echo "TMP/foo:" ) | tee -a $seq_full
pmlogcheck -z -v $tmp/foo >$tmp.out 2>$tmp.err
_filter
cd $tmp
( echo; echo "foo:" ) | tee -a $seq_full
pmlogcheck -z -v foo >$tmp.out 2>$tmp.err
_filter
chmod 644 foo.1
( echo; echo "foo.index:" ) | tee -a $seq_full
pmlogcheck -z -v foo.index >$tmp.out 2>$tmp.err
_filter
( echo; echo "archive-20150415.044829:" ) | tee -a $seq_full
ln foo.0 archive-20150415.044829.0
ln foo.meta archive-20150415.044829.meta
ln foo.index archive-20150415.044829.index
pmlogcheck -z -v archive-20150415.044829 >$tmp.out 2>$tmp.err
_filter

cd $here

echo | tee -a $seq_full
echo "=== pass 0 failures ===" | tee -a $seq_full
for arch in `ls badarchives/badlen-*.meta` badarchives/badti-1 `ls badarchives/badlabel-*.meta`
do
    # some of the bad archives may not have pass 0 style corruption,
    # so skip these ones for now
    #
    case $arch
    in
	badarchives/badlen-0.meta|badarchives/badlen-8.meta|badarchives/badlabel-0.meta)
		;;
	*)
 		( echo; echo "$arch:" ) | tee -a $seq_full
		pmlogcheck -z -v $arch >$tmp.out 2>$tmp.err
		_filter
		;;
    esac
done

echo | tee -a $seq_full
echo "=== pass 1 failures ===" | tee -a $seq_full
for arch in `ls badarchives/badti-*.index`
do
    # some of the bad archives may not have pass 1 style corruption,
    # so skip these ones for now
    #
    case $arch
    in
	badarchives/badti-1.index)
		;;
	*)
 		( echo; echo "$arch:" ) | tee -a $seq_full
		pmlogcheck -z -v $arch >$tmp.out 2>$tmp.err
		_filter
		;;
    esac
done

echo | tee -a $seq_full
echo "=== pass 3 failures ===" | tee -a $seq_full
for arch in `ls badarchives/badlog-*.0`
do
    # some of the bad archives may not have pass 3 style corruption,
    # so skip these ones for now
    #
    case $arch
    in
	*)
 		( echo; echo "$arch:" ) | tee -a $seq_full
		pmlogcheck -z -v $arch >$tmp.out 2>$tmp.err
		_filter
		;;
    esac
done

# success, all done
status=0

exit
