#!/bin/sh
#
# pmdumplog -i -> pmlogsize sort of numbers
#

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

# reduce output to strings of same length as the external representation
# in the .meta file
#
pmdumplog -i $1 \
| sed >$tmp.tmp \
    -e '/^[0-9]/s/.*/InDoTIMESTAMNINS/' \
    -e '/ or /{
s/^  *//
s/^\([0-9][0-9][0-9][0-9][0-9][0-9][0-9]\)[0-9][0-9][0-9]* /\1X/
s/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] /&/
s/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9] /&R/
s/^[0-9][0-9][0-9][0-9][0-9][0-9] /&TR/
s/^[0-9][0-9][0-9][0-9][0-9] /&PTR/
s/^[0-9][0-9][0-9][0-9] /&SPTR/
s/^[0-9][0-9][0-9] /&TSPTR/
s/^[0-9][0-9] /&STSPTR/
s/^[0-9] /&NSTSPTR/
s/ //
s/or "//
s/"$//
}'

# lines like
# InDom: 29.1
# mark the start of each block of indom info
#
grep '^InDom:' <$tmp.tmp \
| while read tag indom
do
    awk <$tmp.tmp >$tmp.indom '
$1 == "InDom:" && $2 == "'$indom'"	{ want=1; next }
want == 1 && $1 == "InDom:" 		{ want=0 }
want == 1				{ print }'

    # lines look like ...
    # InDoTIMESTAMNINS
    # 0NSTSPTRred#
    # 1NSTSPTRgreen#
    # 2NSTSPTRblue#
    nrec=`grep "^InDoTIME" <$tmp.indom | wc -l | sed -e 's/ //g'`
    ninst=`grep "^[0-9]" <$tmp.indom | wc -l | sed -e 's/ //g'`
    bytes=`grep "^[0-9]" <$tmp.indom | wc -c | sed -e 's/ //g'`
    nuniqinst=`grep "^[0-9]" <$tmp.indom | sort | uniq | wc -l | sed -e 's/ //g'`
    if [ $ninst -ne $nuniqinst ]
    then
	uniqbytes=`grep "^[0-9]" <$tmp.indom | sort | uniq | wc -c | sed -e 's/ //g'`
	ndupinst=`expr $ninst - $nuniqinst`
	dupbytes=`expr $bytes - $uniqbytes`
	bytes=`expr $bytes + $nrec \* 20`
	echo "$indom: $bytes bytes [$nrec records, $ninst instances ($dupbytes bytes for $ndupinst dups]"
    else
	bytes=`expr $bytes + $nrec \* 20`
	echo "$indom: $bytes bytes [$nrec records, $ninst instances]"
    fi

done
