#!/bin/sh
# PCP QA Test No. 365
# Errors in pmcd.conf [acccess] that should not prevent pmcd starting
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`

# get standard filters
. ./common.product
. ./common.check
. ./common.filter
. ./localconfig

rm -f $seq.out
if pmcd -x /dev/null -p x 2>&1 | grep '.-p requires a posi*tive numeric argument' >/dev/null
then
    if test $PCP_EOE_VER -ge 2704
    then
	ln $seq.out.3 $seq.out
    else
	ln $seq.out.2 $seq.out
    fi
else
    ln $seq.out.1 $seq.out
fi

status=1
done_clean=false

_cleanup()
{
    if $done_clean
    then
	:
    else
	[ -f $tmp.pmcd.conf ] && $sudo mv $tmp.pmcd.conf $PCP_PMCDCONF_PATH
	rm -f $tmp.*
	$sudo $PCP_RC_DIR/pcp restart | _filter_pcp_start
	_wait_for_pmcd
	_wait_for_pmlogger
	done_clean=true
    fi
    exit $status
}

trap "_cleanup" 0 1 2 3 15

rm -f $seq.full

localhost=`hostname | sed -e 's/\..*//'`
eval `./getpmcdhosts -L -n 2 2>$tmp.out | sed -e 's/^/other1=/' -e 's/ / other2=/'`
echo "other1=$other1" >$seq.full
echo "other2=$other2" >>$seq.full
echo "localhost=$localhost" >>$seq.full
if [ -z "$other1" ]
then
    cat $tmp.out
    _notrun "Cannot find first remote host running pmcd"
fi
if [ -z "$other2" ]
then
    cat $tmp.out
    _notrun "Cannot find second remote host running pmcd"
fi

echo "QA output created by $seq"

# real QA test starts here
home=$PCP_PMDAS_DIR

# copy the pmcd config file to restore state later.
cp $PCP_PMCDCONF_PATH $tmp.pmcd.conf

if [ $PCP_PLATFORM = linux ]
then
    cat <<End-of-File >$tmp.tmp
# from qa/$seq
#
linux	60	dso	linux_init	$PCP_PMDAS_DIR/linux/pmda_linux.so
pmcd	2	dso	pmcd_init	$PCP_PMDAS_DIR/pmcd/pmda_pmcd.so

End-of-File
elif [ $PCP_PLATFORM = irix ]
then
    cat <<End-of-File >$tmp.tmp
# from qa/$seq
#
irix	1	dso	irix_init	libirixpmda.so
pmcd	2	dso	pmcd_init	pmda_pmcd.so
proc	3	dso	proc_init	pmda_proc.so

End-of-File
elif [ $PCP_PLATFORM = darwin ]
then
    cat <<End-of-File >$tmp.tmp
# from qa/$seq
#
darwin	78	dso	darwin_init	$PCP_PMDAS_DIR/darwin/pmda_darwin.dylib
pmcd	2	dso	pmcd_init	$PCP_PMDAS_DIR/pmcd/pmda_pmcd.dylib

End-of-File
elif [ $PCP_PLATFORM = solaris ]
then
    cat <<End-of-File >$tmp.tmp
# from qa/$seq
#
solaris	75	dso	solaris_init	$PCP_PMDAS_DIR/solaris/pmda_solaris.so
pmcd	2	dso	pmcd_init	$PCP_PMDAS_DIR/pmcd/pmda_pmcd.so

End-of-File
else
    echo "Arrgh ... need pmcd.conf for $PCP_PLATFORM"
    exit 1
fi

cat <<End-of-File >>$tmp.tmp

[access]
allow not.a.real.host : all;
allow nohost.engr.sgi.com, $localhost : all;
allow $other1, not.a.real.host, localhost : fetch;
allow localhost, $other2, nohost.melbourne.sgi.com : store;

# from qa/$seq
End-of-File

$sudo cp $tmp.tmp $PCP_PMCDCONF_PATH
$sudo $PCP_RC_DIR/pcp restart | _filter_pcp_start
_wait_for_pmcd

_filter_pmcd_log <$PCP_PMCDLOG_PATH \
| sed \
    -e '/^linux/{
s/     [12] dso/1-or-2 dso/
s/lib=\/usr\//lib=\/usr-or-var\//
s/lib=\/var\//lib=\/usr-or-var\//
}' \
    -e '/gethostbyname(/s/ Unknown host/ No address associated with name/' \
    -e '/gethostbyname(/s/ Host name lookup failure/ No address associated with name/' \
    -e '/gethostbyname(/s/ Resolver Error 0 (no error)/ No address associated with name/' \
    -e "s/$localhost\$/LOCALHOST/g" \
    -e "s/$other1\$/OTHERHOST1/g" \
    -e "s/$other2\$/OTHERHOST2/g" \
    -e "s/[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]/<hexnum>/"

status=0
exit
