# Make cheat sheet
# := Simple assignment, evaluated once at initial assignment
# ?= Conditional assignment, assigns a value only if does not have a value
# $< First prerequisite
# $@ Target being generated

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS    ?=

# All rendered build artifacts are held under BUILDDIR/html,
# namely $(BUILDDIR)/source, $(BUILDIR)/html
BUILDDIR      ?= build

# SOURCEDIR is directory of files not generated by build process
SOURCEDIR     ?= source

# IPYNB_DIR holds jupyter notebooks that will be converted to .rst files
IPYNB_DIR := ../examples

# GENERATED_RST_DIR the location of the generated .rst files
GENERATED_RST_DIR := $(BUILDDIR)/source

IPYNB_PATTERN := $(IPYNB_DIR)/*/*.ipynb
IPYNB_FILES := $(wildcard $(IPYNB_PATTERN))

# GENERATED_RST_FILES the path of the .rst files generated from the $(IPYNB_DIR) files
GENERATED_RST_FILES := $(foreach file,$(IPYNB_FILES),$(GENERATED_RST_DIR)/$(subst $(IPYNB_DIR)/,,$(file:.ipynb=.rst)))

.PHONY: all clean notebook_rst html

# First target is default
all: html

clean:
	rm -r $(BUILDDIR) || true

$(GENERATED_RST_DIR)/%.rst: $(IPYNB_DIR)/%.ipynb
	python -m jupyter nbconvert --log-level=WARN --execute --to rst $< --output-dir $(abspath $(dir $@))

notebook_rst: $(GENERATED_RST_FILES)

html: notebook_rst
	mkdir -p $(BUILDDIR)  #make BUILDDIR if it doesn't exist
	cp -r $(SOURCEDIR)/. $(BUILDDIR)/source
	# GENERATED_RST_FILES already populated in $(BUILDDIR)/source
	sphinx-build -b html -v $(SPHINXOPTS) $(BUILDDIR)/source $(BUILDDIR)/html


