# Website - Makefile
# Rafael P Andrade

SHELL    := /bin/sh

# Assumptions:
# - All the needed source files are in $(INPUT_DIR)
# - The Markdown file to convert to HTML has an .md suffix
# - There is a file called CONTENT_FILE that lists in order the
#   directories and files on the 1st level that have content
#   to be rendered/copied
# - Every other file found is to be copied verbatim

# TODO: move _includes to this (web) folder
#       change tree command to:
#       tree --matchdirs -P "$(cat content.txt | tr '/\n' '*|')" \
#       --prune --noreport


include config.txt
MAKEFLAGS += --no-builtin-rules


HEADER_SOURCE := ./$(INPUT_DIR)/_includes/navbar.part
FOOTER_SOURCE := ./$(INPUT_DIR)/_includes/footer.part
MAP_GEN       := ./$(INPUT_DIR)/_includes/sitemap.sh
CONTENT       := ./$(INPUT_DIR)/$(CONTENT_FILE)
MOST_RECENT   := ./$(INPUT_DIR)/_includes/latest.sh
PREPROCESSOR  := sed -e "s|_BASE_PATH_|$(BASE_PATH)|g"


vpath %.md $(INPUT_DIR)

PANDOC     := $(shell which pandoc)
CSS        := --css $(BASE_PATH)/assets/pandoc.css
OPTIMIZE   := --no-highlight
NAVBAR     := --include-before-body $(HEADER_SOURCE)
FOOTER     := --include-after-body  $(FOOTER_SOURCE)
PFLAGS     := --standalone $(NAVBAR) $(FOOTER) --variable=lang:en $(CSS)



# Here, files are the things that still need to be
# concatenated with the footer and navbar

# Basically, folders to include:
# - all the posts
# - the about.md and index.md files
# Now, we use a separate file for the sake of reuse
# Attention: we need to prepend the input dir so that
# these are listed according to what exists in the source dir
FIND_PATHS := $(shell cat $(CONTENT))


# Which files are site configuration? config.txt and CONTENT_FILE
BASE_FILES := config.txt $(CONTENT_FILE)

# Which things are files? Markdown and html
FIND_FLAGS := \( -name '*.md' -o -name '*.html' \) -type f

# Files: actual normal posts/pages
FILES      := $(shell cd $(INPUT_DIR) && find $(FIND_PATHS) $(FIND_FLAGS) | \
                sed -e 's|md$$|html|' )

# The files that are alongside the posts, but are neither .md or .html
# These are suposed to be copied over
TO_COPY_FLAGS := ! \( -name '*.md' -o -name '*.html' \) -type f

TO_COPY := $(shell cd $(INPUT_DIR) && find $(FIND_PATHS) $(TO_COPY_FLAGS))


# Nav: pages to aid navigation on the website.
# Currently, only the main nav page and search are implemented
NAV        := sitemap.html


# Assets: CSS, images and company, to be copied to assets/ folder
ASSETS     := $(shell find $(INPUT_DIR)/assets -type f -printf "assets/%P\n")

.PHONY: base lite all clean full cleaner fuller assets nav list-files pattern-hack

all     : base

lite    : PFLAGS += $(OPTIMIZE)
lite    : base

base    : $(ASSETS) $(FILES) $(TO_COPY) $(NAV) $(BASE_FILES)

clean   :
	$(RM) $(FILES) $(NAV) $(TO_COPY)

cleaner : clean
	$(RM) $(ASSETS) $(BASE_FILES)

full    : clean all
fuller  : cleaner all

assets  : $(ASSETS)
nav     : $(NAV)

list-files:
	echo $(TO_COPY)

pattern-hack:
	$(SHELL) -c 'echo $$(echo $(notdir $(FILES)) | tr " " "|")'

# File rules:

#  - Sitemap
sitemap.html: . $(HEADER_SOURCE) $(FOOTER_SOURCE) $(MAP_GEN) $(CONTENT_FILE)
	$(SHELL) -c '$(MAP_GEN) -a -P $$(echo $(filter-out index.html,$(notdir $(FILES))) | tr " " "|") --prune' | \
	$(PANDOC) --metadata=title:Sitemap --from=html --to=html $(PFLAGS) | \
	$(PREPROCESSOR) >$@.tmp
	mv $@.tmp $@

#  - Index (first page), with most recent pages, with a markdown source
index.html : $(INPUT_DIR)/index.md $(HEADER_SOURCE) $(FOOTER_SOURCE) \
             $(MOST_RECENT)
	$(SHELL) -c '$(MOST_RECENT) "$(INPUT_DIR)"' | \
	cat $< - | \
	$(PANDOC) $(PFLAGS) | \
	$(PREPROCESSOR) >$@.tmp
	mv $@.tmp $@

#  - Normal posts and pages
%.html : %.md $(HEADER_SOURCE) $(FOOTER_SOURCE)
	mkdir -p $(dir $@) && $(PANDOC) $(PFLAGS) $< | $(PREPROCESSOR) >$@.tmp
	mv $@.tmp $@

#  - Stuff to copy over
$(TO_COPY) : % : $(INPUT_DIR)/%
	mkdir -p $(dir $@) && cp -f $(INPUT_DIR)/$@ $@


$(ASSETS): % : $(INPUT_DIR)/%
	mkdir -p $(dir $@) && cp -f $< $@


# Config.txt: basic confs. This + the Makefile + content is the only
# requirement to generate site
config.txt: $(INPUT_DIR)/config.txt
	cp $< $@

# CONTENT_FILE: essencial to get list of pages belonging to the blog
$(CONTENT_FILE): $(CONTENT)
	sed -e 's|md$$|html|' $< >$@.tmp
	mv $@.tmp $@
