.PHONY: all test build docker install clean proto check_protoc

CMD_NAME := econet-exporter
VERSION ?= development
API_DIR := api/
SCHEMA_DIR := contrib/
PROTO_DIRS := $(wildcard proto/econet/v1alpha1/*)
PLATFORMS := linux/amd64 linux/arm64 darwin/amd64 darwin/arm64
OUTPUT_DIR := bin
VER_PKG := gitea.libretechconsulting.com/rmcguire/go-app/pkg/config.Version
GIT_REPO := gitea.libretechconsulting.com/rmcguire/econet-exporter

all: proto test build docker

proto: check_buf
	buf dep update
	buf generate

test:
	go test -v ./...

build: test
		@echo "Building for platforms: $(PLATFORMS)"
	@for platform in $(PLATFORMS); do \
		OS=$$(echo $$platform | cut -d/ -f1); \
		ARCH=$$(echo $$platform | cut -d/ -f2); \
		OUTPUT="$(OUTPUT_DIR)/$(CMD_NAME)-$$OS-$$ARCH"; \
		mkdir -vp $(OUTPUT_DIR); \
		echo "Building for $$platform into $$OUTPUT"; \
		GOOS=$$OS GOARCH=$$ARCH go build -ldflags "-X $(VER_PKG)=$(VERSION)" -o $$OUTPUT .; \
		echo "Built $$OUTPUT"; \
	done
	go build -ldflags "-X $(VER_PKG)=$(VERSION)" -o bin/${CMD_NAME}

schema:
	go run . -schema > contrib/schema.json

docker:
	@echo "Building Docker image $(GIT_REPO):$(VERSION)"
	docker build \
		--build-arg VER_PKG=$(VER_PKG) \
		--build-arg VERSION=$(VERSION) \
		--build-arg APP_NAME=$(CMD_NAME) \
		-t $(GIT_REPO):$(VERSION) .
	docker push $(GIT_REPO):$(VERSION)

install:
	go install -v -ldflags "-X $(VER_PKG)=$(VERSION)" .

clean:
	rm -rf bin/${CMD_NAME}

check_buf:
	@if ! command -v buf > /dev/null; then \
		echo "Error: buf not found in PATH"; \
		exit 1; \
	fi

rename:
	@echo "Current module path: $(GIT_REPO)"
	@echo "Usage: make rename NAME=your/new/module/name APP=your-app-name"
	@if [ -z "$(NAME)" ]; then \
		echo "No package name provided. Aborting."; \
		exit 1; \
	fi
	@if [ -z "$(APP)" ]; then \
		echo "No app name provided. Aborting."; \
		exit 1; \
	fi
	@echo "New name: app=$(APP) pkg=$(NAME)"
	@echo "Are you sure you want to proceed? (y/N): " && read CONFIRM && if [ "$$CONFIRM" != "y" ] && [ "$$CONFIRM" != "Y" ]; then \
		echo "Aborted."; \
		exit 1; \
	fi
	@sed -i "s|APP_NAME=.*|APP_NAME=$(APP)|g" Dockerfile
	@sed -i "s|^CMD_NAME := .*|CMD_NAME := $(APP)|g" Makefile
	@sed -i "s|merge_file_name=.*|merge_file_name=$(APP)|g" buf.gen.yaml
	@sed -i "s|^name: .*|name: $(APP)|g" helm/Chart.yaml
	@sed -i "s|otelServiceName: .*|otelServiceName: $(APP)|g" helm/values.yaml
	@sed -i "s|= \"demo-app\"|= \"$(APP)\"|g" main.go
	@sed -i "s|app=.*|app=$(APP)|g" helm/values.yaml
	@sed -i "s|$(CMD_NAME)|$(APP)|g" README.md
	@sed -i "s|$(CMD_NAME)|$(APP)|g" .gitea/workflows/ci.yml
	@sed -i "s|$(GIT_REPO)|$(NAME)|g" .gitea/workflows/ci.yml
	@rm contrib/$(CMD_NAME).swagger.json
	@find . -type f -a \
	\( -name '*.go' -o -name 'go.mod' \
		-o -name 'go.sum' -o -name '*.proto' \
		-o -name 'Makefile' \
		-o -name '*.yml' -o -name '*.yaml' \
	\) \
	-not -path './.git' -not -path './.git/*' \
	-exec sed -i "s|$(GIT_REPO)|$(NAME)|g" {} +
	@echo "\n* Project renamed to $(NAME)"
	@echo "* NOTE: You will have to update .gitea/workflows/ci.yml"
	@echo "* NOTE: You will have to run buf generate"
