PY ?= python
VERSION ?= 0.1.0
IMAGE ?= vpcatlas

.PHONY: install lint format test run render-malz docker-build binary docs lock chart-lint chart-package

install:
	$(PY) -m pip install -e ".[dev]"

lint:
	$(PY) -m ruff check src tests
	$(PY) -m ruff format --check src tests

format:
	$(PY) -m ruff format src tests

test:
	$(PY) -m pytest

run:
	$(PY) -m vpcatlas.cli serve --port 8080

docker-build:
	docker build -f deploy/Dockerfile -t $(IMAGE):$(VERSION) .

# Program folder with the dashboard, packed: dist/vpcatlas-<version>-<os>-<arch>.tar.gz (.zip on Windows) and its .sha256 file.
binary:
	PYTHON=$(PY) bash scripts/build-binary.sh

# The Helm chart: lint and render it with the default values and with every option on (needs helm).
chart-lint:
	helm lint --strict deploy/helm/vpcatlas
	helm lint --strict deploy/helm/vpcatlas -f deploy/helm/vpcatlas/ci/full-values.yaml
	helm template atlas deploy/helm/vpcatlas --kube-version 1.30 > /dev/null
	helm template atlas deploy/helm/vpcatlas -f deploy/helm/vpcatlas/ci/full-values.yaml --kube-version 1.30 > /dev/null

# dist/vpcatlas-<version>.tgz, the file the release attaches to the GitHub Release.
chart-package:
	helm package deploy/helm/vpcatlas --destination dist

# Hash-pinned requirements.lock and requirements-build.lock, which the binary and the images install from.
lock:
	bash scripts/lock.sh

# The documentation site (mkdocs.yml) built into site/; fails on any broken link.
docs:
	$(PY) -m pip install -e ".[docs]"
	$(PY) -m mkdocs build --strict

# PNG export: draw.io desktop silently cuts the bottom of a page once the bitmap passes roughly
# 25 million pixels (measured: 6105x5523 at scale 2 lost the lower accounts), so the scale is
# picked per page from the declared pageWidth/pageHeight and never exceeds 2.
DRAWIO ?= /Applications/draw.io.app/Contents/MacOS/draw.io
render-malz:
	PYTHONPATH=tests $(PY) -m fixtures.malz .build/malz-graph.json
	$(PY) -m vpcatlas.cli diagram --cache .build/malz-graph.json -o .build/malz
	@for f in .build/malz/diagrams/*.drawio; do \
	  s=$$($(PY) -c "import re,sys; m=re.search(r'pageWidth=\"(\d+)\" pageHeight=\"(\d+)\"', open(sys.argv[1]).read()); w,h=map(int,m.groups()); print(2 if w*h*4 <= 24_000_000 else 1)" "$$f"); \
	  "$(DRAWIO)" -x -f png -s $$s -a -b 20 -o "$${f%.drawio}.png" "$$f" >/dev/null 2>&1 && echo "$${f%.drawio}.png (scale $$s)"; \
	done

docs-publish: ## Build the docs and publish them, with install.sh and install.ps1, to vpcatlas.com
	sh scripts/publish-docs.sh
