#  This file is part of Siworin. Copyright © 2021 by Ruud Harmsen.
#
#  Siworin is a simple word indexer and local search engine. See
#  https://rudhar.com/sfreview/siworin/#siw04 and
#  https://rudhar.com/sfreview/siworin/toolsrc/siworin .
#
#  Siworin is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published
#  by the Free Software Foundation, either version 3 of the License,
#  or (at your option) any later version.
#
#  Siworin is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Siworin.  If not, see <https://www.gnu.org/licenses/>.

CC=cc
#CC=musl-gcc

#CCOPT= -Wall -Wpedantic -g -DDEBUG -O0
#CCOPT= -Wall -Wpedantic -g -DDEBUG -DDEBUG2 -O0
CCOPT= -Wall -Wpedantic -Wextra

HTMFILS=/var/www/html
TOOLSRC=/var/www/toolsrc/
TOOLBIN=/var/www/toolbin/
CGISRC=/var/www/cgi-src/
CGIBIN=/var/www/cgi-bin/
FPREFIX=siworin
SIWOSRC=$(TOOLSRC)$(FPREFIX)/
SIWOBIN=$(TOOLBIN)$(FPREFIX)/
SIWCBIN=$(CGIBIN)$(FPREFIX)/
TMP=/tmp/

all: progs files

progs: p w m scgi gi

files: paths pathoffs words

clear: clearprogs clearfiles

p: $(SIWOBIN)p
w: $(SIWOBIN)w
m: $(SIWOBIN)m
scgi: $(CGIBIN)s.cgi
s: $(SIWOBIN)s
x: $(SIWOBIN)x
gi: $(SIWOBIN)siworin-genindex

$(SIWOBIN)p: $(SIWOSRC)siworin-pathoffs.c \
		$(SIWOSRC)siworin-files.c $(SIWOSRC)siworin-config.c
	$(CC) $(CCOPT) $(SIWOSRC)siworin-pathoffs.c $(SIWOSRC)siworin-files.c \
		$(SIWOSRC)siworin-config.c -o $(SIWOBIN)p

$(SIWOBIN)w: $(SIWOSRC)siworin-wordsep.c $(SIWOSRC)siworin-config.c
	$(CC) $(CCOPT) $(SIWOSRC)siworin-wordsep.c $(SIWOSRC)siworin-config.c \
		-o $(SIWOBIN)w

$(SIWOBIN)m: $(SIWOSRC)siworin-makelst.c \
		$(SIWOSRC)siworin-files.c $(SIWOSRC)siworin-config.c
	$(CC) $(CCOPT) $(SIWOSRC)siworin-makelst.c $(SIWOSRC)siworin-files.c \
		$(SIWOSRC)siworin-config.c -o $(SIWOBIN)m

$(CGIBIN)s.cgi: $(SIWOSRC)siworin-cgi.c \
		$(CGISRC)cgitools/cgitools.c $(SIWOSRC)siworin-srchind.c \
		$(SIWOSRC)siworin-match.c  $(SIWOSRC)siworin-ranking.c \
		$(SIWOSRC)siworin-displ.c \
		$(SIWOSRC)siworin-files.c $(SIWOSRC)siworin-config.c
	$(CC) $(CCOPT) -I $(CGISRC)cgitools $(SIWOSRC)siworin-cgi.c \
		$(CGISRC)cgitools/cgitools.c $(SIWOSRC)siworin-srchind.c \
		$(SIWOSRC)siworin-files.c $(SIWOSRC)siworin-config.c \
		$(SIWOSRC)siworin-match.c $(SIWOSRC)siworin-ranking.c \
		$(SIWOSRC)siworin-displ.c -o $(CGIBIN)s.cgi

$(SIWOBIN)s: $(SIWOSRC)siworin-srchind.c \
		$(SIWOSRC)siworin-match.c  $(SIWOSRC)siworin-ranking.c \
		$(SIWOSRC)siworin-displ.c \
		$(SIWOSRC)siworin-files.c $(SIWOSRC)siworin-config.c
	$(CC) $(CCOPT) -DMAIN $(SIWOSRC)siworin-srchind.c \
		$(SIWOSRC)siworin-files.c $(SIWOSRC)siworin-config.c \
		$(SIWOSRC)siworin-match.c  $(SIWOSRC)siworin-ranking.c \
		$(SIWOSRC)siworin-displ.c -o $(SIWOBIN)s

