#!/bin/sh
#
# common procedures for PCP QA scripts
#
# Copyright (c) 1997-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

# may be needed in common.* scripts
#
here=`pwd`

# for _wait_for_pmlogger() and _change_config() and _service()
. ./common.check 

# per product QA script customization
#
. ./common.setup
[ -z "$DEFAULT_HOST" ] && DEFAULT_HOST=`hostname`

_setenvironment()
{
    MSGVERB="text:action"
    export MSGVERB
    export PCP_BINADM_DIR
    export PCP_DERIVED_CONFIG=""
}

_log_fyi()
{
    echo "FYI ... here are the PMCD logs"
    for __log
    do
	if [ ! -f $__log ]
	then
	    # try in the other place
	    #
	    case $__log
	    in
		$PCP_LOG_DIR/pmcd/*)
		    __try=`echo $__log | sed -e "s;/pmcd/;/;"`
		    ;;
		*)
		    __try=`echo $__log | sed -e "s;$PCP_LOG_DIR/;$PCP_LOG_DIR/pmcd/;"`
		    ;;
	    esac
	    [ -f $__try ] && __log=$__try
	fi
	if [ -f $__log ]
	then
	    echo ":::::::::  start $__log  ::::::::"
	    cat $__log
	    echo "::::::::::  end $__log  :::::::::"
	    
	    [ $__log != $PCP_LOG_DIR/pmcd/pmcd.log ] && $sudo rm -f $__log
	else
	    echo "$__log: not found"
	fi
    done
}

_havesudo()
{
    if [ -z "$PCP_DIR" ]
    then
	__setuid=`$sudo id | sed -e 's/(.*//' -e 's/.*=//'`
	if [ "$__setuid" != 0 ]
	then
	    echo "\"sudo\" is not an executable setuid root. This is fatal."
	    echo "As root, you need to setup your sudoers file for pcpqa."
	    exit 1
	fi
    fi
}

_haveagents()
{
    __restart=false
    __here=`pwd`

    # PMDAs than need to be here for QA
    #
    __common_pmdas="sample sampledso simple"
    if [ -d $PCP_PMDAS_DIR/cisco ]
    then
	# cisco PMDA requires that you can reach a router
	#
	if which ping >/dev/null 2>&1
	then
	    #  Note: PCP_QA_CISCOROUTER needs to be defined in common.rc
	    if ping -c 1 $PCP_QA_CISCOROUTER >/dev/null 2>&1
	    then
		__common_pmdas="$__common_pmdas cisco"
	    fi
	fi
    fi

    for __agent in $__common_pmdas
    do
	__probe=$__agent
	__agentlog=$PCP_LOG_DIR/pmcd/$__agent.log

	# make this empty to stop any attempt to reinstall
	#
	__agentdir=$PCP_PMDAS_DIR/$__agent

	case $__agent
	in
	    cisco)
		__probe=cisco.bytes_in
		;;
	    sample)
		__probe=sample.milliseconds
		;;
	    sampledso)
		__probe=sampledso.milliseconds
		__agentdir=$PCP_PMDAS_DIR/sample
		;;
	    simple)
		__probe=simple.numfetch
		;;
	esac
	__reinstall=false
	while true
	do
	    [ $__diff = true ] || echo "PMDA probe: pminfo -h $QA_HOST -f $__probe"
	    if pminfo -h $QA_HOST -f $__probe 2>&1 \
	       | tail -1 \
	       | grep -E "^[ 	]*((value)|(inst))[ 	]" >/dev/null
	    then
		break
	    else
		echo "PMDA $__agent is not responding"
		if $__restart
		then
		    :
		else
		    _log_fyi $PCP_LOG_DIR/pmcd/pmcd.log $__agentlog
		    echo "Restarting PMCD ..."
		    _service pmcd restart
		    _wait_for_pmcd
		    __restart=true
		fi
		if $__reinstall
		then
		    echo "Cannot make PMDA $__agent work, ... giving up!"
		    exit 1
		else
		    if [ -z "$__agentdir" ]
		    then
			echo "Skip PMDA re-install"
			break
		    elif [ -d "$__agentdir" ]
		    then
			echo "Trying to re-install PMDA $__agent from $__agentdir ..."
			cd $__agentdir
			unset ROOT TOOLROOT MAKEFLAGS
			$sudo ./Remove </dev/null
			case $__agent
			in
			    cisco)
				cd $__here
				./common.install.cisco $__scriptname $__agentdir
				;;
			    *)
				$sudo ./Install </dev/null
				;;
			esac
			cd $__here
			_log_fyi $PCP_LOG_DIR/pmcd/pmcd.log $__agentlog
			__reinstall=true
		    else
			echo "Cannot find PMDA directory ($__agentdir)"
			echo "to re-install, ... giving up!"
			exit 1
		    fi
		fi
	    fi
	done
    done
}

