#!/bin/sh
#
# Check group file and qa scripts for a specific pcp command ($1)
#
# $1 is assumed to be _both_ the name of a command that appears in
# the QA scripts (or part of a command, e.g. purify in _setup_purify)
# and the name of a group in the group file
#

tmp=/var/tmp/check-group-$$
trap "rm -f $tmp.*; exit 0" 0 1 2 3 15
debug=true
debug=false

if [ $# -ne 1 ]
then
    echo "Usage: $0 pcp-app"
    exit 1
fi

touch $tmp.tmp
for seq in [0-9][0-9][0-9] [0-9][0-9][0-9][0-9]
do
    if grep -q "^$seq[: 	]" group
    then
	:
    else
	echo "$seq: Error: file exists but missing from group"
    fi
    # special control lines
    # check-group-include: group ..
    # check-group-exclude: group ..
    grep '# check-group-' $seq >$tmp.chk
    if [ -s $tmp.chk ]
    then
	if egrep "check-group-include:.* $1( |\$)" <$tmp.chk >/dev/null
	then
	    $debug && echo "$seq: explicit include"
	    echo $seq >>$tmp.tmp
	    continue
	fi
	if egrep "check-group-exclude:.* $1( |\$)" <$tmp.chk >/dev/null
	then
	    $debug && echo "$seq: explicit exclude"
	    continue
	fi
    fi

    # need to handle some alias cases ...
    #
    case "$1"
    in

	collectl)	# executable might be collectl2pcp or pmcollectl or
			# pcp collectl
	    ( grep -v '^#' $seq \
	      | egrep "(^([ 	]*collectl2pcp|pmcollectl|(pcp collectl))([ '\"]|$))|([ /'\"\`_=](collectl2pcp|pmcollectl|(pcp collectl))([ '\"\`_]|$))" >$tmp.debug \
	    ) && echo $seq >>$tmp.tmp
	    $debug && [ -s $tmp.debug ] && echo $seq: && cat $tmp.debug
	    report_group="pmcollectl et al"
	    ;;

	folio)		# executable might be pmafm or mkaf
	    ( grep -v '^#' $seq \
	      | egrep -q "(^[ 	]*(pmafm|mkaf)([ '\"]|$))|([ /'\"\`_=](pmafm|mkaf)([ '\"\`_]|$))" \
	    ) && echo $seq >>$tmp.tmp
	    report_group="pmafm or mkaf"
	    ;;

	logutil)	# pmlogger_ scripts
	    ( grep -v '^#' $seq \
	      | egrep -q "(^[ 	]*(pmlogger_check|pmlogger_daily|pmlogger_daily_report|pmlogger_merge|pmlogger_rewrite)([ '\"]|$))|([ /'\"\`_=](pmlogger_check|pmlogger_daily|pmlogger_daily_report|pmlogger_merge|pmlogger_rewrite)([ '\"\`_]|$))" \
	    ) && echo $seq >>$tmp.tmp
	    report_group="pmlogger_* scripts"
	    ;;

	pmclient)	# executable might be pmlient_fg
	    ( grep -v '^#' $seq \
	      | egrep -q "(^[ 	]*(pmclient|pmclient_fg)([ '\"]|$))|([ /'\"\`_=](pmclient|pmclient_fg)([ '\"\`_]|$))" \
	    ) && echo $seq >>$tmp.tmp
	    report_group="pmclient et al"
	    ;;

	pmdumplog)	# executable might be hidden in _exercise_compression
			# wrapper from common.compress
	    ( grep -v '^#' $seq \
	      | egrep -q "(^[ 	]*(pmdumplog|_exercise_compression)([ '\"]|$))|([ /'\"\`_=](pmdumplog|_exercise_compression)([ '\"\`_]|$))" \
	    ) && echo $seq >>$tmp.tmp
	    report_group=pmdumplog
	    ;;

	pmlogrewrite)	# executable might be hidden in mk.logfarm
	    ( grep -v '^#' $seq \
	      | egrep -q "(^[ 	]*(pmlogrewrite|mk.logfarm)([ '\"]|$))|([ /'\"\`_=](pmlogrewrite|mk.logfarm)([ '\"\`_]|$))" \
	    ) && echo $seq >>$tmp.tmp
	    report_group=pmlogrewrite
	    ;;

	valgrind)	# executable maybe hidden in wrappers, so look for
			# cmd and guard and wrapper
	    ( grep -v '^#' $seq \
	      | egrep -q "(^[ 	]*(valgrind|_check_valgrind)([ '\"]|$))|([ /'\"\`_=](valgrind|_run_valgrind)([ '\"\`_]|$))" \
	    ) && echo $seq >>$tmp.tmp
	    report_group="valgrind"
	    ;;

	*)
	    ( grep -v '^#' $seq \
	      | egrep "(^[ 	]*$1([ '\"]|$))|([ '\"\`]$1([ '\"\`]|$))" >$tmp.debug \
	    ) && echo $seq >>$tmp.tmp
	    $debug && [ -s $tmp.debug ] && echo $seq: && cat $tmp.debug
	    report_group="$1"
	    ;;
    esac
done

sed <group -e '/^$/d' -e '/^[^0-9]/d' -e 's/ .*//' -e 's/:/ /' \
| while read seq state
do
    [ -f "$seq" ] && continue
    [ "$state" = reserved ] && continue
    echo "$seq: Error: in group but file not found"
done

sort -o $tmp.scripts $tmp.tmp

check -r -n -g "$1" | sort \
| while read f
do
    [ -f "$f" ] && echo "$f"
done >$tmp.group

comm -23 $tmp.scripts $tmp.group >$tmp.tmp
if [ -s $tmp.tmp ]
then
    echo "$report_group in QA scripts and NOT in group ..."
    sort -n $tmp.tmp | sed -e 's/^/    /'
    echo
fi

comm -13 $tmp.scripts $tmp.group >$tmp.tmp
if [ -s $tmp.tmp ]
then
    echo "$report_group in group and NOT in QA scripts ..."
    sort -n $tmp.tmp | sed -e 's/^/    /'
    echo
fi

comm -12 $tmp.scripts $tmp.group >$tmp.tmp
echo "$report_group in group and QA scripts `wc -l <$tmp.tmp | sed -e 's/ //g'` times"
