#!/bin/sh
#
# find metrics in the QA archives
#
# command line args are alternate patterns matched in the output
# from pminfo -d ... so metric names and metric descriptors
#

tmp=/var/tmp/$$
sts=0
trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15

_usage()
{
    echo >&2 "Usage: `basename $0` [options] pat ..."
    echo >&2 "   -a         search archives [default]"
    echo >&2 "   -h         search localhost live metrics"
}

arch=true
while getopts "ah?" c
do
    case $c
    in
	a)
	    arch=true
	    ;;
	h)
	    arch=false
	    ;;
	?)
	    _usage
	    sts=1
	    exit
	    ;;
    esac
done
shift `expr $OPTIND - 1`

if [ $# -eq 0 ]
then
    _usage
    sts=1
    exit
fi

pat=""
for arg
do
    if [ -z "$pat" ]
    then
	pat="($arg)"
    else
	pat="$pat|($arg)"
    fi
done

if $arch
then
    srclist="`echo src/*.0 archives/*.0`"
else
    srclist='ignoreme'
fi

for src in $srclist
do
    if $arch
    then
	pminfo -d -a $src 2>&1
    else
	pminfo -d 2>&1
    fi \
    | sed -e '/^[a-z]/{
s/$/|/
N
s/$/|/
N
s/|./|/g
s/  */ /g
s/| Data Type: /|/
s/ InDom: /|/
s/ Semantics: /|/
s/ Units: /|/
}' \
    | egrep "$pat" >$tmp.out

    if [ -s $tmp.out ]
    then
	if $arch
	then
	    pmdumplog -l $src >$tmp.info
	    vers=`sed -n <$tmp.info -e '/Log Format Version/{
s/.*Version //
s/)//
p
}'`
	    host=`sed -n <$tmp.info -e '/ from host /{
s/.* from host //
p
}'`
	    echo "=== `basename $src .0` (V.$vers) host: $host ==="
	    grep commencing $tmp.info
	    grep ending $tmp.info
	    sed <$tmp.out -e "s/^/    /"
	else
	    echo "=== `hostname` ==="
	    sed <$tmp.out -e "s/^/    /"
	fi
    fi
done
