#!/bin/sh
# PCP QA Test No. 695
# Exercise the perl version of the trivial PMDA.
#
# Copyright (c) 2018 Red Hat.
#

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

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

perl -e "use PCP::PMDA" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "perl PCP::PMDA module not installed"
[ -f $PCP_PMDAS_DIR/trivial/pmdatrivial.perl ] \
             || _notrun "perl trivial PMDA is not installed"

status=1	# failure is the default!
$sudo rm -rf $tmp $tmp.* $seq.full

pmdatrivial_remove()
{
    echo; echo "=== remove trivial agent ==="
    $sudo ./Remove >$tmp.out 2>&1
    _filter_pmda_remove <$tmp.out
}

pmdatrivial_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/trivial
    $sudo ./Remove >/dev/null 2>&1

    echo; echo "=== trivial agent $@ installation ===" | tee -a $here/$seq.full
    echo $@ | $sudo ./Install >$tmp.out 2>&1
    cat $tmp.out >>$here/$seq.full
    _filter_pmda_install <$tmp.out
}

_prepare_pmda trivial
trap "_cleanup_pmda trivial; exit \$status" 0 1 2 3 15

# real QA test starts here
pmdatrivial_install perl

firsts=`pmdate %s`	# seconds since the epoch (before sampling)
pmprobe -v trivial.time > $tmp.out
echo pmdate $firsts >> $here/$seq.full
echo pmprobe >> $here/$seq.full
cat $tmp.out >> $here/$seq.full
seconds=`cat $tmp.out | awk '{print $3}'`

if [ $firsts -gt $seconds ]
then
    echo "trivial.time is incorrect - $firsts vs $seconds"
    exit
fi
echo "trivial.time looks OK"

pmdatrivial_remove
status=0
exit
