#!/bin/sh
# PCP QA Test No. 591
# Test libpcp multi-archive support using pminfo
#
# Copyright (c) 2017 Red Hat.  All Rights Reserved.
#

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

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

_get_libpcp_config
$multi_archive_contexts || _notrun "Multi-archive support is not available"

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

_filter()
{
    tmpx=`echo $tmp`
    sed -e 's/^Archive:.* Host:/ARCHIVE: Host:/' \
	-e "s|$tmpx|TMP|g"
}

# real QA test starts here
TOOL=pminfo
mkdir -p $tmp.archives

for DIR in archives/multi-xz archives/multi-xz-1k
do
    # Check that running pminfo on a set of archives performs identically
    # to running it on the archive created by combining those same archives
    # using pmlogextract.

    # First, run pmlogextract against the archives.
    IN="$DIR/20150508.11.44 $DIR/20150508.11.46 $DIR/20150508.11.50 $DIR/20150508.11.57"
    OUT=$tmp.44+46+50+57.pmlogextract
    rm -f $OUT.*
    echo "Running pmlogextract $IN $OUT" | _filter | tee -a $seq_full
    pmlogextract $IN $OUT 2>&1 >> $seq_full
    [ $? -eq 0 ] || echo "Error running pmlogextract $IN $OUT -- see $seq.full"

    # Now run pminfo against the extracted archive.
    IN=$OUT
    OUTBASE=$OUT.$TOOL.txt
    echo "Running $TOOL -a $IN" | _filter
    $TOOL -a $IN 2>&1 | _filter | tee $OUTBASE

    # Now, run pminfo against the set of archives.
    IN=$DIR/20150508.11.44,$DIR/20150508.11.46,$DIR/20150508.11.50,$DIR/20150508.11.57
    OUT=$tmp.44+46+50+57.$TOOL.txt
    echo "Running $TOOL -a $IN" | _filter
    $TOOL -a $IN 2>&1 | _filter | tee $OUT

    # Make sure the output is the same
    echo "Running diff $OUTBASE $OUT" | _filter | tee -a $seq_full
    diff $OUTBASE $OUT 2>&1 >> $seq_full
    [ $? -eq 0 ] || echo "$OUT differs from $OUTBASE  -- see $seq.full"

    # Now do it again, specifying the set of archives using the directory name
    IN=$DIR
    OUT=$tmp.$DIR.$TOOL.txt
    echo "Running $TOOL -a $IN" | _filter
    $TOOL -a $IN 2>&1 | _filter | tee $OUT

    # Make sure the output is the same
    echo "Running diff $OUTBASE $OUT" | _filter | tee -a $seq_full
    diff $OUTBASE $OUT 2>&1 >> $seq_full
    [ $? -eq 0 ] || echo "$OUT differs from $OUTBASE  -- see $seq.full"
done

# success, all done
status=0
exit
