#! /bin/sh
# PCP QA Test No. 575
#
# Copyright (c) 2012 Red Hat.
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

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

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

_cleanup()
{
    if $pmie_was_running
    then
	_restore_auto_restart pmie
	[ "$pmie_state" = on ] && _change_config pmie on
	_service pmie start >>$here/$seq.full 2>&1
    else
	_service pmie stop >>$here/$seq.full 2>&1
	$sudo $PCP_BINADM_DIR/pmsignal -a -s TERM pmie >>$here/$seq.full 2>&1
	_wait_pmie_end
	[ "$pmie_state" = on ] || _change_config pmie off
    fi
    rm -f $tmp.*
}

signal=$PCP_BINADM_DIR/pmsignal
status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

pmie_state=`_get_config pmie`
pmie_was_running=false
if [ -f $PCP_RUN_DIR/pmie.pid ]
then
    pmie_was_running=true
    echo "Found pmie PID `cat $PCP_RUN_DIR/pmie.pid` running @ start" >>$here/$seq.full
    _stop_auto_restart pmie
    _service pmie stop >>$here/$seq.full 2>&1
fi
$sudo $signal -a pmie >/dev/null 2>&1
_wait_pmie_end

# real QA test starts here

_change_config pmie on
# The "pmie not running" message does not appear when systemd is in
# control, as the "stop" is a no-op if the service is not already
# running
# And the "Waiting for pmie process(es) to terminate ..." message
# depends on the platform and the state of the system.
#
_service pmie stop | _filter_pmie_stop \
| sed \
    -e '/pmie not running/d' \
    -e '/Waiting for pmie process/d' \
# end

_service pmie start | _filter_pmie_start

# success, all done
status=0
exit
