#!/bin/sh
#
# Check that PDU-related QA apps provide full coverage of all
# current PDU types
#
# Copyright (c) 2022 Ken McDonell.  All Rights Reserved.
#

INCDIR=../src/include/pcp

if [ ! -d $INCDIR ]
then
    echo "Botch: cannot find dir $INCDIR"
    exit 1
fi

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

# Notes
# - PDU_START and PDU_FINISH define the range of values are not real
#   PDU types.
# - PDU_CONTROL_REQ is no longer used in the PCP code base (there are
#   some remnants, e.g. __pmPDUTypeStr_r(), libpcp.h and "do nothing"
#   __pmdaMainPDU(), but no code actually uses this PDU type any more
#
grep -r 'define PDU_' $INCDIR \
| grep '0x[78]' \
| sed \
    -e '/[ 	]PDU_START[ 	]/d' \
    -e '/[ 	]PDU_FINISH[ 	]/d' \
    -e '/[ 	]PDU_CONTROL_REQ[ 	]/d' \
| awk '{ print $2 }' \
| sort \
| uniq \
| while read pdu
do
    case $pdu
    in
	PDU_ATTR)
		pat="(PDU_ATTR|PDU_AUTH)"
		;;
	*)
		pat="$pdu"
		;;
    esac
    for src in pdu-server.c pducheck.c pducrash.c
    do
	if grep -E "[ 	]$pat[;:) ]" src/$src >/dev/null
	then
	    :
	else
	    echo "$pdu" >>$tmp.$src
	fi
    done
done

for src in pdu-server.c pducheck.c pducrash.c
do
    if [ -s $tmp.$src ]
    then
	echo "=== missing from $src ==="
	cat $tmp.$src
    fi
done
