PY ?= python3
VER := 1.0.0
PKG := dist/ghostpay-$(VER).zip

.PHONY: help selftest test test-fast doctor demo invoice list xpub qr serve package clean

help:
	@grep -E '^[a-z-]+:.*?##' Makefile | sed 's/:.*##/\t/' | column -t

selftest:   ## run the 68 built-in crypto/QR protocol vectors
	$(PY) ghostpay.py selftest
	$(PY) logjack.py selftest

test:       ## full suite (crypto, QR audit, end-to-end, live network + Tor)
	sh tests/run_all.sh

test-fast:  ## offline suite only
	sh tests/run_all.sh --fast

doctor:     ## check config, crypto, explorers and Tor connectivity
	$(PY) ghostpay.py doctor

demo:       ## run an offline demo shop (fake chain, no xpub needed)
	GHOSTPAY_API_KEY=demo $(PY) ghostpay.py serve --demo --port 8899

serve:      ## run the gateway
	$(PY) ghostpay.py serve

invoice:    ## create an invoice from the CLI:  make invoice SATS=150000
	$(PY) ghostpay.py invoice --sats $(or $(SATS),150000)

list:       ## list invoices
	$(PY) ghostpay.py list

xpub:       ## derive an account xpub from a seed phrase:  make xpub MNEMONIC="abandon ... about"
	$(PY) ghostpay.py xpub --mnemonic "$(MNEMONIC)"

qr:         ## render a QR to SVG:  make qr TEXT="bitcoin:bc1..." OUT=/tmp/q.svg
	$(PY) ghostpay.py qr "$(TEXT)" $(if $(OUT),--out $(OUT),)

package:    ## build the distributable zip + sha256 (extracts into ghostpay-<ver>/)
	@mkdir -p dist
	rm -f $(PKG) $(PKG).sha256
	rm -rf dist/stage && mkdir -p dist/stage/ghostpay-$(VER)
	cp -R ghostpay.py logjack.py README.md SALES.md TAILS.md PROOF.txt LICENSE Makefile Dockerfile \
	  docker-compose.yml ghostpay.service install.sh examples tests landing \
	  dist/stage/ghostpay-$(VER)/
	find dist/stage -name '__pycache__' -type d -exec rm -rf {} + 2>/dev/null || true
	rm -f dist/stage/ghostpay-$(VER)/landing/*.zip dist/stage/ghostpay-$(VER)/landing/SHA256SUMS.txt
	cd dist/stage && zip -q -r ../ghostpay-$(VER).zip ghostpay-$(VER)
	rm -rf dist/stage
	shasum -a 256 $(PKG) | tee $(PKG).sha256
	@echo "packaged $(PKG) ($$(wc -c < $(PKG)) bytes) -> extracts to ghostpay-$(VER)/"

clean:
	rm -rf dist __pycache__ tests/__pycache__ ghostpay.db ghostpay.db-wal ghostpay.db-shm