$(SIWOBIN)x: $(SIWOSRC)siworin-match.c  $(SIWOSRC)siworin-ranking.c \
		$(SIWOSRC)siworin-displ.c \
		$(SIWOSRC)siworin-files.c $(SIWOSRC)siworin-config.c
	$(CC) $(CCOPT) -DMAINMATCH $(SIWOSRC)siworin-match.c  $(SIWOSRC)siworin-ranking.c \
		$(SIWOSRC)siworin-displ.c $(SIWOSRC)siworin-files.c \
		$(SIWOSRC)siworin-config.c -o $(SIWOBIN)x

$(SIWOBIN)siworin-genindex: $(SIWOSRC)siworin-genindex.c $(CGISRC)cgitools/cgitools.c
	$(CC) $(CCOPT) -I $(CGISRC)cgitools $(SIWOSRC)siworin-genindex.c \
	$(CGISRC)cgitools/cgitools.c -o $(SIWOBIN)siworin-genindex

paths: $(SIWCBIN)$(FPREFIX).paths
pathoffs: $(SIWCBIN)$(FPREFIX).pathoffs
words: $(SIWCBIN)$(FPREFIX).words

refresh:
	-find $(HTMFILS) -type f ! -name 'index-?.html' ! -name index.html ! -name whatsnew.htm \
		-newer $(SIWCBIN)$(FPREFIX).paths \
		-exec rm -f $(SIWCBIN)$(FPREFIX).paths \; -quit

$(SIWCBIN)$(FPREFIX).paths: $(SIWOBIN)w
	find $(HTMFILS) -type f | \
		grep -vi -f $(SIWOSRC)$(FPREFIX).stoplist | \
		(LC_ALL=C sort) > $(SIWCBIN)$(FPREFIX).paths

$(SIWCBIN)$(FPREFIX).pathoffs: $(SIWCBIN)$(FPREFIX).paths
	$(SIWOBIN)p

$(SIWCBIN)$(FPREFIX).words: $(SIWCBIN)$(FPREFIX).paths
	$(SIWOBIN)w < $(SIWCBIN)$(FPREFIX).paths | (LC_ALL=C sort) | $(SIWOBIN)m
	if test -f $(SIWCBIN)$(FPREFIX).uniqwrd; \
		then mv $(SIWCBIN)$(FPREFIX).uniqwrd $(SIWCBIN)$(FPREFIX).uniqwas; \
	fi
	sed 's/!.*$$//' $(SIWCBIN)$(FPREFIX).words > $(SIWCBIN)$(FPREFIX).uniqwrd
	(cd $(HTMFILS)/index; rm index-?.html; $(SIWOBIN)siworin-genindex $(SIWCBIN)$(FPREFIX).uniqwrd)
	if test -f $(SIWCBIN)$(FPREFIX).uniqwas; then \
		diff $(SIWCBIN)$(FPREFIX).uniqwas $(SIWCBIN)$(FPREFIX).uniqwrd > $(SIWCBIN)$(FPREFIX).uniqdif; \
		less $(SIWCBIN)$(FPREFIX).uniqdif; \
	fi

clearprogs:
	# Remove programs
	rm -f $(SIWOBIN)p $(SIWOBIN)w $(SIWOBIN)m $(CGIBIN)s.cgi $(SIWOBIN)s $(SIWOBIN)x

clearfiles:
	# Remove paths file and its index file
	rm -f $(SIWCBIN)$(FPREFIX).paths $(SIWCBIN)$(FPREFIX).pathoffs
	# Remove words file and its index file, and the stripped words file
	rm -f $(SIWCBIN)$(FPREFIX).words $(SIWCBIN)$(FPREFIX).wordoffs

# Commands for performance testing. See:
# https://rudhar.com/sfreview/siworin/siworin06.htm and
# https://rudhar.com/sfreview/siworin/siworin06a.htm
perftest-step-1-6:
	find $(HTMFILS) -type f | \
		grep -vi -f $(SIWOSRC)$(FPREFIX).stoplist | \
		(LC_ALL=C sort) > $(SIWCBIN)$(FPREFIX).paths
	$(SIWOBIN)p

# Example of testing this and other steps, in a shell:
# time make perftest-step-2-4-5
perftest-step-2-4-5:
	$(SIWOBIN)w < $(SIWCBIN)$(FPREFIX).paths | (LC_ALL=C sort) | $(SIWOBIN)m

perftest-step-2:
	$(SIWOBIN)w < $(SIWCBIN)$(FPREFIX).paths > $(TMP)siworin-w-output

perftest-step-4:
	(LC_ALL=C sort) < $(TMP)siworin-w-output > $(TMP)siworin-sort-output

perftest-step-5:
	$(SIWOBIN)m < $(TMP)siworin-sort-output

perftest-wc:
	$(SIWOBIN)w < $(SIWCBIN)$(FPREFIX).paths | wc

