IMAGE ?= webrestrict/control:0.1.0-phase1
BUILD_VERSION ?= 0.1.0-phase1

.PHONY: help test vet build up down restart logs ps hash-password fmt

help:
	@echo "make test           run go test ./... in golang:1.25-alpine"
	@echo "make vet            run go vet ./... in golang:1.25-alpine"
	@echo "make build          build the container image ($(IMAGE))"
	@echo "make up             docker compose up -d --build"
	@echo "make down           docker compose down"
	@echo "make logs           follow container logs"
	@echo "make hash-password  generate a bcrypt hash (PASSWORD=...)"

test:
	docker run --rm --network host \
	  -v "$$PWD":/src -w /src \
	  -v webrestrict-go-mod:/go/pkg/mod \
	  -v webrestrict-go-build:/root/.cache/go-build \
	  golang:1.25-alpine sh -c 'go test ./...'

vet:
	docker run --rm --network host \
	  -v "$$PWD":/src -w /src \
	  -v webrestrict-go-mod:/go/pkg/mod \
	  -v webrestrict-go-build:/root/.cache/go-build \
	  golang:1.25-alpine sh -c 'go vet ./...'

build:
	docker build --build-arg VERSION=$(BUILD_VERSION) -t $(IMAGE) .

up:
	docker compose up -d --build

down:
	docker compose down

restart:
	docker compose restart control

logs:
	docker compose logs -f control

ps:
	docker compose ps

hash-password:
	@test -n "$(PASSWORD)" || (echo "usage: make hash-password PASSWORD=secret"; exit 2)
	docker run --rm --entrypoint /app/control $(IMAGE) hash-password '$(PASSWORD)'