_havelogger()
{
    if [ -z "`_get_pids_by_name -a 'pmlogger.*-P'`" ]
    then
	echo "Primary pmlogger not running ..."
	echo "chkconfig pmlogger on, and restart PMCD"
	_change_config pmlogger on
	if sed -e '/^#/d' "${PCP_PMLOGGERCONTROL_PATH}" \
	   | $PCP_AWK_PROG '$2 == "y" && $3 == "n" { sts = 1 } END { exit 1-sts }'
	then
	    : echo "have primary pmlogger line in main control file"
	elif sed -e '/^#/d' "${PCP_PMLOGGERCONTROL_PATH}.d/local" \
	   | $PCP_AWK_PROG '$2 == "y" && $3 == "n" { sts = 1 } END { exit 1-sts }'
	then
	    : echo "have primary pmlogger line in local control file"
	else
	    : echo "no primary pmlogger line in control file"
	    if $PCP_AWK_PROG '$2 == "y" && $3 == "n" { sts = 1} END { exit 1-sts }' <$PCP_PMLOGGERCONTROL_PATH
	    then
		: echo uncomment
		sed <$PCP_PMLOGGERCONTROL_PATH >$tmp.tmp \
		    -e '/^#[^ 	][^ 	]*[ 	][ 	]*y/s/#//'
		$sudo cp $tmp.tmp $PCP_PMLOGGERCONTROL_PATH
	    elif $PCP_AWK_PROG '$2 == "y" && $3 == "n" { sts = 1} END { exit 1-sts }' <$PCP_PMLOGGERCONTROL_PATH.d/local
	    then
		: echo uncomment
		sed <$PCP_PMLOGGERCONTROL_PATH.d/local >$tmp.tmp \
		    -e '/^#[^ 	][^ 	]*[ 	][ 	]*y/s/#//'
		$sudo cp $tmp.tmp $PCP_PMLOGGERCONTROL_PATH.d/local
	    else
		: echo add
		cp $PCP_PMLOGGERCONTROL_PATH.d/local $tmp.tmp
		cat >>$tmp.tmp <<'End-of-File'

# Installed by PCP QA common on `date`
LOCALHOSTNAME	y   n	PCP_ARCHIVE_DIR/LOCALHOSTNAME	-c config.default
End-of-File
		$sudo cp $tmp.tmp $PCP_PMLOGGERCONTROL_PATH.d/local
	    fi
	fi
	
	# we're seeing races here and timeouts for the first pmcd
	# restart after a package install ... just going one step
	# at a time, carefully ...
	#
	__pid=`_get_pids_by_name -a 'pmlogger.*-P'`
	_service pmlogger stop
	if [ -n "$__pid" ]
	then
	    # odd ... a primary pmlogger has appeared ... wait for it to
	    # go away
	    _wait_pmlogger_end $__pid
	fi
	_service pmcd stop
	_wait_pmcd_end
	_service pmcd start
	_wait_for_pmcd
	_service pmlogger start
	_wait_for_pmlogger
    fi
}

