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

SHELL = /bin/sh

# Copyright © 2023–2024 Lady [@ Lady’s 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/>.

# CAUDEX := .🪾📰
# LESML := .💄📝
SHUSHE := .⛩📰
YSEME := .👥📤

THISDIR := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
SRCDIR := site
INCLUDEDIR := data site/data
BUILDDIR := build
BUILDTARGET := .grass
DESTDIR := public

# SHUSHEOPTS := SRCDIR='$(SRCDIR)' INCLUDEDIR='$(INCLUDEDIR)' DESTDIR='$(DESTDIR)' EXTRAMAGIC='$(wildcard $(LESML)/magic/*) $(wildcard magic/*)' PARSERS='$(LESML)/parser.xslt $(SHUSHE)/parsers/record-jar.xslt $(SHUSHE)/parsers/tsv.xslt $(wildcard parsers/*.xslt)' EXTRATRANSFORMS='$(CAUDEX)/transform.xslt $(wildcard transforms/*.xslt)' EXTRAFINDRULES='-not -name '"'"'*.license'"'"''
SHUSHEOPTS := SRCDIR='$(SRCDIR)' INCLUDEDIR='$(INCLUDEDIR)' DESTDIR='$(DESTDIR)' EXTRAMAGIC='$(wildcard magic/*)' PARSERS='$(SHUSHE)/parsers/record-jar.xslt $(SHUSHE)/parsers/tsv.xslt $(wildcard parsers/*.xslt)' EXTRATRANSFORMS='$(wildcard transforms/*.xslt)' EXTRAFINDRULES='-not -name '"'"'*.license'"'"''

YSEMEOPTS := BUILDTARGET='$(BUILDTARGET)' DESTDIR='$(DESTDIR)' SERVERPATH='lady/langdev'

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

# install : $(SHUSHE)/GNUmakefile $(LESML)/parser.xslt $(CAUDEX)/transform.xslt build
install : $(SHUSHE)/GNUmakefile build
	@for publicfile in $(patsubst %,"%",$(wildcard $(DESTDIR)/*)); do if git check-ignore -q "$$publicfile"; then rm -rf "$$publicfile"; fi; done
	@$(MAKE) -f '$<' install $(SHUSHEOPTS)
	@touch '$(BUILDTARGET)'

build : $(SHUSHE)/GNUmakefile
	@$(MAKE) -f '$<' $(SHUSHEOPTS)

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

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

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

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

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

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

.PHONY : FORCE build clean dry-sync gone 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 : ;

# ━ § Submodule targets ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ifneq ($(filter $(abspath $(THISDIR))/%,$(abspath $(LESML))),)
$(LESML)/parser.xslt : %/GNUmakefile : FORCE
	git submodule update --init '$*'
endif

ifneq ($(filter $(abspath $(THISDIR))/%,$(abspath $(CAUDEX))),)
$(CAUDEX)/transform.xslt : %/GNUmakefile : FORCE
	git submodule update --init '$*'
endif

ifneq ($(filter $(abspath $(THISDIR))/%,$(abspath $(SHUSHE))),)
$(SHUSHE)/GNUmakefile : %/GNUmakefile : FORCE
	git submodule update --init '$*'
endif

ifneq ($(filter $(abspath $(THISDIR))/%,$(abspath $(YSEME))),)
$(YSEME)/GNUmakefile : %/GNUmakefile : FORCE
	git submodule update --init '$*'
endif