TMPDIR			:= ./_tmp
CSRD			:= ./og-csrd/index.html
MD_CSRD			:= $(TMPDIR)/og-csrd.md
CCSRD			:= $(TMPDIR)/ccsrd.md

STYLESDIR		:= ./pandoc
WEBDIR			:= $(TMPDIR)/web
HTML_OUT		:= $(WEBDIR)/index.html

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

DE_OGCSRD_PATCH_DIR	:= ./patches/de-ogcsrd
DE_OGCSRD_PATCHES	:= $(wildcard $(DE_OGCSRD_PATCH_DIR)/*.patch)
DE_OGCSRD_PATCHES_OUT	:= $(subst $(DE_OGCSRD_PATCH_DIR),$(TMPDIR),$(DE_OGCSRD_PATCHES))

# DEFAULT BUILD

all: base

# CCSRD VERSIONS

base: $(CCSRD) $(BASE_PATCHES_OUT)
de-ogcsrd: base $(DE_OGCSRD_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: $(DE_OGCSRD_PATCH_DIR)/%.patch
	$(info >>> BASE '$<')
	@patch $(CCSRD) $< --quiet
	@touch $@

# OUTPUTS

web: $(CCSRD)
	mkdir -p $(WEBDIR)
	pandoc $(CCSRD) --data-dir=$(STYLESDIR) --template bss -s -o $(HTML_OUT)

# BUILD STEPS

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

# generate the root markdown file
$(MD_CSRD): $(CSRD) | $(TMPDIR)
	pandoc $(CSRD) -o $(MD_CSRD) --columns 120

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