_haveremote()
{
    # ensure critical daemons allow for remote access

    if test -f "${PCP_SYSCONFIG_DIR}/pmcd"
    then
	sed <$PCP_SYSCONFIG_DIR/pmcd >$tmp.tmp \
		-e '/^PMCD_LOCAL=1/s/^/#PCP QA# /g'
	diff $PCP_SYSCONFIG_DIR/pmcd $tmp.tmp >/dev/null || \
	$sudo cp $tmp.tmp $PCP_SYSCONFIG_DIR/pmcd
    fi

    if test -f "${PCP_SYSCONFIG_DIR}/pmproxy"
    then
	sed <$PCP_SYSCONFIG_DIR/pmproxy >$tmp.tmp \
		-e '/^PMPROXY_LOCAL=1/s/^/#/g'
	diff $PCP_SYSCONFIG_DIR/pmproxy $tmp.tmp >/dev/null || \
	$sudo cp $tmp.tmp $PCP_SYSCONFIG_DIR/pmproxy
    fi

    if test -f "${PCP_SYSCONFIG_DIR}/pmlogger"
    then
	sed <$PCP_SYSCONFIG_DIR/pmlogger >$tmp.tmp \
		-e '/^PMLOGGER_LOCAL=1/s/^/#/g'
	diff $PCP_SYSCONFIG_DIR/pmlogger $tmp.tmp >/dev/null || \
	$sudo cp $tmp.tmp $PCP_SYSCONFIG_DIR/pmlogger
    fi
}

_havepmcdtrace()
{
    __fix=false
    for __trace in traceconn tracepdu
    do
	__bit=`pminfo -h $QA_HOST -f pmcd.control.$__trace | sed -n -e '/value/s/ *value *//p'`
	if [ "X$__bit" != X1 ]
	then
	    pminfo -h $QA_HOST -f pmcd.control.$__trace
	    echo "PMCD event tracing not enabled!"
	    __fix=true
	fi
    done
    if $__fix
    then
	echo "fix options, and restart PMCD"
	[ -z "PCP_PMCDOPTIONS_PATH" ] && \
	      PCP_PMCDOPTIONS_PATH="$PCP_SYSCONF_DIR/pmcd/pmcd.options"
	PCPQA_PMCDOPTIONS="$PCP_PMCDOPTIONS_PATH"
	sed <$PCPQA_PMCDOPTIONS >$tmp.tmp -e '/^-T/s/^/#/'
	cat >>$tmp.tmp <<End-of-File

# Installed by PCP QA common on `date`
-T 3
End-of-File
	$sudo cp $tmp.tmp $PCPQA_PMCDOPTIONS
	__grp=root
	case $PCP_PLATFORM
	in
	    freebsd|netbsd|openbsd)
		    __grp=wheel
		    ;;
	esac
	$sudo chown root:$__grp $PCPQA_PMCDOPTIONS
	_service pmcd restart
	_wait_for_pmcd
    fi

}

_setenvironment

__check=${__check-true}
__quick=${__quick-false}

# Try and guess what "make" is called ... configure leaves a comment
# like this
#NB: don't override $(MAKE); gnumake sets it well, propagating -j etc.
#MAKE	= /usr/local/bin/gmake
# in builddefs, so try there ...
#
if [ -f $PCP_INC_DIR/builddefs ]
then
    __make=`sed <$PCP_INC_DIR/builddefs -n -e '/^#NB: don.t override \$(MAKE);/{
N
s/.*MAKE[ 	]*=[ 	]*//p
}'`
    [ -n "$__make" ] && export MAKE=$__make
fi

if $__check
then
    if $__quick
    then
	:
    else
	if [ -f GNUmakefile.install ]
	then
	    # running QA in the tree
	    ${MAKE:-make} -f GNUmakefile.install >>/tmp/$$.make 2>&1
	    __sts=$?
	    ( cd qt; ./setup-executables )
	else
	    ${MAKE:-make} >/tmp/$$.make 2>&1
	    __sts=$?
	fi
	if [ $__sts != 0 ]
	then
	    cat /tmp/$$.make
	    echo "Warning: ${MAKE:-make} failed -- some tests may be missing"
	fi
	rm -f /tmp/$$.make
    fi
fi

__diff=diff
if [ ! -z "$DISPLAY" ]
then
    which tkdiff >/dev/null 2>&1 && __diff=tkdiff
    which xdiff >/dev/null 2>&1 && __diff=xdiff
    which xxdiff >/dev/null 2>&1 && __diff=xxdiff
    which gdiff >/dev/null 2>&1 && __diff=gdiff
fi

__color=false
__group=false
__xgroup=false
__exclude=false
__rflag=false
__selbygroup=false
__snarf=''
__showme=false
__have_test_arg=false
__check_config=false
__timestamp=false
__timings_file=false
rm -f $tmp.list $tmp.tmp $tmp.sed $tmp.exclude

