default: all
	# $^ -> finished $@ at $(shell date)

# Use GNU utilities here if they exist (via e.g. `brew`)
ECHO := $(shell which gecho 2>/dev/null || which echo)
FIND := $(shell which gfind 2>/dev/null || which find)
LN   := $(shell which gln 2>/dev/null || which ln)

NEWEST          := $(shell $(FIND) . -type f -printf '%T@ %p\n' \
			| grep -vE '(cache|output|.doit.db|state_data|#|/sentinel)' \
			| sort -n | tail -1 | cut -f2- -d' ')
LATEX_MAKEFILES := $(shell $(FIND) latex -name Makefile)
LATEX_FOLDERS   := $(dir $(LATEX_MAKEFILES))
LATEX_TEX       := $(shell $(FIND) $(LATEX_FOLDERS) -maxdepth 1 -name [^.]\*.tex)
LATEX_SVGS      := $(patsubst %.tex,%.svg,$(LATEX_TEX))
FILES_SVGS      := $(patsubst latex/%,files/%,$(LATEX_SVGS))

DATE_FOLDERS    := $(wildcard posts/20??/[0-9][0-9]/[0-9][0-9] posts/20??/[0-9][0-9])
DATE_UP_INDEX   := $(patsubst posts/%,files/posts/%/index.html,$(DATE_FOLDERS))

NEAR_ASSET_FOLDERS   := $(shell $(FIND) pages posts -type d -name assets)
FAR_ASSET_FOLDERS    := $(patsubst %/assets,files/%/assets,$(NEAR_ASSET_FOLDERS))

NEAR_CODE_FOLDERS   := $(shell $(FIND) pages posts -type d -name code)
FAR_CODE_FOLDERS    := $(patsubst %/code,listings/%/code,$(NEAR_CODE_FOLDERS))

LIST_IGNORED_FILES := git ls-files --ignored --exclude-standard --others

newest:
	@echo ================================================================
	@echo ================ updated file: $(NEWEST)
	@echo ================================================================

deploy:
	until $(MAKE) all ; do sleep 1 ; done
	../is_clean.py | grep revision | grep clean
	git push
	$(MAKE) orphan-clean
	date | tee output/deploy
	until $(MAKE) -B output ; do sleep 1 ; done
	nikola deploy
	rm -fv output/deploy

all: latex files listings output
	# $^ -> finished $@ at $(shell date)
orphan-clean:
	nikola orphans | xargs -r rm -v
	while $(FIND) output -type d -empty | xargs rmdir -v 2>/dev/null ; \
		do : ; done
clean: latex-clean files-clean listings-clean
	rm -rf cache
	$(FIND) output -mindepth 1 -maxdepth 1 | grep -v sentinel | xargs rm -rfv
	while $(FIND) output -type d -empty | xargs rmdir -v 2>/dev/null ; \
		do : ; done
	# $^ -> finished $@ at $(shell date)

serve: all
	cd output && ../../serve_utf8.py
	# $^ -> finished $@ at $(shell date)

templates/sentinel.html: $(NEWEST)
	../is_clean.py | tee $@

output: templates/sentinel.html
	mkdir -p $@
	[ -e $@/busy ] && exit 1 || { date | tee $@/busy ; }
	make -C python/p-exam
	$(MAKE) -B $^
	nikola build || { rm $@/busy ; exit 1; }
	$(FIND) $@ -type f | xargs -r chmod 0644
	$(FIND) $@ -type d | xargs -r chmod 0755
	../is_clean.py | tee files/sentinel $@/sentinel # for the server
	rm -fv $@/busy
	touch $@
	# $^ -> finished $@ at $(shell date)

continuous: all
	while true ; do \
		[ -e output/deploy ] || \
			$(MAKE) -q -s latex output || \
			$(MAKE) newest all; \
		$(ECHO) -n -e \\r $$(date +"%F %T.%N :") "" ; \
		sleep 1; \
	done
	# $^ -> finished $@ at $(shell date)

tmux: all
	$(MAKE) $^
	#### This command-line thing would work if `make tmux` never
	#### ran in the background.
	# tmux split-window $(MAKE) continuous
	# tmux split-window $(MAKE) serve
	#### But sometimes `make tmux` gets called from the
	#### background, so this Python script is smart about *which*
	#### tmux window to modify.
	MAKE="$(MAKE)" ./make-tmux.py
	# $^ -> finished $@ at $(shell date)

latex: $(LATEX_SVGS) $(FILES_SVGS)
	touch $@
	# $^ -> finished $@ at $(shell date)
files/%.svg : latex/%.svg
	$(LN) -rs $< $@
latex/%.svg : latex/%.tex
	$(MAKE) -C $(dir $@)
	# $^ -> finished $@ at $(shell date)
latex-clean:
	$(foreach folder, $(LATEX_FOLDERS), $(MAKE) -C $(folder) clean;)
	# $^ -> finished $@ at $(shell date)

files: files/assets files/indices
	make -C python/p-exam
	touch $@
	# $^ -> finished $@ at $(shell date)
files-clean:
	$(LIST_IGNORED_FILES) files | xargs -r rm -v
	while $(FIND) files -type d -empty | xargs rmdir -v 2>/dev/null ; \
		do : ; done

files/indices: $(DATE_UP_INDEX)
	@ls -l $^ > $@
	# finished $@ at $(shell date)
files/posts/%/index.html: posts/%
	@mkdir -p $(dir $@) && cp -v files/pages/index.html $(dir $@)

files/assets: $(FAR_ASSET_FOLDERS)
	@ls -lLd $^ > $@
	# finished $@ at $(shell date)
files/%/assets: %/assets
	@rm -fv $@
	@mkdir -pv $(dir $@)
	$(LN) -rs $^ $@

listings: listings/auto
	touch $@
	# $^ -> finished $@ at $(shell date)
listings/auto: $(FAR_CODE_FOLDERS)
	# finished $@ at $(shell date)
listings/%/code : %/code
	@rm -fv $@
	@mkdir -pv $(dir $(dir $@))
	$(LN) -rs $^ $(dir $(dir $@))
	@mkdir -pv listings/auto
	rm -fv listings/auto/$(notdir $*)
	$(LN) -rsv $@ listings/auto/$(notdir $*)
	# $^ -> finished $@ at $(shell date)
listings-clean:
	rm -rfv listings/auto
	$(LIST_IGNORED_FILES) listings | xargs -r rm -v
	while $(FIND) listings -type d -empty | xargs rmdir -v 2>/dev/null ; \
		do : ; done
