# The MIT License (MIT)

# Copyright (c) 2020 наб <nabijaczleweli@nabijaczleweli.xyz>

# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


include configMakefile


LDDLLS := efivar efiboot crypto $(OS_LD_LIBS)
LDAR := $(LNCXXAR) $(foreach dll,$(LDDLLS),-l$(dll)) $(shell { command -v pkg-config > /dev/null && pkg-config --libs fmt; } || echo -lfmt)
INCAR := -isystemext $(shell for p in catch2 fmt; do pkg-config --cflags $p; done 2>/dev/null)
VERAR := $(foreach l,KLAPKI,-D$(l)_VERSION='$($(l)_VERSION)')
SOURCES := $(sort $(wildcard $(SRCDIR)*.cpp $(SRCDIR)**/*.cpp $(SRCDIR)**/**/*.cpp $(SRCDIR)**/**/**/*.cpp))
TEST_SOURCES := $(sort $(wildcard $(TSTDIR)*.cpp $(TSTDIR)**/*.cpp $(TSTDIR)**/**/*.cpp $(TSTDIR)**/**/**/*.cpp))
MANPAGES := $(subst man/,,$(wildcard man/*.8))


.PHONY : all clean build build-test manpages htmlpages
.SECONDARY:


all : build manpages htmlpages build-test test

test: build-test
	$(OUTDIR)klapki-test

clean :
	rm -rf $(OUTDIR)

build : $(OUTDIR)klapki
build-test : $(OUTDIR)klapki-test
manpages : $(patsubst %,$(OUTDIR)man/%,$(MANPAGES))
htmlpages : $(patsubst %,$(OUTDIR)man/%.html,$(MANPAGES)) $(OUTDIR)man/style.css


$(OUTDIR)klapki : $(subst $(SRCDIR),$(OBJDIR),$(subst .cpp,$(OBJ),$(SOURCES)))
	$(CXX) $(CXXAR) -o$@ $^ $(LDAR)
	$(STRIP) $(STRIPAR) $@

$(OUTDIR)klapki-test : $(subst $(TSTDIR),$(BLDDIR)test/,$(subst .cpp,$(OBJ),$(TEST_SOURCES))) $(subst $(SRCDIR),$(OBJDIR),$(subst .cpp,$(OBJ),$(filter-out $(SRCDIR)main.cpp,$(SOURCES))))
	$(CXX) $(CXXAR) -o$@ $^ $(LDAR)

# The d-v-o-s string starts at "BSD" (hence the "BSD General Commands Manual" default); we're not BSD, so hide it
# Can't put it at the very top, since man(1) only loads mdoc *after* the first mdoc macro (.Dd in our case)
$(OUTDIR)man/% : man/%
	@mkdir -p $(dir $@)
	awk '$$0 == ".Dd" {$$2 = "$(KLAPKI_DATE)"}  $$1 == ".Dt" { print ".ds doc-volume-operating-system" }  $$0 == ".Os" {$$2 = "klapki"; $$3 = $(KLAPKI_VERSION)}  {print}' $< > $@
	! $(MANDOC) -Tlint $@ 2>&1 | grep -vF -e 'mandoc: outdated mandoc.db' -e 'STYLE: referenced manual not found' -e 'STYLE: input text line longer than 80 bytes:' -e 'STYLE: operating system explicitly specified: Os klapki' -e 'STYLE: no blank before trailing delimiter: Pa /sys/\:firmware/\:efi/\:efivars/\:'

# https://manpages.debian.org/bullseye/SHA1.3ssl
$(OUTDIR)man/%.html : $(OUTDIR)man/%
	@mkdir -p $(dir $@)
	$(MANDOC) -Thtml -Ostyle="style.css",man="https://manpages.debian.org/bullseye/%N.%S" $< | \
		awk '/^<h1/ {in_syn = $$0 ~ /id="SYNOPSIS"/}  /^<br/ {if(in_syn) {print >"/dev/stderr"; next}}  {print}' | \
		sed -Ee 's/ title=".."//g' -e 's/<a class="permalink" href="#([^"]*)"><span class="No" id="([^"]*)">/<a><span class="No">/g' > $@

$(OUTDIR)man/style.css : man/style.css
	@mkdir -p $(dir $@)
	cp $^ $@


$(OBJDIR)%$(OBJ) : $(SRCDIR)%.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXAR) $(INCAR) $(VERAR) -c -o$@ $^

$(BLDDIR)test/%$(OBJ) : $(TSTDIR)%.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXAR) $(INCAR) -I$(SRCDIR) $(VERAR) -c -o$@ $^
