#!/bin/sh
# PCP QA Test No. 1384
# simplest pmlogrewrite test for V3 (and V2) archives ... no
# delta indoms, so output archive should be the same as the
# input archive
# also feature bits rewriting tests
#
# non-valgrind variant, see qa/1386 for the valgrind variant
#
# Copyright (c) 2022 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

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

do_valgrind=false
if [ "$1" = "--valgrind" ]
then
    _check_valgrind
    do_valgrind=true
elif which valgrind >/dev/null 2>&1
then
    : note we want to run both variants for this test, as the
    : valgrind variant filters away the functional checks and
    : only reports memory issues
fi

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
    # end
}

# real QA test starts here

for version in 2 3
do
    echo
    echo "--- Version $version archive ---"

    rm -f $tmp.0 $tmp.meta $tmp.index

    if $do_valgrind
    then
	_run_valgrind pmlogextract tmparch/sampledso-no-delta_v$version $tmp
    else
	pmlogrewrite tmparch/sampledso-no-delta_v$version $tmp 2>&1
    fi \
    | _filter

    echo "=== scanmeta for input ===" >>$seq_full
    src/scanmeta -a tmparch/sampledso-no-delta_v$version.meta 2>&1 \
    | tee -a $seq_full \
    | sed -e 's/^\[[0-9][0-9]*/[NN/' \
    | LC_COLLATE=POSIX sort >$tmp.input

    echo >>$seq_full
    echo "=== scanmeta for output ===" >>$seq_full
    src/scanmeta -a $tmp.meta 2>&1 \
    | tee -a $seq_full \
    | sed -e 's/^\[[0-9][0-9]*/[NN/' \
    | LC_COLLATE=POSIX sort >$tmp.output

    echo "=== scanmeta diffs ==="
    diff $tmp.input $tmp.output

    echo "=== dumplog for input ===" >>$seq_full
    pmdumplog -aI tmparch/sampledso-no-delta_v$version 2>&1 \
    | tee -a $seq_full \
    | sed -e '/^PID for pmlogger:/s/[0-9][0-9]*/<PID>/' >$tmp.input

    echo >>$seq_full
    echo "=== dumplog for output ===" >>$seq_full
    pmdumplog -aI $tmp 2>&1 \
    | tee -a $seq_full \
    | sed -e '/^PID for pmlogger:/s/[0-9][0-9]*/<PID>/' >$tmp.output

    echo "=== pmdumplog diffs ==="
    diff $tmp.input $tmp.output

done

$do_valgrind && exit

# exercise some V3 features and error checks
#
cat <<'End-of-File'  >$tmp.0
global {
    timezone -> "AEST-10"
}
End-of-File

cat <<'End-of-File'  >$tmp.1
global {
    zoneinfo -> ":Africa/Timbuktu"
}
End-of-File

cat <<'End-of-File' >$tmp.2
global {
    features -> 2147483655
}
End-of-File

pmdumplog -z -L archives/bozo.client_v2 >$tmp.label.2

echo
echo "Expect timezone to change ..."
pmlogrewrite -V 3 -c $tmp.0 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
pmdumplog -z -L $tmp.eek >$tmp.label.3
diff $tmp.label.[23]
rm -f $tmp.eek.*

echo
echo "Expect zoneinfo to be added ..."
pmlogrewrite -V 3 -c $tmp.1 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
pmdumplog -z -L $tmp.eek >$tmp.label.3
diff $tmp.label.[23]
rm -f $tmp.eek.*

echo
echo "Expect feature bits 0,1,2 and 31 to be added ..."
pmlogrewrite -V 3 -c $tmp.2 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
pmdumplog -z -L $tmp.eek >$tmp.label.3
diff $tmp.label.[23]

echo
echo "Expect feature bit 0 and 1 to be removed ..."
cat <<'End-of-File' >$tmp.2
global {
    features -> BITS(31,2)
}
End-of-File
pmlogrewrite -V 3 -c $tmp.2 $tmp.eek $tmp.urk 2>&1 | _filter
pmdumplog -z -L $tmp.urk >$tmp.label.4
diff $tmp.label.[34]
rm -f $tmp.eek.* $tmp.urk.*

echo
echo "Expect errors ..."
pmlogrewrite -c $tmp.1 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
rm -f $tmp.eek.*

echo
pmlogrewrite -c $tmp.2 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
rm -f $tmp.eek.*

echo
cat <<'End-of-File' >$tmp.2
global {
    # missing )
    features -> bits(31.32
}
End-of-File
pmlogrewrite -c $tmp.2 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
rm -f $tmp.eek.*

echo
cat <<'End-of-File' >$tmp.2
global {
    # space not comma
    features -> bits(31 32)
}
End-of-File
pmlogrewrite -c $tmp.2 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
rm -f $tmp.eek.*

echo
cat <<'End-of-File' >$tmp.2
global {
    # missing list...)
    features -> bits(
}
End-of-File
pmlogrewrite -c $tmp.2 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
rm -f $tmp.eek.*

echo
cat <<'End-of-File' >$tmp.2
global {
    # missing (list...)
    features -> bits
}
End-of-File
pmlogrewrite -c $tmp.2 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
rm -f $tmp.eek.*

echo
cat <<'End-of-File' >$tmp.2
global {
    features -> bits(33)
}
End-of-File
pmlogrewrite -c $tmp.2 archives/bozo.client_v2 $tmp.eek 2>&1 | _filter
rm -f $tmp.eek.*

# success, all done
exit
