#! /bin/sh
# PCP QA Test No. 225
# Basic parsing for derived metric definitions ... no binding, so no
# semantic checks
#
# Copyright (c) 2009 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

if grep -q 'pmRegisterDerived' $PCP_DIR/usr/include/pcp/pmapi.h
then
    :
else
    echo "No derived metric support" >$seq.notrun
    echo "$seq: [not run] `cat $seq.notrun`"
    exit 0
fi

unset PCP_DERIVED_CONFIG

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

_filter()
{
    cat $tmp.out >>$seq.full
    awk <$tmp.out >$tmp.sed '
# expr node 0x9edc340 type=PLUS left=0x9edc2f8 right=0x9edc370
$1 == "expr" && $2 == "node" && $3 ~ /^0x/	{ print "s/" $3 "/<addr-" n ">/"; n++ }
				{ next }'
    echo "=== sed ===" >>$seq.full
    cat $tmp.sed >>$seq.full
    sed -f $tmp.sed <$tmp.out \
    | sed \
	-e 's/=0x0 /=(nil) /g' \
	-e 's/ref=0x[0-9a-f]*/ref=<addr>/' \
	-e "s;$tmp;TMP;"
}

echo "root { local_pmns_no_pmcd_connection 1:2:3 }" >$tmp.pmns

# real QA test starts here
echo "No syntax errors here ..." | tee -a $seq.full
cat <<End-of-File >$tmp.config
myname.a = disk.dev.read
myname.b = disk.dev.read + disk.dev.write
myname.c = disk.dev.read + disk.dev.write + sample.long.writeme
myname.d = (disk.dev.read)
myname.e = 2 * disk.dev.write + sample.long.writeme
myname.f = 2 + disk.dev.write * sample.long.writeme
myname.g = ( 2 + disk.dev.write ) * sample.long.writeme
myname.h = 2 + ( disk.dev.write * sample.long.writeme )
myname.i = (disk.dev.write+disk.dev.read)*(sample.long.one+sample.long.ten)
myname.j = (disk.dev.write*disk.dev.read)+(sample.long.one*sample.long.ten)
myname.k = sample.long.one + sample.long.ten + sample.long.hundred + sample.long.million + sample.longlong.one + sample.longlong.ten + sample.longlong.hundred + sample.longlong.million
myname.l = sample.long.one + sample.long.ten + sample.long.hundred + sample.long.million - sample.longlong.one - sample.longlong.ten - sample.longlong.hundred - sample.longlong.million
myname.m = sample.long.one * sample.longlong.one + sample.long.ten * sample.longlong.ten
myname.n = sample.long.one + sample.longlong.one * sample.long.ten + sample.longlong.ten
myname.ABC.k42.x_1_3	=		disk.dev.write*disk.dev.read	+	sample.long.one*sample.long.ten	
myname.o= sample.long.one * sample.longlong.one + sample.long.ten * sample.longlong.ten + sample.long.hundred * sample.longlong.hundred + sample.long.million * sample.longlong.million
myname.p =  pmcd.pdu_in.total / ( pmcd.pdu_in.total + pmcd.pdu_out.total )
myname.biggest.const = 4294967295
End-of-File

pminfo -Dderive,appl0 -c $tmp.config -n $tmp.pmns >$tmp.out 2>&1
_filter

echo | tee -a $seq.full
echo "Syntax errors ..." | tee -a $seq.full
cat <<End-of-File >$tmp.config
myname.a = sample.long.one +
myname.b = sample.long.one sample.long.ten
myname.c=sample.long.one**sample.long.ten
=
myname.d
myname.e =
myname.e sample.long.one + sample.long.ten
bad-name-xyz = sample.long.one
123_really_bad_name = sample.long.one
tricky._bad_name = sample.long.one
myname.f =  pmcd.pdu_in.total / ( pmcd.pdu_in.total + pmcd.pdu_out.total
myname.g =  pmcd.pdu_in.total / ( pmcd.pdu_in.total + pmcd.pdu_out.total ) )
myname.a = 1
myname.a = dup.name.a
myname.h = 4294967296
# extra tests here from gcov analysis
myname.gcov.a = foo = bar
# very long source lines to test buffer handling in parser
myname.gcov.b = sample.longlong.million + sample.longlong.million + sample.longlong.million + sample.longlong.million + sample.longlong.million + sample.longlong.million + sample.longlong.million + sample.longlong.million + sample.longlong.million + sample.longlong.million + sample.longlong.million + "bogus"
myname.gcov.c = sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million.sample.longlong.million + "bogus"
   myname.gcov.d = 3 * space
	myname.gcov.e = tab
myname.gcov.f = ( 1 + foo + ( ?
myname.gcov.g = /

End-of-File

pminfo -Dderive,appl0 -c $tmp.config -n $tmp.pmns >$tmp.out 2>&1
_filter

export PCP_DERIVED_CONFIG=/no/such/file
pminfo -Dderive -d no.such.metric

# success, all done
exit