for __r
do

    if $__group
    then
    	__selbygroup=true

	# arg after -g
	__group_list=`
	if $__rflag
	then
	    sed <group \
		-e 's/\([0-9]\):retired /\1 /' \
		-e 's/\([0-9]\):reserved /\1 /' \
	    # end
	else
	    sed <group \
		-e '/[0-9]:retired /d' \
		-e '/[0-9]:reserved /d' \
	    # end
	fi \
	| sed -n -e 's/$/ /' -e "/^[0-9][0-9]*.* $__r /"'{
s/ .*//p
}'`
	if [ -z "$__group_list" ]
	then
	    echo "Group \"$__r\" is empty or not defined; ignored" >&2
	else
	    [ ! -s $tmp.list ] && touch $tmp.list
	    for __t in $__group_list
	    do
		if grep -s "^$__t\$" $tmp.list >/dev/null
		then
		    :
		else
		    echo "$__t" >>$tmp.list
		fi
	    done
    	fi
	__group=false
	continue

    elif $__xgroup
    then
    	__selbygroup=true

	# if no test numbers, do everything from group file
	[ -s $tmp.list ] || \
	if $__rflag
	then
	    sed -e 's/\([0-9]\):\(retired\|reserved\) /\1 /' <group
	else
	    sed -e '/[0-9]:\(retired\|reserved\) /d' <group
	fi \
	| sed -n -e '/^[0-9]/s/[ 	].*//p' >$tmp.list

	# arg after -x
	__group_list=`
	if $__rflag
	then
	    sed -e 's/\([0-9]\):\(retired\|reserved\) /\1 /' <group
	else
	    sed -e '/[0-9]:\(retired\|reserved\) /d' <group
	fi \
	| sed -n \
	    -e "/^[0-9].* $__r /s/[ 	].*//p" \
	    -e "/^[0-9].* $__r\$/s/[ 	].*//p"`
	if [ -z "$__group_list" ]
	then
	    echo "Group \"$__r\" is empty or not defined; ignored" >&2
	else
	    __numsed=0
	    rm -f $tmp.sed
	    for __t in $__group_list
	    do
		if [ $__numsed -gt 100 ]
		then
		    sed -f $tmp.sed <$tmp.list >$tmp.tmp
		    mv $tmp.tmp $tmp.list
		    __numsed=0
		    rm -f $tmp.sed
		fi
		echo "/^$__t\$/d" >>$tmp.sed
		__numsed=`expr $__numsed + 1`
	    done
	    sed -f $tmp.sed <$tmp.list >$tmp.tmp
	    mv $tmp.tmp $tmp.list
    	fi
	__xgroup=false
	continue

    elif $__exclude
    then
	# if no test numbers, do everything from group file
	[ -s $tmp.list ] || \
	if $__rflag
	then
	    sed -e 's/\([0-9]\):\(retired\|reserved\) /\1 /' <group
	else
	    sed -e '/[0-9]:\(retired\|reserved\) /d' <group
	fi \
	| sed -n -e '/^[0-9]/s/[ 	].*//p' >$tmp.list

	if echo "$__r" | grep '[0-9]-[0-9]' >/dev/null
	then
	    eval `echo $__r | sed -e 's/^/__start=/' -e 's/-/ __end=/'`
	    $PCP_AWK_PROG </dev/null '
BEGIN	{ for (t = '$__start'; t <= '$__end'; t++) printf "%03d\n",t }' \
	    | while read __id
	    do
		# if test not present, silently forget about it
		if [ ! -f $__id ]
		then
		    continue
		fi
		if grep -E -s "^$__id([: 	]|\$)" group >/dev/null
		then
		    # in group file ... OK
		    echo "/^$__id\$/d" >>$tmp.exclude
		fi
	    done
	else
	    for __t in `echo "$__r" | sed -e 's/,/ /g'`
	    do
		echo "/^0*$__t\$/d" >>$tmp.exclude
	    done
	fi
	__exclude=false
	continue

    elif [ ! -z "$__snarf" ]
    then
	case $__snarf
	in
	    d)
		QA_DIR=$__r
		;;
	    h)
		QA_HOST=$__r
		;;
	    u)
		QA_USER=$__r
		;;
	esac
	__snarf=''
	continue
    fi

    __xpand=true
    __range=false
    case "$__r"
    in

	-\?)	# usage
	    echo "Usage: $0 [options] [testlist]"'

