#!/bin/sh
#
# Copyright (c) 1995,2003 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 

# source the PCP configuration environment variables
. $PCP_DIR/etc/pcp.env

if [ -n "$PCP_TMPFILE_DIR" -a -d "$PCP_TMPFILE_DIR" ]
then
    tmp=`mktemp -d "$PCP_TMPFILE_DIR/pmns_stdpmid.XXXXXXXXX"` || exit 1
else
    # assume run during a build and /tmp is a safe bet
    #
    tmp=`mktemp -d "/tmp/pmms_stdpmid.XXXXXXXXX"` || exit 1
fi

status=1
trap "rm -rf $tmp; exit \$status" 0 1 2 3 15

prog=`basename $0`
OLD=stdpmid
NEW=$tmp/new
SOURCE=""
for file in `echo stdpmid.*`
do
    case $file
    in
	stdpmid.'*'|stdpmid.O|stdpmid.O.*|stdpmid.N|stdpmid.N.*|stdpmid.rpmorig|stdpmid.*.rpmorig)
		;;
	*)
		SOURCE="$SOURCE $file"
		;;
    esac
done

[ -z "$SOURCE" ] && exit	# nothing to do - the norm nowadays

# post-processing with sed ...
#	- removes comments
#	- maps white space to a single space
#	- performs domain re-numbering that occured for LAB and ASH
#	  between PCP 2.0 and PCP 2.1 to avoid duplicates in the interim
#
# Note on sort.  Used to be "sort -n +1 -2", but changed to "sort -n -k2,3"
# to avoid problems with more recent Linux coreutils versions.
#
for file in $SOURCE
do
    sed <$file \
	-e '/^#/d' \
	-e 's/[ 	][ 	]*/ /' \
	-e '/^LAB /s/254/246/' \
	-e '/^ASH /s/7/11/'
done \
| sort -n -k2,3 \
| uniq >$tmp/tmp

if [ -s "$tmp/tmp" ]
then
    error=false
else
    echo "$prog: Error: failed to create temporary file"
    exit
fi

# scan for duplicate domain name, but different domain number
#
$PCP_AWK_PROG '{ print $1 }' <$tmp/tmp \
| sort \
| uniq -c \
| while read cnt domain
do
    [ $cnt -eq 1 ] && continue
    echo "$prog: Error: duplicate for domain name \"$domain\" ..."
    grep "^$domain[ 	]" $SOURCE | sed -e 's/^/    /'
    error=true
done

# scan for duplicate domain number, but different domain name
#
$PCP_AWK_PROG '{ print $2 }' <$tmp/tmp \
| sort \
| uniq -c \
| while read cnt number
do
    [ $cnt -eq 1 ] && continue
    echo "$prog: Error: duplicate for domain number \"$number\" ..."
    grep "[ 	]$number\$" $SOURCE | sed -e 's/^/    /'
    error=true
done

$error && exit

# preamble
#
cat <<'End-of-File' >$NEW
/*
 * NOTE:
 *	Do not edit this file (it is re-created by Make.stdpmid).
 *	To make changes, edit one of the stdpmid.* files, most probably
 *	stdpmid.local, and as root
 *		# make stdpmid
 *
 * The following domain number assignments are assumed to apply
 *
 * Domain Number Range		Use
 *	  0			reserved -- DO NOT USE
 *	  1-384			production PMDAs from PCP packages
 *	385-510			end-user PMDAs (allocate from high to low)
 *	511			reserved for dynamic PMNS entries -- DO NOT USE
 *
 * A Performance Metrics Identifier (PMID) is internally encoded as
 *	 1 bits	- flag reserved for internal use
 *	 9 bits	- the Performance Metric Domain Agent (PMDA) domain number
 *		  from the list below
 *	12 bits	- cluster within domain
 *	10 bits	- serial within cluster
 */

#ifndef __STDPMID
#define __STDPMID

End-of-File

cat $tmp/tmp \
| while read domain number
do
    echo "#define $domain	$number" >>$NEW
done

echo '
#endif' >>$NEW

# only update if it has changed
#
if [ -f $OLD ]
then
    if cmp -s $OLD $NEW >/dev/null 2>&1
    then
	rm -f $NEW
    fi
fi

if [ -f $NEW ]
then
    cp $NEW $OLD
    chmod 444 $NEW
fi

status=0
exit
