#!/bin/sh
# PCP QA Test No. 1103
# scalar() function for derived metrics
#
# Copyright (c) 2018 Ken McDonell.  All Rights Reserved.
#

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

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

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

_filter()
{
    $PCP_AWK_PROG <$tmp.out >$tmp.sed '
BEGIN				{ n = 0 }
$1 == "expr" && $2 == "node" && $3 ~ /^0x/	{ print "s/" $3 "/<addr-" n ">/"; n++ }
				{ next }'
    sed -f $tmp.sed <$tmp.out \
    | sed \
	-e '/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/s/[^ ]*.*numpmid/TIMESTAMP ... numpmid/' \
	-e 's/=0x0 /=(nil) /g' \
	-e '/InDom:/s/\(InDom: [^ ]* 0x\)[0-9a-f][0-9a-f]*/\1..../' \
	-e "s;$tmp;TMP;" \
    # end
}

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

# real QA test starts here
echo "=== simple cases ==="
cat <<'End-of-File' >$tmp.config
qa.a = scalar(sample.bin[bin-300])
qa.b = (scalar(sample.bin[bin-100]) + scalar(sample.bin[bin-200]) + scalar(sample.bin[bin-300])) / 3
qa.c = (scalar(sample.bin[bin-100]) + scalar(matchinst(/^bin-200$/,sample.bin)) + scalar(sample.bin[bin-300])) / 3
End-of-File

export PCP_DERIVED_CONFIG=$tmp.config
pminfo -df -Dderive,appl1,appl2 qa >$tmp.out 2>&1
cat $tmp.out >>$here/$seq.full
_filter

echo
echo "=== some error cases ==="
cat <<'End-of-File' >$tmp.config
qa.p = 123 + scalar(sample.bin
qa.q = 123 + scalar(^name)
End-of-File

export PCP_DERIVED_CONFIG=$tmp.config
pminfo -df -Dderive,appl1,appl2 qa >$tmp.out 2>&1
cat $tmp.out >>$here/$seq.full
_filter

# success, all done
status=0
exit
