# Deployment and development helpers.  Nothing here needs any dependency beyond
# Python 3.11+ and, for the container targets, Docker.

CONFIG ?= config.json
IMAGE  ?= opds-from-rss:latest
PYTHON ?= python3

.PHONY: help test run check demo demo-up demo-down build up down logs shell hash-password lint clean

help:
	@echo "make test           run the test suite (no dependencies needed)"
	@echo "make run            run the server locally with $(CONFIG)"
	@echo "make check          validate $(CONFIG) and print the effective settings"
	@echo "make hash-password  print the sha256 digest for auth.users[].password_sha256"
	@echo "make demo           serve a built-in fake FreshRSS, for a smoke test"
	@echo "make demo-up        run the self-contained demo stack (catalogue + fake FreshRSS)"
	@echo "make demo-down      stop the demo stack"
	@echo "make build          build the container image"
	@echo "make up / down      start or stop the container via docker compose"
	@echo "make logs           follow the container logs"
	@echo "make shell          open a shell in the image"
	@echo "make lint           byte-compile and check formatting of the sources"
	@echo "make clean          remove caches and local configuration"

test:
	$(PYTHON) -m unittest discover -s tests -t . -v

lint:
	$(PYTHON) -m compileall -q app tests tools
	@echo "sources compile cleanly"

run:
	$(PYTHON) -m app --config $(CONFIG)

check:
	$(PYTHON) -m app --config $(CONFIG) --check-config

hash-password:
	@$(PYTHON) -c "import getpass, hashlib; print(hashlib.sha256(getpass.getpass('Password: ').encode()).hexdigest())"

demo:
	$(PYTHON) tools/fake-freshrss.py

# The self-contained stack: builds, starts, and prints where to point a reader.
demo-up:
	docker compose -f docker-compose.demo.yaml up -d --build
	@echo ""
	@echo "catalogue: http://127.0.0.1:8088/opds   (sign in as demo / demo)"
	@echo "logs:      make demo-down  |  docker compose -f docker-compose.demo.yaml logs -f"

demo-down:
	docker compose -f docker-compose.demo.yaml down

build:
	docker build -t $(IMAGE) .

up:
	@test -f config.json || { echo "config.json is missing: cp config.example.json config.json, then edit it"; exit 1; }
	docker compose up -d --build

down:
	docker compose down

logs:
	docker compose logs -f

shell:
	docker run --rm -it --entrypoint /bin/sh $(IMAGE)

clean:
	rm -rf app/__pycache__ tests/__pycache__ tools/__pycache__ .env config.json
