# SPDX-FileCopyrightText: 2023, 2024, 2025 Lady <https://www.ladys.computer/about/#lady>
# SPDX-License-Identifier: MPL-2.0

SHELL = /bin/sh

# Copyright © 2023–2025 Lady [@ Ladys Computer].
#
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the M·P·L was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0/>.

CP := cp
FIND := find
GIT := git
MKDIR := mkdir
PRINTF := printf
RM := rm
SED := sed
TEST := test
TOUCH := touch
XARGS := xargs

DATADIR := data
SRCDIR := site
BUILDDIR := build
BUILDTARGET := .grass
DESTDIR := public

THISDIR := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))

SHUSHE := .⛩📰
MAKEOPTS := CP='$(CP)' FIND='$(FIND)' GIT='$(GIT)' MKDIR='$(MKDIR)' PRINTF='$(PRINTF)' RM='$(RM)' SED='$(SED)' TEST='$(TEST)' TOUCH='$(TOUCH)' XARGS='$(XARGS)'
SHUSHEOPTS := $(MAKEOPTS) SRCDIR='$(SRCDIR)' DATADIR='$(DATADIR)' BUILDDIR='$(BUILDDIR)' DESTDIR='$(BUILDDIR)/public' EXTRAPARSERS='$(wildcard $(THISDIR)/parsers/*.xslt)' EXTRAPARSERLIBS='$(DATADIR)/ontologies/¦' EXTRATRANSFORMS='$(wildcard $(THISDIR)/transforms/*.xslt)'

YSEME := .👥📤
YSEMEOPTS := $(MAKEOPTS) BUILDTARGET='$(BUILDTARGET)' DESTDIR='$(DESTDIR)' SERVERPATH='lady/vocab'

xmls := $(wildcard $(DATADIR)/*/*)

# ━ § Call out to ⛩📰 书社 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

install : $(SHUSHE)/GNUmakefile build
	@for publicfile in $(patsubst %,"%",$(shell $(FIND) '$(DESTDIR)'/*)); do if $(GIT) check-ignore -q "$$publicfile"; then $(RM) -R -f "$$publicfile"; fi; done
	@for dir in $$($(FIND) '$(BUILDDIR)/public' -type d | $(SED) 's:$(BUILDDIR)/public/::'); do if $(TEST) '!' -d "$(DESTDIR)/$$dir"; then $(MKDIR) -p "$(DESTDIR)/$$dir"; fi; done
	@for file in $$($(FIND) '$(BUILDDIR)/public' -type f | $(SED) 's:$(BUILDDIR)/public/::'); do $(PRINTF) '%s\n' "$$file" | $(SED) 's:/_\(.*\)_\(\..*\)$$:/(\1)\2:g;s/¦/:/g' | $(XARGS) $(PRINTF) 'Copying <%s>…\n' >&2; $(PRINTF) '%s\n' "$$file" | $(SED) 's:^:$(DESTDIR)/:;s:/_\(.*\)_\(\..*\)$$:/(\1)\2:g;s/¦/:/g' | $(XARGS) $(CP) "$(BUILDDIR)/public/$$file"; done
	$(TOUCH) '$(BUILDTARGET)'

build : $(SHUSHE)/GNUmakefile
	@$(RM) -R -f '$(BUILDDIR)/public'
	@$(MAKE) -f '$<' install $(SHUSHEOPTS)

clean list listout : $(SHUSHE)/GNUmakefile
	@$(MAKE) -f '$<' $@ $(SHUSHEOPTS)

$(BUILDDIR)/% : $(SHUSHE)/GNUmakefile FORCE
	@$(MAKE) -f '$<' $@ $(SHUSHEOPTS)

# ━ § Call out to 👥📤 Yseme ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

dry-sync sync : $(YSEME)/GNUmakefile
	@$(MAKE) -f '$<' $@ $(YSEMEOPTS)

# ━ § Build xmllint ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

# This is temporarily required as DTD resolving requires a very new
# version of `xmllint´ when a filename contains non‐Ascii characters.
# Once `libxml2` ≥ 2.13.2 is more widely available, this subrepo and
# build step can probably be dropped.

.libxml2/xmllint : .libxml2/autogen.sh
	cd .libxml2 && ./autogen.sh
	$(MAKE) -C .libxml2

# ━ § Linting ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

lint : $(foreach xml,$(xmls),lint][$(xml)) ;

$(foreach xml,$(xmls),lint][$(xml)) : lint][% : .libxml2/xmllint
	@.libxml2/xmllint --valid --noout '$*'

# ━ § Special targets ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

.IGNORE : $(foreach xml,$(xmls),lint][$(xml)) ;

.PHONY : FORCE build clean dry-sync gone lint $(foreach xml,$(xmls),lint][$(xml)) list prebuild sync uninstall ;

gone : clean uninstall ;

uninstall :
	@for publicfile in $(patsubst %,"%",$(wildcard $(DESTDIR)/*)); do if $(GIT) check-ignore -q "$$publicfile"; then $(RM) -rf "$$publicfile"; fi; done

FORCE : ;

# ━ § Make·file targets ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

$(SHUSHE)/GNUmakefile $(YSEME)/GNUmakefile : %/GNUmakefile : FORCE
	$(GIT) submodule update --init '$*'

.libxml2/autogen.sh : %/autogen.sh : FORCE
	$(GIT) submodule update --init '$*'