check options
    -c		check configuration files [off]
    -C		color mode output [off]
    -CI		run in CI environment => set $PCPQA_IN_CI in environment
    		and -x x11 -x remote -x not_in_ci
    -g group	include tests from these groups (multiple flags allowed)
    -G          report groups and number of tests per group
    -l		line mode diff [xdiff]
    -s		sssh mode diff [no diff, regular output]
    -n		show me, do not run tests
    -q		quick, no PMDA checks (you are on your own)
    -r		include reserved and retired tests
    -T		output timestamps
    -TT		write timestamps to check.timings
    -x group	exclude tests from these groups (multiple flags allowed)
    -X seq[,seq...]
		exclude these tests

show-me options
    -d QA_DIR		[isms/pcp2.0/qa]
    -h QA_HOST		['`hostname`']
    -u QA_USER		[pcpqa]
'
	    __aborted=false
	    exit 0
	    ;;

	-c)	# check config files
	    __check_config=true
	    __xpand=false
	    ;;

	-C)	# color mode for results and summaries
	    __color=true
	    __xpand=false
	    ;;

	-CI)	# run in CI environment
		# TODO still need -x x11 -x remote -x not_in_ci
	    export PCPQA_IN_CI=yes
	    __xpand=false
	    ;;

	-d)	# directory for show-me
	    __snarf=d
	    __xpand=false
	    ;;

	-G)	# -G report groups and number of tests per group
	    sed -n -e '/^[a-z]/{
s/[ 	].*//
p
}' <group >$tmp.group
	    cat $tmp.group
	    echo ===
	    LC_COLLATE=POSIX sort $tmp.group \
	    | while read __name
	    do
		__num=`
		    if $__rflag
		    then
			sed -e 's/\([0-9]\):\(retired\|reserved\) /\1 /' <group
		    else
			sed -e '/[0-9]:\(retired\|reserved\) /d' <group
		    fi \
		    | grep -E "^[0-9].*[ 	]$__name([ 	]|\$)" \
		    | wc -l \
		    | sed -e 's/ //g'`
		printf "%-20.20s %3d\n" $__name $__num
	    done \
	    | LC_COLLATE=POSIX sort -nr -k2,2
	    __aborted=false
	    exit 0
	    ;;

	-g)	# -g group ... pick from group file
	    __group=true
	    __xpand=false
	    ;;

	-h)	# host for show-me
	    __snarf=h
	    __xpand=false
	    ;;

	-l)	# line mode for diff, not gdiff over modems
	    __diff=diff
	    __xpand=false
	    ;;

	-n)	# show me, don't do it
	    __showme=true
	    __quick=true
	    __xpand=false
	    ;;

	-q)	# "quick", no PMDA checks (you are on your own)
	    __quick=true
	    __xpand=false
	    ;;

	-r)	# include reserved and retired tests
	    __rflag=true
	    __xpand=false
	    ;;

	-s)	# sssh mode for diff, no diff at all 
	    __diff=true
	    __xpand=false
	    ;;

	-T)	# turn on timestamp output
	    __timestamp=true
	    __xpand=false
	    ;;

	-TT)	# turn on check.timings
	    __timings_file=true
	    __xpand=false
	    ;;

	-u)	# user for show-me
	    __snarf=u
	    __xpand=false
	    ;;

	-x)	# -x group ... exclude from group file
	    __xgroup=true
	    __xpand=false
	    ;;

	-X)	# -X seq[,seq...] or -X seq-seq ... exclude explicit tests
	    __exclude=true
	    __xpand=false
	    ;;

	'[0-9][0-9][0-9] [0-9][0-9][0-9][0-9]')
	    echo "No tests?" >&2
	    status=1
	    exit $status
	    ;;

	[0-9]*-[0-9]*)
	    eval `echo $__r | sed -e 's/^/__start=/' -e 's/-/ __end=/'`
	    __range=true
	    ;;

	[0-9]*-)
	    eval `echo $__r | sed -e 's/^/__start=/' -e 's/-//'`
	    __end=`echo [0-9][0-9][0-9] [0-9][0-9][0-9][0-9] | sed -e 's/\[0-9]//g' -e 's/  *$//' -e 's/.* //'`
	    if [ -z "$__end" ]
	    then
		echo "No tests in __range \"$__r\"?" >&2
		status=1
		exit $status
	    fi
	    __range=true
	    ;;

	[0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])
	    __start=$__r
	    __end=$__r
	    ;;

	*)
	    echo "Error: \"$__r\" is not a valid QA test number" >&2
	    status=1
	    exit $status
	    ;;

    esac

    if $__xpand
    then
	__start=`echo $__start | sed -e 's/^0*\(.\)/\1/'`
	__end=`echo $__end | sed -e 's/^0*\(.\)/\1/'`
	__have_test_arg=true
	if $__rflag
	then
	    sed -e 's/\([0-9]\):\(retired\|reserved\) /\1 /' <group
	else
	    sed -e '/[0-9]:\(retired\|reserved\) /d' <group
	fi >$tmp.group
	$PCP_AWK_PROG </dev/null '
