VERSION := 1.1.7-beta
BUILD := $(shell git rev-parse --short HEAD)
UPDATE_SRC := "https://git.nevets.tech/api/v1/repos/Steven/certman/releases"

GO := go

BUILD_FLAGS := -buildmode=pie -trimpath
LDFLAGS := -linkmode=external -extldflags="-Wl,-z,relro,-z,now" -X git.nevets.tech/Steven/certman/common.Version=$(VERSION) -X git.nevets.tech/Steven/certman/common.Build=$(BUILD) -X git.nevets.tech/Steven/certman/app.SourceCertmanRepo=$(UPDATE_SRC)

.PHONY: proto bundle client server executor cryptr build debug stage help

proto:
	@protoc --go_out=./proto --go-grpc_out=./proto proto/hook.proto
	@protoc --go_out=./proto --go-grpc_out=./proto proto/symlink.proto

bundle: proto
	@echo "Building Bundled Certman"
	$(GO) build $(BUILD_FLAGS) -ldflags="-s -w $(LDFLAGS)" -o ./bin/certman-$(VERSION)-amd64 ./app/bundle

client: proto
	@echo "Building Certman Client"
	$(GO) build $(BUILD_FLAGS) -ldflags="-s -w $(LDFLAGS)" -o ./bin/certman-client-$(VERSION)-amd64 ./app/client

server: proto
	@echo "Building Certman Server"
	$(GO) build $(BUILD_FLAGS) -ldflags="-s -w $(LDFLAGS)" -o ./bin/certman-server-$(VERSION)-amd64 ./app/server

executor: proto
	@echo "Building Certman Executor"
	$(GO) build $(BUILD_FLAGS) -ldflags="-s -w $(LDFLAGS)" -o ./bin/certman-executor-$(VERSION)-amd64 ./app/executor

cryptr:
	@echo "Building Cryptr"
	$(GO) build $(BUILD_FLAGS) -ldflags="-s -w -linkmode=external -extldflags="-Wl,-z,relro,-z,now"" -o ./bin/cryptr ./app/cryptr

build: proto bundle client server cryptr
	@echo "All binaries successfully built"

debug: proto
	$(GO) build $(BUILD_FLAGS) -ldflags="$(LDFLAGS)" -o ./certman .
	@cp ./certman ./certman-$(VERSION)-amd64

stage: build
	@sudo cp ./certman /srv/vm-passthru/certman
	@ssh steven@192.168.122.44 updateCertman.sh

help:
	@echo "proto    - Generate gRPC proto stubs"
	@echo "bundle   - Build bundled binary (client, server, executor)"
	@echo "client   - Build client binary"
	@echo "server   - Build server binary"
	@echo "executor - Build executor binary"
	@echo "build    - Build all binaries"
	@echo "debug    - Build bundled binary without stripping and hardening"
	@echo "stage    - Build all binaries and upload to dev environment (vm)"