#!/bin/sh
# PCP QA Test No. 1100
# bash shell completion check (see 967 for zsh version)
#
# Copyright (c) 2017 Red Hat.
#

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

# get standard environment, filters and checks
. ./common.python
. ./common.config

_check_display
echo "DISPLAY=$DISPLAY" >>$here/$seq.full
echo "PCPQA_CLOSE_X_SERVER=$PCPQA_CLOSE_X_SERVER" >>$here/$seq.full

[ $PCP_PLATFORM = linux ] || _notrun "Test needs to run only on Linux"

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

export PCP_STDERR=$tmp.err

# these may not be available if needed modules missing
#
skip_pcp2elasticsearch=false
$python -c "import requests" >/dev/null 2>&1
[ $? -eq 0 ] || skip_pcp2elasticsearch=true
skip_pcp2xlsx=false
$python -c "import openpyxl" >/dev/null 2>&1
[ $? -eq 0 ] || skip_pcp2xlsx=true
skip_pmrep=false
$python -c "from collections import OrderedDict" >/dev/null 2>&1
if [ $? -ne 0 ]; then
    skip_pcp2elasticsearch=true
    skip_pcp2graphite=true
    skip_pcp2influxdb=true
    skip_pcp2json=true
    skip_pcp2spark=true
    skip_pcp2xlsx=true
    skip_pcp2xml=true
    skip_pcp2zabbix=true
    skip_pmrep=true
fi

# these may not be available if X11 display is missing
#
skip_pmchart=false
which xdpyinfo >/dev/null 2>&1
if [ $? -eq 0 ]
then
    xdpyinfo >/dev/null 2>&1
    [ $? -eq 0 ] || skip_pmchart=true
else
    skip_pmchart=true
fi

# these may not be installed on all platforms
which pmchart > /dev/null 2>&1
if [ $? -ne 0 ]
then
    skip_pmchart=true
fi
skip_pmdumptext=false
which pmdumptext > /dev/null 2>&1
if [ $? -ne 0 ]
then
    skip_pmdumptext=true
fi
skip_pmseries=false
which pmseries > /dev/null 2>&1
if [ $? -ne 0 ]
then
    skip_pmseries=true
fi

# sources
share_dir=`dirname $PCP_SHARE_DIR`
bash_comp=${share_dir}/bash-completion/completions/pcp

# functions
_check_completion_bash()
{
  cmds="$(grep '  pcp2.*)' $bash_comp | tr -d ')')"
  cmds="$cmds $(grep '  pm.*)' $bash_comp | tr -d ')')"
  for cmd in $cmds; do
    # Handle aliases
    str="  $cmd"
    [ "$cmd" = "pmlogdump|pmdumplog" ] && cmd=pmdumplog && str="  pmlogdump|pmdumplog"
    which $cmd > /dev/null 2>&1 || continue
    $skip_pcp2elasticsearch && [ "$cmd" = pcp2elasticsearch ] && continue
    $skip_pcp2graphite && [ "$cmd" = pcp2graphite ] && continue
    $skip_pcp2influxdb && [ "$cmd" = pcp2influxdb ] && continue
    $skip_pcp2json && [ "$cmd" = pcp2json ] && continue
    $skip_pcp2spark && [ "$cmd" = pcp2spark ] && continue
    $skip_pcp2xlsx && [ "$cmd" = pcp2xlsx ] && continue
    $skip_pcp2xml && [ "$cmd" = pcp2xml ] && continue
    $skip_pcp2zabbix && [ "$cmd" = pcp2zabbix ] && continue
    $skip_pmchart && [ "$cmd" = pmchart ] && continue
    $skip_pmdumptext && [ "$cmd" = pmdumptext ] && continue
    $skip_pmrep && [ "$cmd" = pmrep ] && continue
    $skip_pmseries && [ "$cmd" = pmseries ] && continue
    comps=$(grep -A 1 "$str)" $bash_comp | tail -n 1 | sed -e 's,all_args=",,' -e 's,",,')
    # Need $tmp.err and PCP_STDERR for pmchart ...
    #
    rm -f $tmp.err
    touch $tmp.err
    echo "=== bash $cmd ===" >>$here/$seq.full
    echo "comps=$comps" >>$here/$seq.full
    $cmd --help >$tmp.out 2>&1
    echo "--- stdout ---" >>$here/$seq.full
    cat $tmp.out >>$here/$seq.full
    echo "--- stderr ---" >>$here/$seq.full
    cat $tmp.err >>$here/$seq.full
    opts=$(cat $tmp.out $tmp.err | grep -Eo -- ' -.' | tr -d '-' | tr -d '?' | sort | uniq)
    echo "opts=$opts" >>$here/$seq.full

    for opt in $opts; do
      echo $comps | grep $opt > /dev/null 2>&1
      if [ $? -ne 0 ]; then
        echo "$opt missing for $cmd bash completions"
      fi
    done

    for comp in $(echo $comps | grep -o .); do
      echo $opts | grep $comp > /dev/null 2>&1
      if [ $? -ne 0 ]; then
        echo "$comp looks extraneous for $cmd bash completions"
      fi
    done

  done
}

# real QA test starts here
_check_completion_bash

# success, all done
echo "== done"
status=0
exit