BEGIN	{ for (t = '$__start'; t <= '$__end'; t++) printf "%03d\n",t }' \
	| while read __id
	do
	    # if test not present, silently forget about it
	    if [ ! -f $__id ]
	    then
		if [ "$__scriptname" != "show-me" ]
		then
		    if [ $__id = $__start ]
		    then
			echo "$__id - no such test, skipped"
		    elif [ $__id = $__end ]
		    then
			echo "$__id - no such test, skipped"
		    fi
		fi
		continue
	    fi
	    if grep -E -s "^$__id([ 	]|$)" $tmp.group >/dev/null
	    then
		# in group file ... OK
		echo $__id >>$tmp.list
	    elif grep -E -s "^$__id:retired([ 	]|$)" group >/dev/null
	    then
		# in group file, but retired
		if $__rflag
		then
		    echo $__id >>$tmp.list
		else
		   $__range || echo "$__id - retired test, ignored"
		fi
	    elif grep -E -s "^$__id:reserved([ 	]|$)" group >/dev/null
	    then
		# in group file, but reserved
		if $__rflag
		then
		    echo $__id >>$tmp.list
		else
		    $__range || echo "$__id - reserved test, ignored"
		fi
	    else
		# oops
		$__range || echo "$__id - unknown test, ignored"
	    fi
	done
    fi

done

if [ -s $tmp.list ]
then
    # found some valid test numbers ... this is good
    :
else
    if $__have_test_arg
    then
	# had test numbers, but none in group file ... do nothing
	touch $tmp.list
    else
	if $__selbygroup
    	then
    	    echo "No tests selected?" >&2
    	    exit 1
    	else
	    # no test numbers, do everything from group file
	    touch $tmp.list
	    if $__rflag
	    then
		sed -e 's/\([0-9]\):\(retired\|reserved\) /\1 /' <group
	    else
		sed -e '/[0-9]:\(retired\|reserved\) /d' <group
	    fi \
	    | sed -n -e '/^[0-9]/{
s/[ 	].*//
p
}' \
	    | while read __id
	    do
		[ -f $__id ] || continue
		echo $__id >>$tmp.list
	    done
    	fi
    fi
fi

# handle -X exclusions
#
if [ -s $tmp.exclude ]
then
    sed -f $tmp.exclude <$tmp.list >$tmp.tmp
    mv $tmp.tmp $tmp.list
fi

__list=`sort -n $tmp.list`
rm -f $tmp.list $tmp.tmp $tmp.sed $tmp.exclude

# re-process the product-specific setup in case command line args have
# changed things, e.g. -h hostname
#
. ./common.setup

[ -z "$QA_HOST" ] && QA_HOST=$DEFAULT_HOST
export QA_HOST QA_DIR QA_USER

