# BiRefNet background removal service — CLI shortcuts.
# Override defaults inline, e.g.:  make test BG=white INPUT=photo.jpg

COMPOSE   ?= docker compose
PYTHON    ?= python3
PORT      ?= 8000
INPUT     ?= test.jpg
OUTPUT    ?= output.png
BG        ?= alpha
BLUR      ?= 0

.DEFAULT_GOAL := help

.PHONY: help build run up stop down logs log ps test test-mask dev sync shell clean fmt drytestpub testpub bump

help: ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## ' $(MAKEFILE_LIST) \
		| awk 'BEGIN{FS=":.*?## "}{printf "  \033[36m%-12s\033[0m %s\n", $$1, $$2}'

build: ## Build the Docker image
	$(COMPOSE) build

run up: ## Start the service (GPU) in the background
	$(COMPOSE) up -d

stop down: ## Stop and remove the service container
	$(COMPOSE) down

logs log: ## Follow service logs
	$(COMPOSE) logs -f

ps: ## Show service status
	$(COMPOSE) ps

test: ## Send INPUT to the running service, save OUTPUT
	$(PYTHON) scripts/client.py --url http://localhost:$(PORT) \
		--input $(INPUT) --output $(OUTPUT) --background $(BG) --mask-blur $(BLUR)

test-mask: ## Like 'test' but also save the raw mask (mask.png)
	$(PYTHON) scripts/client.py --url http://localhost:$(PORT) \
		--input $(INPUT) --output $(OUTPUT) --background $(BG) \
		--mask-blur $(BLUR) --mask-output mask.png

sync: ## Install dependencies locally with uv
	uv sync

dev: sync ## Run the service locally (no Docker; needs local CUDA)
	uv run rmbg-as-a-service

shell: ## Open a shell inside a fresh container
	$(COMPOSE) run --rm --entrypoint bash rmbg

fmt: ## Format code with ruff
	uv run ruff format src scripts

clean: ## Stop the service and remove build artifacts
	-$(COMPOSE) down
	rm -f $(OUTPUT) mask.png

bump: ## Bump the patch version in pyproject.toml (0.0.1 -> 0.0.2)
	uv version --bump patch

drytestpub: ## Publish dist/ to TestPyPI (UV_PUBLISH_TOKEN from .env)
	uv build && \
	set -a && . ./.env && set +a && \
		uv publish --publish-url https://test.pypi.org/legacy/ -t "$$UV_PUBLISH_TOKEN" --dry-run

testpub: ## Publish dist/ to TestPyPI (UV_PUBLISH_TOKEN from .env)
	uv build && \
	set -a && . ./.env && set +a && \
		uv publish --publish-url https://test.pypi.org/legacy/ -t "$$UV_PUBLISH_TOKEN" 

publish:
	rm -rf dist/ && uv build && uv publish
