#!/bin/sh
# PCP QA Test No. 1871
# Exercise pmsearch functionality using local SQLite FTS5 index.
# Tests the copy-and-extend strategy: build a base index from help
# files, copy it, then append instance data from an archive.
#
# Copyright (c) 2020,2026 Red Hat.  All Rights Reserved.
#

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

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

which pmsearch >/dev/null 2>&1 || \
    _notrun "pmsearch not installed"
which newhelp >/dev/null 2>&1 || \
    _notrun "newhelp not installed"
newhelp -\? 2>&1 | grep -q '\-S, --search' || \
    _notrun "newhelp not built with SQLite search index support"
[ -x $PCP_BINADM_DIR/pmsearch_daily ] || \
    _notrun "pmsearch_daily not installed"

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

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

_filter_info()
{
    sed \
	-e 's/: [0-9][0-9]*/: INTEGER/g' \
    #end
}

_filter_extras()
{
    sed \
	-e 's/\(Score:\) [0-9][0-9]*\.[0-9][0-9]*/\1 N.NN/g' \
	-e 's/\([0-9][0-9]* hit in\) [0-9][0-9]*\.[0-9][0-9]* seconds/\1 N.NNN seconds/g' \
	-e 's/\([0-9][0-9]* hits in\) [0-9][0-9]*\.[0-9][0-9]* seconds/\1 N.NNN seconds/g' \
    #end
}

_filter_docid()
{
    sed \
	-e 's/^ID: [0-9][0-9]*:/ID: N:/g' \
    #end
}

# real QA test starts here

archive="$here/archives/sample-labels"
[ -f "$archive.0" ] || _notrun "sample-labels archive not found"
help=$PCP_PMDAS_DIR/sample/help
[ -f "$help" ] || _notrun "sample PMDA help file not found"

# Mirror the real copy-and-extend strategy:
#   1. Build base index from PMDA help files (like package install)
#   2. Copy base to nightly location (like pmsearch_daily.sh does)
#   3. Append instance data from archive to the nightly copy

# --- Base index (like $PCP_SHARE_DIR/lib/pmsearch.index) ---
mkdir -p $tmp.share/lib
newhelp -S -o $tmp.share/lib/pmsearch.index $help 2>&1 \
    | sed -e "s,$tmp,TMP,g"
echo "Base index built"

# --- Nightly index (like $PCP_VAR_DIR/lib/pmsearch.index) ---
# Use pmsearch_daily to copy base and append instance data from archive
mkdir -p $tmp.var/lib
export PCP_VAR_DIR=$tmp.var
export PCP_SHARE_DIR=$tmp.share
$PCP_BINADM_DIR/pmsearch_daily -a $archive
echo "Nightly index built (base + instances)"
echo

# PCP_VAR_DIR and PCP_SHARE_DIR already exported above for pmsearch_daily

echo "=== Empty search ==="
pmsearch -C xyznonexistent

search_terms="99 random interesting result"
for term in $search_terms
do
    echo "=== Search on $term ==="
    pmsearch -C $term
done

echo "=== Search engine info ==="
pmsearch -C -i | _filter_info
echo

echo "=== Extra reporting - timing, score, hits, doc IDs ==="
pmsearch -dStT 99 | _filter_extras | _filter_docid
echo

echo "=== Suggestion search 1 ==="
pmsearch -s sample | _filter_extras

echo "=== Suggestion search 2 ==="
pmsearch -s blue | _filter_extras

echo "=== Indom search ==="
pmsearch -n 29.3 | _filter_extras
echo

# A dotted metric name query must be treated the same as the equivalent
# space-separated terms (dots are term separators, not FTS5 syntax).
echo "=== Dotted query equivalence ==="
pmsearch -C sample.long >$tmp.dotted 2>&1
pmsearch -C sample long >$tmp.spaced 2>&1
if diff $tmp.dotted $tmp.spaced >/dev/null
then
    echo "sample.long same as 'sample long': yes"
else
    echo "sample.long same as 'sample long': no"
    diff $tmp.dotted $tmp.spaced
fi
echo

# Other punctuation that is not valid in FTS5 query syntax (commas, slashes,
# parentheses, ...) must also be treated as term separators rather than
# causing a parse error, e.g. when searching help text.
echo "=== Punctuated query equivalence ==="
pmsearch -C "sample, (long)/bin" >$tmp.punct 2>&1
pmsearch -C sample long bin >$tmp.spaced 2>&1
if diff $tmp.punct $tmp.spaced >/dev/null
then
    echo "'sample, (long)/bin' same as 'sample long bin': yes"
else
    echo "'sample, (long)/bin' same as 'sample long bin': no"
    diff $tmp.punct $tmp.spaced
fi
echo

# A query with no searchable tokens (only dots, or empty) must report no
# results rather than failing with an FTS5 parse error.
echo "=== Token-less queries ==="
echo "query '.':"; pmsearch -C .
echo "query '...':"; pmsearch -C ...
echo "suggest '.':"; pmsearch -s .


# success, all done
status=0
exit
