#!/bin/sh
# PCP QA Test No. 560
# #681487 error handling in pmdaproc.sh dialogs
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

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

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ -d $PCP_PMDAS_DIR/simple ] || _notrun "simple PMDA directory is not installed"

status=1	# failure is the default!
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15
$sudo rm -f $seq.full

echo "PCPQA_IN_CI=$PCPQA_IN_CI" >>$seq.full
if [ "$PCPQA_IN_CI" = yes ]
then
    # in CI
    # failing on test-ubuntu1804i386-container and nowhere else ...
    # despite extensive triage, tcp port "already in use" problem
    # persists
    #
    eval `src/sizeof ptr`
    echo "ptr=$ptr" >>$seq.full
    if [ "$ptr" = 4 ]
    then
	admin/whatami >>$seq.full
	# 32-bit platform
	case `admin/whatami`
	in
	    *Ubuntu\ 18.04\ *)
		# bingo ... no cigar
		_notrun "cannot make this test work for test-ubuntu1804i386-container"
		;;
	esac
    fi
fi

# real QA test starts here
cd $PCP_PMDAS_DIR/simple

unset ROOT MAKEFLAGS

$sudo $PCP_MAKE_PROG clobber >>$here/$seq.full 2>&1
$sudo $PCP_MAKE_PROG >>$here/$seq.full 2>&1

_filter_make()
{
    sed \
	-e '/In function .vpmprintf./d' \
	-e '/warning: the use of .tempnam./d' \
	-e '/make: Nothing to be done/d'
}

# see note below on bind: Address already in use issue
#
port=`_get_port tcp 52078 52127`
if [ -z "$port" ]
then
    echo "Arrgh ... no free TCP port in the range 52078 ... 52127"
    exit
fi
echo "port=$port" >>$here/$seq.full
echo "hostname: `hostname`" >>$here/$seq.full

$sudo fuser tcp/$port >>$here/$seq.full 2>&1
cat <<End-of-File | $sudo ./Install -e >$tmp.out 2>&1
foo
daemon
foo
socket
foo
Unix

Internet
$port
End-of-File

tee -a $here/$seq.full <$tmp.out \
| sed \
    -e "s/ $port\$/ PORT/" \
    -e 's/or perl //g' \
    -e 's/or python //g' \
    -e 's/daemon or dso,/"daemon" or "dso",/g' \
| _filter_pmda_install \
| _filter_make

echo "in \$PCP_PMCDCONF_PATH ..."
sed -n <$PCP_PMCDCONF_PATH \
    -e '/^simple/{
s;/private/;/;
s;'"$PCP_PMDAS_DIR"';$PCP_PMDAS_DIR;
s/$/ /
s/\([ 	]\)'"$port"'\([ 	]\)/\1PORT\2/g
s/ $//
p
}'

if [ -f "$PCP_LOG_DIR/pmcd/simple.log" ]
then
    cat "$PCP_LOG_DIR/pmcd/simple.log" >>$here/$seq.full
else
    echo "$PCP_LOG_DIR/pmcd/simple.log: missing?"
fi
pminfo -f simple pmcd.agent.status >>$here/$seq.full

$sudo ./Remove

# need to be sure these back-to-back installs use a different
# port ... otherwise the second install risks smacking into a
# "bind: Address already in use" issue if the previous PMDA
# process has not cleaned up ... this is why the first install
# uses a port in the range starting at 5000+2078, so the second
# install below can use the default 2078 port
#

echo
if $sudo $PCP_MAKE_PROG >$tmp.tmp 2>$tmp.err
then
    cat $tmp.err | _filter_make
else
    cat $tmp.tmp
    echo "make prior to Install failed!"
fi
$sudo ./Install </dev/null >$tmp.out 2>&1
tee -a $here/$seq.full <$tmp.out \
| grep 'Check'

if [ -f "$PCP_LOG_DIR/pmcd/simple.log" ]
then
    cat "$PCP_LOG_DIR/pmcd/simple.log" >>$here/$seq.full
else
    echo "$PCP_LOG_DIR/pmcd/simple.log: missing?"
fi
pminfo -f simple pmcd.agent.status >>$here/$seq.full

# success, all done
status=0
exit