if $__check
then
    # try loopback default (-h localhost) first ...
    #
    __x=`pminfo -v pmcd.control.timeout 2>&1`
    if [ -n "$__x" ]
    then
	echo "Is pmcd running on localhost?  Simple test produces ..."
	echo "$__x"
	exit 1
    fi
    # now try -h localhostname
    #
    $sudo rm -f $tmp.ok
    __x=`pminfo -h $QA_HOST -v pmcd.control.timeout 2>&1`
    if [ -n "$__x" ]
    then
	case "$__x"
	in
	    *'Connection refused')
		# OK, now this might be a Fedora 27 style config set up
		# where PMCD_LOCAL is set in the environment before the
		# pmcd start up script is run
		#
		if [ -n "$PCP_SYSCONFIG_DIR" -a -f $PCP_SYSCONFIG_DIR/pmcd ]
		then
		    if grep '^[^#]*PMCD_LOCAL=1' $PCP_SYSCONFIG_DIR/pmcd >/dev/null
		    then
			_haveremote
			_service pmcd restart
			_wait_for_pmcd
			__x=`pminfo -h $QA_HOST -v pmcd.control.timeout 2>&1`
			[ -z "$__x" ] && touch $tmp.ok
		    fi
		fi
		;;
	esac
	if [ ! -f $tmp.ok ]
	then
	    echo "Is pmcd running on host $QA_HOST?  Simple test produces ..."
	    echo "$__x"
	    exit 1
	fi
    fi
fi

case $__scriptname
in
    show-me)
	# don't need sudo
	;;
    *)
	_havesudo
	;;
esac

case $__scriptname
in
    check|remake)
	# $PCP_RC_DIR/pcp needs this to produce deterministic output now
	#
	_change_config verbose on
	;;
esac

if $__quick
then
    :
else
    ./mk.localconfig

    $OPTION_AGENTS && _haveagents
    $OPTION_LOGGER && _havelogger
    $OPTION_REMOTE && _haveremote
    $OPTION_PMCD_TRACE && _havepmcdtrace

    if [ -f GNUmakefile.install ]
    then
	# running QA in the tree
	${MAKE:-make} -f GNUmakefile.install setup
	cd pmdas
	if ${MAKE:-make} -f GNUmakefile.install setup >$tmp.tmp 2>&1
	then
	    :
	else
	    cat $tmp.tmp
	    echo "Warning: pmdas: $MAKE -f GNUmakefile.install setup failed"
	fi
	cd broken
	if ${MAKE:-make} -f GNUmakefile.install broken_v2.dir >$tmp.tmp 2>&1
	then
	    :
	else
	    cat $tmp.tmp
	    echo "Warning: pmdas/broken: $MAKE -f GNUmakefile.install broken_v2.dir failed"
	fi
	cd ../..
    else
	${MAKE:-make} setup
    fi
    if [ $? != 0 ]
    then
	echo "Error: ${MAKE:-make} setup failed"
	exit 1
    fi

    # since pmcd no longer runs as root, normal users must be able
    # to read all dirs on the path to the qa home ...
    #
    __x=`pwd`
    while [ -n "$__x" -a "$__x" != / ]
    do
	__prot=`ls -ldL "$__x" | sed -e 's/[ 	].*//'`
	case $__prot
	in
	    dr?xr?xr?[xt]*)
		;;
	    *)
		echo "Warning: parent directory $__x (mode:$__prot) not world readable and searchable"
		;;
	esac
	__x=`dirname $__x`
    done

    # if we have selinux and it is enforcing, and PCP was build with
    # selinux enabled, then make sure the PCP QA policy module
    # (pcp-testsuite) is installed
    #
    ENABLE_SELINUX=false
    if [ -f $PCP_INC_DIR/builddefs ]
    then
	eval `sed -n -e '/ENABLE_SELINUX =/s/ //gp' <$PCP_INC_DIR/builddefs`
    fi
    #debug# echo "ENABLE_SELINUX=$ENABLE_SELINUX"
    if $ENABLE_SELINUX
    then
	if which getenforce >/dev/null 2>&1
	then
	    case `getenforce`
	    in
		Enforcing)
		    if $sudo semodule -l | grep pcp-testsuite >/dev/null
		    then
			# nothing to do, module already installed
			# ... to force a re-install, e.g. after the module
			# has been updated, do
			# $ sudo semodule -r pcp-testsuite
			# first
			#
			:
		    else
			$sudo semodule -i pcp-testsuite.pp
		    fi
		    # and now any selinux booleans that help
		    #
		    for __bool in pcp_read_generic_logs pcp_bind_all_unreserved_ports
		    do
			if $sudo getsebool $__bool 2>/dev/null | grep ' --> off' >/dev/null
			then
			    $sudo setsebool $__bool true
			fi
		    done
		    ;;
		*)
		    ;;
	    esac
	fi
    fi
fi

# Hook for check.callback to allow once-per-check-run tests
#
touch check.onetrip
