# docgen — one target per thing this makes.
#
# The folder is meant to be copied out of soleprint and used on its own, so
# everything here is derived from where this file sits rather than written down:
# copy the directory anywhere, `cd` into it, and `make` works. Renaming it works
# too, since the package name comes from the directory.
#
# This tool is a library, not a CLI. There is no `python -m docgen` and no
# argparse — the prompt is explicit that it is consumed from a notebook. So each
# target below is a one-line `python3 -c` against the public surface, which
# doubles as the shortest possible worked example of calling it.
#
#   make check                      prove it works, on fixtures it builds
#   make notebook                   the three .ipynb variants -> out/
#   make graph                      the example graph, every profile -> out/
#   make extract DIR=~/diagrams     a folder of exports -> tokens + a profile
#   make profiles                   what profiles are available
#   make doctor                     what this machine has
#
# The logic lives in the Python, never here.

HERE   := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
PKG    := $(notdir $(HERE))
PARENT := $(patsubst %/,%,$(dir $(HERE)))
PY     ?= python3

# Run the package from its parent, which is what an import needs and what lets
# this work without installing anything.
RUN := PYTHONPATH=$(PARENT) $(PY) -c

OUT  ?= $(HERE)/out
DIR  ?=
NAME ?= extracted

.PHONY: help check notebook graph extract profiles doctor clean

help:  ## List every target
	@echo "docgen — graph generation and document output, with the style as data"
	@echo
	@grep -E '^[a-z-]+:.*?## .*$$' $(MAKEFILE_LIST) \
	  | awk 'BEGIN{FS=":.*?## "}{printf "  \033[1m%-12s\033[0m %s\n", $$1, $$2}'
	@echo
	@echo "  OUT=/path         where output goes  (default: $(PKG)/out)"
	@echo "  DIR=/path         the folder of exported diagrams, for extract"
	@echo "  NAME=lucid-real   what to call the extracted profile"
	@echo
	@echo "  It is a library. From a notebook:"
	@echo "      from $(PKG) import Profile, emit, render   # graphgen owns Graph"

check:  ## Prove the whole thing works, needing nothing installed and no network
	@$(PY) $(HERE)/selftest.py

notebook:  ## Emit vanilla, executable and live .ipynb into OUT
	@$(RUN) "from $(PKG).export import write_all; \
from $(PKG).export.specs import vanilla; \
[print('  ', p) for p in write_all(vanilla.build(), '$(OUT)')]"

graph:  ## Render graphgen's example graph through every shipped profile into OUT
	@$(RUN) "from $(PKG).demo import render_all; render_all('$(OUT)')"

extract:  ## A folder of exported SVG/PDF diagrams -> tokens.json + a profile
	@test -n "$(DIR)" || { echo "Error: set DIR=/path/to/diagrams" >&2; exit 1; }
	@test -d "$(DIR)" || { echo "Error: no such folder: $(DIR)" >&2; exit 1; }
	@$(RUN) "from $(PKG).style import from_folder, summarise; \
r = from_folder('$(DIR)', '$(OUT)', '$(NAME)'); \
print(summarise(r['data'])); \
print(); print('  tokens  ', r['tokens']); print('  profile ', r['profile']); \
print(); print('  Drop the profile into $(PKG)/profiles/ to use it by name,'); \
print('  or pass its path: Profile.load(\"%s\")' % r['profile'])"

profiles:  ## What style profiles are available
	@$(RUN) "from $(PKG) import Profile; \
[print('  %-12s %s' % (n, Profile.load(n).data.get('note','')[:88])) for n in Profile.available()]"

doctor:  ## Report whether this machine can run it
	@printf 'python     : '; $(PY) --version 2>&1 || echo MISSING
	@printf 'dot        : '; (dot -V 2>&1) || echo 'MISSING — sudo apt install graphviz (needed to render)'
	@printf 'pdftocairo : '; (pdftocairo -v 2>&1 | head -1) || echo 'MISSING — sudo apt install poppler-utils (only for PDF sources)'
	@printf 'lxml       : '; $(PY) -c 'import lxml.etree; print(lxml.etree.__version__)' 2>/dev/null || echo 'MISSING — pip install lxml (only for extraction)'
	@printf 'package    : %s (from %s)\n' '$(PKG)' '$(PARENT)'
	@printf 'graphgen   : '; test -d '$(PARENT)/graphgen' \
	  && echo 'beside this folder — `make graph` will work' \
	  || echo 'absent — everything works except `make graph` (it owns the model)'
	@$(RUN) "import $(PKG), $(PKG).style, $(PKG).export" >/dev/null 2>&1 \
	  && echo 'import     : ok' || echo 'import     : FAILED — is the folder intact?'

clean:  ## Delete OUT. Nothing else is ever written to
	@rm -rf "$(OUT)" && echo "Removed $(OUT)"
