#!/bin/sh
# PCP QA Test No. 416
# news pmda ... exercise perl PMDA bits-n-bobs
#
# Copyright (c) 2010 Ken McDonell.  All Rights Reserved.
#

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

seq=`basename $0`

perl -e "use PCP::PMDA" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "perl PCP::PMDA module not installed"

echo "QA output created by $seq"

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

_filter()
{
    sed \
	-e "s;$tmp;TMP;g" \
	-e "s;/[a-zA-Z0-9_/.\-]*/perl;PERL;" \
	-e "s;$PCP_PMDAS_DIR;PCP_PMDAS_DIR;" \
	-e '/^Log for pmdanews/s/ on .*/ on .../' \
	-e '/^Log finished/s/ed .*/ed .../' \
	-e '/^pmResult dump/{
s/ 0x[0-9a-f][0-9a-f]* / ADDR /
s/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\.[0-9][0-9][0-9]/TIMESTAMP/
}'
}

cat >$tmp.pmns <<End-of-File
/* need local pmns in case the news PMDA is not installed */
root {
    news
}

news {
	articles
	readers
}

news.articles {
	total		28:0:201
	count		28:0:301
	last		28:0:302
}

news.readers {
	xrn		28:0:113
	rn		28:0:111
	nnrpd		28:0:101
	trn		28:0:112
	vn		28:0:114
}
End-of-File

home=$PCP_PMDAS_DIR
if [ ! -d $home/news ]
then
    echo "Where is $home/news?"
    exit 1
fi
cd $home/news

# real QA test starts here

cat <<End-of-File | $sudo dbpmda -ie -n $tmp.pmns >$tmp.out 2>&1
open pipe `which perl` `pwd`/pmdanews.pl
traverse news
children news.articles
children news.articles.count
pmid news.articles.total
fetch news.articles.total
desc news.articles.count
instance 28.0
fetch news.articles.count
profile 28.0 none
profile 28.0 add 2
profile 28.0 add 4
getdesc on
fetch news.articles.count
close
quit
End-of-File

cat $tmp.out >>$here/$seq.full
_filter <$tmp.out

# ensure news pmda is all done ...
#
sleep 3

echo
echo "=== news.log ==="
_filter <news.log

# success, all done
status=0
exit
