# Project metadata
APP_NAME := hspguard
CMD_DIR := ./cmd/$(APP_NAME)
BIN_DIR := ./bin

# Detect platform and add .exe suffix on Windows
OS := $(shell go env GOOS)
EXT := $(if $(filter windows,$(OS)),.exe,)
BIN_PATH := $(BIN_DIR)/$(APP_NAME)$(EXT)

PKG := ./...

# Go tools
GO := go
GOLINT := golangci-lint
GOFMT := gofmt
GOTEST := go test

# Build flags
LD_FLAGS := -s -w
BUILD_TIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>NUL || echo unknown)

.PHONY: all build clean fmt lint run test mod

all: build

build:
	$(GO) build -ldflags "-X main.buildTime=$(BUILD_TIME) -X main.commitHash=$(GIT_COMMIT) $(LD_FLAGS)" -o $(BIN_PATH) $(CMD_DIR)

run:
	$(GO) run $(CMD_DIR)

fmt:
	$(GOFMT) -s -w .

lint:
	$(GOLINT) run

test:
	$(GOTEST) -v $(PKG)

clean:
	@if [ -d "$(BIN_DIR)" ]; then rm -rf $(BIN_DIR); fi

mod:
	$(GO) mod tidy
