TMPDIR			:= ./_tmp
CSRD			:= ./srd/Cypher-System-Reference-Document-2025-05-05-fixed.docx
MD_CSRD			:= $(TMPDIR)/csrd.md
CCSRD			:= $(TMPDIR)/ccsrd.md
WEBDIR			:= ./incorporealcms-instance
PAGESDIR		:= $(TMPDIR)/$(WEBDIR)/pages

BASE_PATCH_DIR		:= ./patches/base
BASE_PATCHES		:= $(wildcard $(BASE_PATCH_DIR)/*.patch)
BASE_PATCHES_OUT	:= $(subst $(BASE_PATCH_DIR),$(TMPDIR),$(BASE_PATCHES))

WEB_PATCH_DIR		:= ./patches/web
WEB_PATCHES		:= $(wildcard $(WEB_PATCH_DIR)/*.patch)
WEB_PATCHES_OUT		:= $(subst $(WEB_PATCH_DIR),$(TMPDIR),$(WEB_PATCHES))

# DEFAULT BUILD

all: base

# CCSRD VERSIONS

base: $(CCSRD) $(BASE_PATCHES_OUT)
web: base $(WEB_PATCHES_OUT)

# CLEANUPS

clean:
	rm -rf $(TMPDIR)/ccsrd*
	rm -rf $(TMPDIR)/*.patch

distclean:
	rm -rf $(TMPDIR)

# PATCHERS

$(TMPDIR)/%.patch: $(BASE_PATCH_DIR)/%.patch
	$(info >>> BASE '$<')
	@patch $(CCSRD) $< --quiet
	@touch $@

$(TMPDIR)/%.patch: $(WEB_PATCH_DIR)/%.patch
	$(info >>> WEB '$<')
	@patch $(CCSRD) $< --quiet
	@touch $@

# OUTPUTS

http: base web
	cp -a $(WEBDIR) $(TMPDIR)/
	mkdir -p $(PAGESDIR)
	cp $(CCSRD) $(PAGESDIR)/index.md
	incorporealcms-build $(TMPDIR)/$(WEBDIR) /var/www/localhost/incorporealcms-testing/root

# BUILD STEPS

# generate the basic markdown file
$(MD_CSRD): $(CSRD) | $(TMPDIR)
	pandoc -f docx -t gfm $(CSRD) -o $(MD_CSRD) --columns 120

# create the markdown file to patch
$(CCSRD): $(MD_CSRD)
	cp -a $(MD_CSRD) $(CCSRD)

# create the build dir
$(TMPDIR):
	mkdir -p $(TMPDIR)
