#
# Part of the ht://Dig package   <http://www.htdig.org/>
# Copyright (c) 1999, 2000 The ht://Dig Group
# For copyright details, see the file COPYING in your distribution
# or the GNU General Public License version 2 or later
# <http://www.gnu.org/copyleft/gpl.html>
#
include $(top_srcdir)/Makefile.config

#
# All test programs use the index description from the
# mifluz.conf file (MIFLUZ_CONFIG set in test_functions.in). 
# Some have hard wired additions but these
# additions are never redundant with mifluz.conf content. Some
# programs have options that allow command line overriding of
# index parameters (page size, compression and such). 
#
# To run individual tests use (for instance):
# make TESTS=t_wordkey check
#
# To turn on verbosity use (for instance):
# VERBOSE=-vv make TESTS=t_wordkey check
#
# Run individual test without using make 
# MAKE=make srcdir=. VERBOSE=-vv t_wordkey
#
# To purify objects use:
# ( cd ../test ; rm -f word ; make CXXLD='purify g++' word )
#
# To generate benchmarks in benchmark/... make sure
# you have rrdtool installed and use 
# make MONITOR=' -m' dobench
#

TESTS = t_wordkey t_wordlist t_wordskip t_wordbitstream \
	t_search t_htdb t_rdonly t_trunc \
	t_htdig t_htsearch t_htmerge t_htnet t_htdig_local

TESTS_ENVIRONMENT = $(top_srcdir)/test/test_prepare
AM_MAKEFLAGS = MAKE="$(MAKE)"

EXTRA_DIST = test_functions.in test_prepare $(TESTS) \
	skiptest_db.txt search.txt mifluz.conf mifluz-search.conf \
	benchmark-report benchmark t_htdb.dump \
	htdocs conf url.parents url.children

LOCAL_DEFINES = -I$(top_builddir)/db -I$(top_srcdir)/db $(PROFILING)

check_PROGRAMS = word dbbench txt2mifluz search testnet document url

dbbench_SOURCES = dbbench.cc
dbbench_DEPENDENCIES = $(HTLIBS)
dbbench_LDFLAGS = $(PROFILING)
dbbench_LDADD = $(HTLIBS)

word_SOURCES = word.cc
word_DEPENDENCIES = $(HTLIBS)
word_LDFLAGS = $(PROFILING)
word_LDADD = $(HTLIBS)

txt2mifluz_SOURCES = txt2mifluz.cc
txt2mifluz_DEPENDENCIES = $(HTLIBS)
txt2mifluz_LDFLAGS = $(PROFILING)
txt2mifluz_LDADD = $(HTLIBS)

search_SOURCES = search.cc
search_DEPENDENCIES = $(HTLIBS)
search_LDFLAGS = $(PROFILING)
search_LDADD = $(HTLIBS)

testnet_SOURCES = testnet.cc
testnet_DEPENDENCIES = $(HTLIBS)
testnet_LDFLAGS = $(PROFILING)
testnet_LDADD = $(HTLIBS)

document_SOURCES = document.cc
document_DEPENDENCIES = $(HTLIBS)
document_LDFLAGS = $(PROFILING)
document_LDADD = $(HTLIBS)

url_SOURCES = url.cc
url_DEPENDENCIES = $(HTLIBS)
url_LDFLAGS = $(PROFILING)
url_LDADD = $(HTLIBS)

clean-local:
	rm -fr gmon.out test test_weakcmpr __db*
	rm -f  tmpfile t_htdb.d? monitor.out 
	$(MAKE) -C conf clean

distclean-local:
	rm -fr words.all words.uniq

#
# The benchmark directory contains the result of some benchmarks in HTML
# form generated by benchmark-report, if MONITOR='-m' is specified.
#
dobench: dbbench
	$(MAKE) BASE="$(BASE)" CACHESIZE="$(CACHESIZE)" PAGESIZE="$(PAGESIZE)" LOOP="$(LOOP)" NWORDS="$(NWORDS)" CMPR='-z' REPORT='Nz' MONITOR="$(MONITOR)" bench
	$(MAKE) BASE="$(BASE)" CACHESIZE="$(CACHESIZE)" PAGESIZE="$(PAGESIZE)" LOOP="$(LOOP)" NWORDS="$(NWORDS)" CMPR='' REPORT='N' MONITOR="$(MONITOR)" bench
	$(MAKE) BASE="$(BASE)" CACHESIZE="$(CACHESIZE)" PAGESIZE="$(PAGESIZE)" LOOP="$(LOOP)" NWORDS="$(NWORDS)" CMPR='-W -z' REPORT='Wz' MONITOR="$(MONITOR)" bench
	$(MAKE) BASE="$(BASE)" CACHESIZE="$(CACHESIZE)" PAGESIZE="$(PAGESIZE)" LOOP="$(LOOP)" NWORDS="$(NWORDS)" CMPR='-W' REPORT='W' MONITOR="$(MONITOR)" bench

BASE = test
CACHESIZE = -C `expr 64 \* 1024 \* 1024`
PAGESIZE = -S 8192
CMPR = -z
WORDS = -w words.all
LOOP = -l 3
NWORDS = 
#MONITOR = -m
MONITOR = 

bench:
	rm -f $(BASE) $(BASE)_weakcmpr __db* monitor.out bench.out
	( \
	  MIFLUZ_CONFIG=$(top_srcdir)/test/mifluz.conf $(TIMEV) $(top_builddir)/test/dbbench $(CACHESIZE) $(PAGESIZE) $(CMPR) $(WORDS) $(LOOP) -B $(BASE) $(NWORDS) $(MONITOR) ; \
	  ls -l $(BASE) ; \
	  if [ -f $(BASE)_weakcmpr ] ; then MIFLUZ_CONFIG=$(top_srcdir)/test/mifluz.conf $(top_builddir)/htdb/htdump -p $(BASE)_weakcmpr ; fi ; \
	  MIFLUZ_CONFIG=$(top_srcdir)/test/mifluz.conf $(top_builddir)/htdb/htstat $(CMPR) -d $(BASE) ; \
	) 2>&1 | tee bench.out
	if [ "$(RRDTOOL)" -a "X$(MONITOR)" != "X" ] ; then \
		if [ "$(REPORT)" ] ; \
		then \
			output="--output $(REPORT)" ; \
		fi ; \
		$(PERL) benchmark-report $$output --comment="`cat bench.out`" ; \
		rm monitor.out ; \
	fi

#
# Generate list of words from info and man files.
#
words:
	if [ -d /usr/info ] ; then root=/usr ; else root=/usr/share ; fi ; \
	find $$root/info -name '*.gz' -print | xargs zcat | perl -n -e 'print join("\n", map { lc } grep(length() > 2 && length() < 32, m/[a-z]+/ig)) . "\n"' | grep -v '^$$' > words.all ; \
	find $$root/man -type f -name '*.gz' -print | xargs zcat | perl -n -e 'print join("\n", map { lc } grep(length() > 2 && length() < 32, m/[a-z]+/ig)) . "\n"' | grep -v '^$$' >> words.all 
	sort -u < words.all > words.uniq
