#! /bin/sh
# PCP QA Test No. 110
# check out simple PMDA
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

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

signal=$PCP_BINADM_DIR/pmsignal
status=1

ROOT_LOG_PATH=`echo $PCP_PMCDLOG_PATH | sed -e 's/pmcd.log/root.log/'`
PMDA_LOG_PATH=`echo $PCP_PMCDLOG_PATH | sed -e 's/pmcd.log/simple.log/'`

_cleanup()
{
	[ -f $home/$iam/$iam.conf.$seq ] && _restore_config $home/$iam/$iam.conf
	if diff $PCP_PMCDCONF_PATH.$seq $PCP_PMCDCONF_PATH > /dev/null 2>&1
	then
	    _restore_config $PCP_PMCDCONF_PATH
	else

# do a default install which ensures the pmns and any views are installed

	    $sudo ./Install < /dev/null > /dev/null 2>&1

# PMDA may have been installed differently to default. As everything is
# installed we can use the old pmcd.conf file to restore state.

	    if diff $PCP_PMCDCONF_PATH.$seq $PCP_PMCDCONF_PATH > /dev/null 2>&1
	    then
	    	_restore_config $PCP_PMCDCONF_PATH
	    else
	    	_restore_config $PCP_PMCDCONF_PATH
		$sudo $signal -a -s HUP pmcd
	    fi
	fi

	_service pmlogger start >>$seq_full
	_wait_for_pmlogger

	$sudo rm -f $tmp.*
	exit $status
}

trap "_cleanup" 0 1 2 3 15

_failed()
{
    echo
    echo "... failed! ... here is the Install log ..."
    cat $tmp.out
    echo
    echo "... and the tail of the pmcd log ..."
    tail -20 $PCP_PMCDLOG_PATH
    echo
    echo "... and the root PMDA log ..."
    cat $ROOT_LOG_PATH
    echo
    echo "...and the PMDA log ..."
    cat $PMDA_LOG_PATH
}

_filter()
{
    _filter_pmda_install | \
    sed \
	-e 's/ or perl//' \
	-e 's/ or python//' \
	-e "s;$tmp;TMP;" \
	-e "s/$port1/PORT1/" \
	-e "s/$port2/PORT2/"
}

_full()
{
    echo "--- pmcd.conf ---" >>$seq_full
    cat $PCP_PMCDCONF_PATH >>$seq_full
    echo "--- pmcd.log ---" >>$seq_full
    cat $PCP_PMCDLOG_PATH >>$seq_full
    echo "--- root.log ---" >>$seq_full
    if [ -f $ROOT_LOG_PATH ]
    then
	cat $ROOT_LOG_PATH >>$seq_full
    else
	echo "$ROOT_LOG_PATH: file not found" >>$seq_full
    fi
    echo "--- simple.log ---" >>$seq_full
    if [ -f $PMDA_LOG_PATH ]
    then
	cat $PMDA_LOG_PATH >>$seq_full
    else
	echo "$PMDA_LOG_PATH: file not found" >>$seq_full
    fi
    echo >>$seq_full
}


# pmlogger may get confused by pmcd reconfig ... safest to stop it
# first, and restart it in cleanup()
#
if ! _service pmlogger stop >>$seq_full; then _exit 1; fi

# real QA test starts here
home=$PCP_PMDAS_DIR
iam=simple
if [ ! -d $home/$iam ]
then
    echo "Where is $home/$iam?"
    exit 1
fi
cd $home/$iam
unset ROOT MAKEFLAGS

# copy the pmcd config file to restore state later.
_save_config $PCP_PMCDCONF_PATH
# ditto for simple pmda config
[ -f $home/$iam/$iam.conf ] && _save_config $home/$iam/$iam.conf

if $sudo $PCP_MAKE_PROG clobber >$tmp.out 2>&1
then
    :
else
    cat $tmp.out
    echo "Arrgh, make clobber failed"
    exit
fi

# start from a known starting point
$sudo ./Remove >/dev/null 2>&1

echo
echo "=== pipe daemon agent ==="
echo "=== pipe daemon agent ===" >>$seq_full
$sudo ./Remove >$tmp.out 2>&1
$sudo rm -f $PMDA_LOG_PATH
echo 'daemon
pipe' | $sudo ./Install -e >>$tmp.out 2>&1
_full

_filter <$tmp.out

if pminfo -v simple
then
    :
else
    _failed
fi

# give the PMDA + socket a chance to be setup before pminfo check
export PCPQA_CHECK_DELAY=3

echo
echo "=== Unix socket daemon agent ==="
echo "=== Unix socket daemon agent ===" >>$seq_full
$sudo ./Remove >$tmp.out 2>&1
echo 'daemon
socket
Unix
'"$tmp.fifo-1" | $sudo ./Install -e >>$tmp.out 2>&1
echo "+++ fifo-1 PMDA log +++" >>$seq_full
cat $PMDA_LOG_PATH >>$seq_full
ls -ld "$tmp.fifo-1" `dirname "$tmp.fifo-1"` >>$seq_full
$sudo rm -f $PMDA_LOG_PATH
echo 'daemon
socket
Unix
'"$tmp.fifo-2" | $sudo ./Install -e >>$tmp.out 2>&1
echo "+++ fifo-2 PMDA log +++" >>$seq_full
cat $PMDA_LOG_PATH >>$seq_full
ls -ld "$tmp.fifo-2" `dirname "$tmp.fifo-2"` >>$seq_full
_full

_filter <$tmp.out

if pminfo -v simple
then
    :
else
    _failed
fi

echo
echo "=== Internet socket daemon agent ==="
echo "=== Internet socket daemon agent ===" >>$seq_full
$sudo ./Remove >$tmp.out 2>&1

# pick a tcp port that is not in use
#
port1=`_get_port tcp 5650 5699`
if [ -z "$port1" ]
then
    echo "Arrgh ... no free TCP port in the range 5650 ... 5699"
    exit 1
fi

echo 'daemon
socket
Internet
'$port1 | $sudo ./Install -e >>$tmp.out 2>&1

# use next port this time
port2=`expr $port1 + 1`

$sudo rm -f $PMDA_LOG_PATH
echo 'daemon
socket
Internet
'$port2 | $sudo ./Install -e >>$tmp.out 2>&1
_full

_filter <$tmp.out

if pminfo -v simple
then
    :
else
    _failed
fi

echo
echo "=== dso agent ==="
echo "=== dso agent ===" >>$seq_full
$sudo ./Remove >$tmp.out 2>&1
echo dso | $sudo ./Install -e >>$tmp.out 2>&1
$sudo rm -f $PMDA_LOG_PATH
echo dso | $sudo ./Install -e >>$tmp.out 2>&1
_full

_filter <$tmp.out

if pminfo -v simple
then
    :
else
    _failed
fi

$sudo ./Remove >$tmp.out 2>&1
_filter <$tmp.out

status=0
exit
