#!/bin/sh
# PCP QA Test No. 256
#
# Derived metrics and PMNS operations
#
# 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

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

# Derived metric expr dump from 0x8513a48...
# expr node 0x867eb68 type=PLUS left=0x867eb98 right=0x867ed28
_filter()
{
    cat $tmp.out >>$seq.full
    awk <$tmp.out >$tmp.sed '
BEGIN				{ n = 0 }
$1 == "expr" && $2 == "node" && $3 ~ /^0x/	{ print "s/" $3 "/<addr-" n ">/"; n++ }
				{ next }'
    echo "=== sed ===" >>$seq.full
    cat $tmp.sed >>$seq.full
    echo "=== end sed ===" >>$seq.full
    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 "s;$tmp;TMP;"
# -e 's/ val=[0-9][0-9]*/ val=<number>/g'
}

# expr node <addr-6> type=NAME left=(nil) right=(nil) save_last=1 [sample.pdu] master=0
# ...
# [0] inst=-1, val=49794
_filter2()
{
    sed <$tmp.out -e 's/val=/val /' \
    | awk >$tmp.sed2 '
BEGIN			{ n = 0 }
/expr.*sample\./	{ want=1; next }
want == 1 && $3 == "val"	{ if (seen[$4] != "y") {
				    print "s/ val=" $4 "$/ val=<value-" n ">/"
				    n++
				    seen[$4] = "y"
				  }
				}
$1 == "expr"		{ want = 0 }'
    echo "=== sed2 ===" >>$seq.full
    cat $tmp.sed2 >>$seq.full
    echo "=== end sed2 ===" >>$seq.full
    sed -f $tmp.sed2 \
    | sed \
	-e '/pmGetChildren(name="")/s/ [0-9][0-9]* regular/ N regular/' \
	-e '/pmGetChildren/s/returns [0-9][0-9]/returns N/'
}

# real QA test starts here

echo "HOST context ..." | tee -a $seq.full
cat <<End-of-File >$tmp.config
myname.one = sample.long.one
myname.suba.two = 2*sample.long.one
myname.suba.three = 3*sample.long.one
myname.suba.subb.four = 4*sample.long.one
myname.suba.subb.subc.subd.five = 5*sample.long.one
myname.suba.subb.subc.subd.six = 6*sample.long.one
End-of-File
echo | tee -a $seq.full
cat $tmp.config | tee -a $seq.full

for args in myname.suba.subb.subc.subd.six myname.suba.subb myname \
    myname.one.bad myname.suba.subb.subc.bad
do
    echo | tee -a $seq.full
    echo "=== -h localhost $args ===" | tee -a $seq.full
    pminfo -h localhost -c $tmp.config $args >$tmp.out 2>&1
    _filter
    echo "=== -h localhost -f $args ===" | tee -a $seq.full
    pminfo -h localhost -c $tmp.config -f $args >$tmp.out 2>&1
    _filter
done

echo | tee -a $seq.full
echo "ARCHIVE context ..." | tee -a $seq.full
cat <<End-of-File >$tmp.config
sample.one = sample.longlong.one
sample.suba.two = 2*sample.longlong.one
sample.suba.three = 3*sample.longlong.one
sample.suba.subb.four = 4*sample.longlong.one
sample.suba.subb.subc.subd.five = 5*sample.longlong.one
sample.suba.subb.subc.subd.six = 6*sample.longlong.one
End-of-File
echo | tee -a $seq.full
cat $tmp.config | tee -a $seq.full

for args in sample.suba.subb.subc.subd.six sample.suba.subb sample \
    sample.one.bad sample.suba.subb.subc.bad
do
    echo | tee -a $seq.full
    echo "=== -a src/reduce-1 $args ===" | tee -a $seq.full
    pminfo -a src/reduce-1 -c $tmp.config $args >$tmp.out 2>&1
    _filter
    echo "=== -a src/reduce-1 -f $args ===" | tee -a $seq.full
    pminfo -a src/reduce-1 -c $tmp.config -f $args >$tmp.out 2>&1
    _filter
done

echo | tee -a $seq.full
echo "LOCAL context ..." | tee -a $seq.full
cat <<End-of-File >$tmp.config
sampledso.one = sampledso.longlong.one
sampledso.suba.two = 2*sampledso.longlong.one
sampledso.suba.three = 3*sampledso.longlong.one
sampledso.suba.subb.four = 4*sampledso.longlong.one
sampledso.suba.subb.subc.subd.five = 5*sampledso.longlong.one
sampledso.suba.subb.subc.subd.six = 6*sampledso.longlong.one
End-of-File
echo | tee -a $seq.full
cat $tmp.config | tee -a $seq.full
export PMDA_LOCAL_SAMPLE=yes

for args in sampledso.suba.subb.subc.subd.six sampledso.suba.subb sampledso \
    sampledso.one.bad sampledso.suba.subb.subc.bad
do
    echo | tee -a $seq.full
    echo "=== -L $args ===" | tee -a $seq.full
    $sudo pminfo -L -c $tmp.config $args >$tmp.out 2>&1
    _filter
    echo "=== -L -f $args ===" | tee -a $seq.full
    $sudo pminfo -L -c $tmp.config -f $args >$tmp.out 2>&1
    _filter \
    | $PCP_AWK_PROG '
NF == 0	{ want = 0; next }
$1 == "sampledso.one"	{ print ""; want = 1 }
$1 ~ /^sampledso\.suba\./	{ print ""; want = 1 }
want == 1		{ print }'
    echo "=== -n .../root -L ===" | tee -a $seq.full
    $sudo pminfo -L -c $tmp.config -n $PCP_VAR_DIR/pmns/root $args >$tmp.out 2>&1
    _filter
    echo "=== -n .../root ===" | tee -a $seq.full
    export PCP_DERIVED_CONFIG=$tmp.config
    src/nameall -n $PCP_VAR_DIR/pmns/root $args >$tmp.out 2>&1
    _filter | sed -e '/Name space load:/s/[0-9.]* msec/N.NN msec/'
    unset PCP_DERIVED_CONFIG
done

echo | tee -a $seq.full
echo "Splice-n-dice into existing PMNS ..." | tee -a $seq.full
cat <<End-of-File >$tmp.config
derived = sample.long.one
derived_tree.ten = sample.long.ten
derived_tree.hundred = sample.long.hundred
derived_tree.sample.long.million = sample.long.million
sample.long.derived = sample.long.one
sample.long.foo.bar.derived = sample.long.one
End-of-File
echo | tee -a $seq.full
cat $tmp.config | tee -a $seq.full

for args in "" derived derived_tree sample
do
    echo | tee -a $seq.full
    echo "=== $args ===" | tee -a $seq.full
    pminfo -c $tmp.config $args 2>&1 | grep derived
done

echo | tee -a $seq.full
echo "Check diags ..." | tee -a $seq.full
cat <<End-of-File >$tmp.config
derived.tree.one = sample.long.one
derived.tree.ten = sample.long.ten
derived.tree.hundred = sample.long.hundred
derived.tree.a.million = sample.long.million
derived.tree.b.million = sample.long.million
derived.tree.b.cmillion = sample.long.million
sample.long.derived = sample.long.one
sample.long.foo.bar.derived = sample.long.one
End-of-File
cat $tmp.config
export PCP_DERIVED_CONFIG=$tmp.config
#debug# src/torture_pmns -Dall -h localhost derived.tree >$tmp.out 2>&1
src/torture_pmns -Dderive,appl1 -h localhost derived.tree >$tmp.out 2>&1
_filter | _filter2

# success, all done
exit
