# Co-op marketplace starter kit - common tasks.
# Everything here is stdlib Python; there is nothing to install.

DB ?= coopmarket.db
PORT ?= 8000   # local/dev default; deployments run 8080 behind a TLS proxy (see DEPLOYMENT.md)

.PHONY: help demo test init serve backup clean

help:
	@echo "make init    - create $(DB) and the first admin"
	@echo "make serve   - create $(DB) if missing, then serve it on port $(PORT)"
	@echo "make demo    - build demo.db with sample members and jobs, then serve it"
	@echo "make test    - run the 53-check end-to-end smoke test"
	@echo "make backup  - consistent snapshot of $(DB) into backups/"
	@echo "make clean   - remove demo databases and __pycache__"

# A fresh clone has no database. `make serve` should still work, so the file
# target builds itself once and is then left alone.
$(DB):
	python3 run.py init --db $(DB) --coop-name "New Co-op Marketplace"

init:
	python3 run.py init --db $(DB)

demo:
	python3 tools/seed_demo.py --db demo.db --force
	@echo "sign in at http://127.0.0.1:$(PORT)/ (logins printed above)"
	python3 run.py serve --db demo.db --host 127.0.0.1 --port $(PORT)

test:
	COOPMARKET_QUIET=1 python3 tools/smoke_test.py

serve: $(DB)
	python3 run.py serve --db $(DB) --host 0.0.0.0 --port $(PORT)

backup:
	python3 run.py backup --db $(DB) --out backups

clean:
	rm -f demo.db demo.db-wal demo.db-shm
	find . -name __pycache__ -type d -exec rm -rf {} +
