#!/bin/sh
# PCP QA Test No. 728
# Basic workout for pmGetOptions PMAPI interfaces.
#
# non-valgrind variant, see qa/1730 for the valgrind variant
#
# Copyright (c) 2014 Red Hat.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

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

do_valgrind=false
if [ "$1" = "--valgrind" ]
then
    _check_valgrind
    do_valgrind=true
elif which valgrind >/dev/null 2>&1
then
    [ "$PCPQA_VALGRIND" = both ] || \
        _notrun "valgrind variant qa/1730 will be run"
fi

status=1	# failure is the default!
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

_filter()
{
    tee -a $seq_full \
    | sed \
	-e '/ (init) /d' \
	-e "s@$PCP_BINADM_DIR@\$PCP_BINADM_DIR@" \
	-e "s@$PCP_VAR_DIR@\$PCP_VAR_DIR@" \
	-e "s@$PCP_PMCDCONF_PATH@\$PCP_PMCDCONF_PATH@" \
	-e "s@$tmp@\$TMP@g" \
	-e '/Command: /s;Command: .*/getoptions;Command: .../getoptions;' \
    # end
}

# $tmp.env is optional and contains one env var setting
# per line
#
_getoptions()
{
    if [ -f $tmp.env ]
    then
	echo "+++ .env +++" >>$seq_full
	cat $tmp.env >>$seq_full
	# set
	. $tmp.env
	sed <$tmp.env >$tmp.set -e 's/=.*//' -e 's/^/export /'
	# export
	. $tmp.set
	echo "+++ .set +++" >>$seq_full
	cat $tmp.set >>$seq_full
    fi
    if $do_valgrind
    then
	_run_valgrind $here/src/getoptions "$@" 2>&1 | _filter
    else
	$here/src/getoptions "$@" 2>&1 | _filter
    fi
    if [ -f $tmp.env ]
    then
	sed <$tmp.env >$tmp.unset -e 's/=.*//' -e 's/^/unset /'
	# unset
	. $tmp.unset
	rm -f $tmp.env
    fi
}

# real QA test starts here
echo '== no arguments'
_getoptions

echo '== ... -?'
_getoptions -\?
echo '== ... --help'
_getoptions --help

echo '== ... -x -X goodness'
_getoptions -x -X goodness
echo '== ... --extra --eXtra goodness'
_getoptions --extra --eXtra goodness

echo '== ... -y -Y not [own short-only options]'
_getoptions -y -Y not

echo '== ... --fubar --foobar eek [own long-only options]'
_getoptions --fubar --foobar eek

echo '== ... -a archives/20041125.0 -z -A "1 minute" -O "@10:10:10" -S "@10:15" -T "@10:30" [one archive and all time, short options]'
_getoptions -a archives/20041125.0 -z \
	-A "1 minute" -O "@10:10:10" -S "@10:15" -T "@10:30"
echo '== [same but long options]'
_getoptions --archive archives/20041125.0 --hostzone \
	--align "1 minute" --origin "@10:10:10" \
	--start "@10:15" --finish "@10:30"

echo '== ... -h foo.bar.com -t 1sec -s 5 [one host]'
_getoptions -h foo.bar.com -t 1sec -s 5
echo '== [same but long options]'
_getoptions --host foo.bar.com --interval 1sec --samples 5

echo '== PM_OPTFLAG_ENV_ONLY=true PCP_HOST=pcp.org ... -h [one host]'
cat <<'End-of-File' >$tmp.env
PM_OPTFLAG_ENV_ONLY=true
PCP_HOST=pcp.org
End-of-File
_getoptions -h non.org

echo '== PCP_HOST=pcp.org PCP_INTERVAL="2 milliseconds" PCP_SAMPLES=5 ...'
cat <<'End-of-File' >$tmp.env
PCP_HOST=pcp.org
PCP_INTERVAL="2 milliseconds"
PCP_SAMPLES=5
End-of-File
_getoptions

echo '== PCP_HOST_LIST=foo.com,bar.org ... [multiple hosts]'
cat <<'End-of-File' >$tmp.env
PCP_HOST_LIST=foo.com,bar.org
End-of-File
_getoptions

echo '== [one archive and all time options from environment]'
cat <<'End-of-File' >$tmp.env
PCP_ARCHIVE=archives/20041125.0
PCP_HOSTZONE=true
PCP_ALIGN_TIME="1 minute"
PCP_ORIGIN_TIME="@10:10:10"
PCP_START_TIME="@10:15"
PCP_FINISH_TIME="@10:30"
End-of-File
_getoptions

