BINARY   := acme-reverseproxy
PKG      := .
GO       := go
GOFLAGS  := -mod=vendor
LDFLAGS  := -s -w

BIN_DIR  := bin
BIN      := $(BIN_DIR)/$(BINARY)

.PHONY: all build install run test fmt vet mod tidy clean docker gen-config help

all: build

help:
	@echo "Targets:"
	@echo "  build       compile $(BINARY) to $(BIN)"
	@echo "  install     install to \$$GOPATH/bin or \$$GOBIN"
	@echo "  run         run server with example-config.toml"
	@echo "  test        run tests"
	@echo "  fmt         go fmt ./..."
	@echo "  vet         go vet ./..."
	@echo "  mod         download and verify modules"
	@echo "  tidy        go mod tidy"
	@echo "  gen-config  generate sample config to stdout"
	@echo "  docker      build Docker image"
	@echo "  clean       remove build artifacts"

build: $(BIN)

$(BIN):
	@mkdir -p $(BIN_DIR)
	$(GO) build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(BIN) $(PKG)

install:
	$(GO) install $(GOFLAGS) -ldflags "$(LDFLAGS)" $(PKG)

run: build
	$(BIN) srv --config example-config.toml

test:
	$(GO) test $(GOFLAGS) ./...

fmt:
	$(GO) fmt ./...

vet:
	$(GO) vet $(GOFLAGS) ./...

mod:
	$(GO) mod download
	$(GO) mod verify

tidy:
	$(GO) mod tidy

gen-config: build
	$(BIN) gen config

docker:
	docker build -t $(BINARY) .

clean:
	rm -rf $(BIN_DIR)
