#!/bin/sh
# PCP QA Test No. 510
# pmlogger -Dall calls __pmDumpResult() which fails for usage with
# pmlc control requests
#
# Copyright (c) 2012 Ken McDonell.  All Rights Reserved.
#
# check-group-include: pmlogger

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

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

status=1
$sudo rm -rf $tmp.* $seq.full
trap "_cleanup" 0 1 2 3 15
needclean=true

_cleanup()
{
    if $needclean
    then
	_restore_config $control
	needclean=false
	_restore_primary_logger
	echo "Stop/start at end ..." >>$here/$seq.full
	_stop_primary_pmlogger
	_service pmlogger start 2>&1 | _filter_pcp_start
	_wait_for_pmlogger
    fi
    $sudo rm -f $tmp.*
    exit $status
}

_filter()
{
    sed \
	-e 's/Version [0-9].*/Version VERSION/'
}

# Note.  If cron-based or systemd-timers are running we have a
# potential race with a primary pmlogger being started after
# we stop the primary logger and before we start our special -Dall
# one.
# This procedure does not protect against that, but it does minimize
# the risk somewhat ... sigh.
#
_stop_primary_pmlogger()
{
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '[p]mlogger' >>$here/$seq.full
    _pid=`_get_pids_by_name -a 'pmlogger.*-P'`
    _numpid=`echo $_pid | wc -w | sed -e 's/ //g'`
    echo "Found: $_numpid ($_pid)" >>$here/$seq.full
    case "$_numpid"
    in
	0)
	    echo "Arrgh ... expected one primary logger, found none"
	    exit
	    ;;
	1)
	    ;;
	*)
	    echo "Arrgh ... expecting one primary logger, found $_numpid ($_pid)"
	    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '[p]mlogger'
	    exit
	    ;;
    esac
    ls -l $PCP_TMP_DIR/pmlogger >>$here/$seq.full
    _service pmlogger stop 2>&1 | _filter_pcp_stop
    _wait_pmlogger_end $_pid >>$here/$seq.full 2>&1
}

control=$PCP_PMLOGGERCONTROL_PATH
[ -f $PCP_PMLOGGERCONTROL_PATH.d/local ] && \
control=$PCP_PMLOGGERCONTROL_PATH.d/local

# stop primary logger, add -Dall to config file and restart pmlogger
echo "Stop/start at beginning ..." >>$here/$seq.full
_stop_primary_pmlogger

sed -e '/^LOCALHOSTNAME/s/$/ -Dall/' < $control >$tmp.control
_save_config $control
$sudo cp $tmp.control $control
_writable_primary_logger
_service pmlogger start 2>&1 | _filter_pcp_start
_wait_for_pmlogger

# real QA test starts here
pmlc -ie <<End-of-File | _filter
connect primary
query sample.event
log mandatory on once sample.event.records
query sample.event
End-of-File

# success, all done
status=0
exit