echo '== PCP_ARCHIVE_LIST=foo,bar,moo ... [multiple archives]'
cat <<'End-of-File' >$tmp.env
PCP_ARCHIVE_LIST=foo,bar,moo
End-of-File
_getoptions

echo '== PCP_ARCHIVE=foo ...'
cat <<'End-of-File' >$tmp.env
PCP_ARCHIVE=foo
End-of-File
_getoptions

echo '== PCP_TIMEZONE=UTC ...'
cat <<'End-of-File' >$tmp.env
PCP_TIMEZONE=UTC
End-of-File
_getoptions

echo '== PCP_HOSTZONE=true ...'
cat <<'End-of-File' >$tmp.env
PCP_HOSTZONE=true
End-of-File
_getoptions

echo '== PCP_GUIPORT=42 ...'
cat <<'End-of-File' >$tmp.env
PCP_GUIPORT=42
End-of-File
_getoptions
echo 'PCP_GUIPORT=/tmp/socket ...'
cat <<'End-of-File' >$tmp.env
PCP_GUIPORT=/tmp/socket
End-of-File
_getoptions

echo '== PCP_GUIMODE=true ...'
cat <<'End-of-File' >$tmp.env
PCP_GUIMODE=true
End-of-File
_getoptions

echo '== PM_OPTFLAG_MIXED=true PCP_HOST=acme.com PCP_ARCHIVE=log ...'
cat <<'End-of-File' >$tmp.env
PM_OPTFLAG_MIXED=true
PCP_HOST=acme.com
PCP_ARCHIVE=log
End-of-File
_getoptions

echo '== PM_OPTFLAG_MULTI=true ... -H $tmp.hosts [hosts file]'
echo foo.bar.com > $tmp.hosts
echo acme.com   >> $tmp.hosts
cat <<'End-of-File' >$tmp.env
PM_OPTFLAG_MULTI=true
End-of-File
_getoptions -H $tmp.hosts

mkaf archives/2004* >$tmp.myfolio
cat $tmp.myfolio >>$seq_full

cat <<End-of-File >$tmp.pmns
root {
    nothing	1:2:3
}
End-of-File

echo '== PCP_DEBUG=config ... -g --archive-folio $tmp.myfolio -n $tmp.pmns'
cat <<'End-of-File' >$tmp.env
PCP_DEBUG=config
End-of-File
_getoptions -g --archive-folio $tmp.myfolio -n $tmp.pmns
echo '== ... -Dconfig -L -K clear'
_getoptions -Dconfig -L -K clear
echo '== ... -Dconfig -L -K add,...'
_getoptions -Dconfig -L -K add,123,somepath/somepmda.so,some_init

echo '== PCP_DEBUG=config PCP_NAMESPACE=$tmp.pmns PCP_FOLIO=$tmp.myfolio PCP_GUIMODE= ...'
cat <<End-of-File >$tmp.env
PCP_DEBUG=config
PCP_NAMESPACE=$tmp.pmns
PCP_FOLIO=$tmp.myfolio
PCP_GUIMODE=
End-of-File
_getoptions
echo '== PCP_DEBUG=config PCP_UNIQNAMES=$tmp.pmns ...'
cat <<End-of-File >$tmp.env
PCP_DEBUG=config
PCP_UNIQNAMES=$tmp.pmns
End-of-File
_getoptions
echo '== PCP_DEBUG=config PCP_LOCALMODE= PCP_SPECLOCAL=clear ...'
cat <<'End-of-File' >$tmp.env
PCP_DEBUG=config
PCP_LOCALMODE=
PCP_SPECLOCAL=clear
End-of-File
_getoptions
echo '== PCP_DEBUG=config PCP_LOCALPMDA= PCP_SPECLOCAL=add,... ...'
cat <<'End-of-File' >$tmp.env
PCP_DEBUG=config
PCP_LOCALPMDA=
PCP_SPECLOCAL=add,123,somepath/somepmda.so,some_init
End-of-File
_getoptions

echo '== PCP_DEBUG=config PCP_ORIGIN="+13s" ... [old env var]'
cat <<'End-of-File' >$tmp.env
PCP_DEBUG=config
PCP_ORIGIN="+13s"
End-of-File
_getoptions -a archives/20041125.0

# success, all done
status=0
exit
