K8S_VERSION = v1.26.3
KUBECONFIG = $(PWD)/.kubeconfig
CLUSTER = $(shell kind get clusters | grep 'bh-local')
GOBIN = $(shell go env GOPATH)/bin

###################
# Utilities
###################

.PHONY: setup
setup: $(GOBIN)/sqlc $(GOBIN)/buf ./env/.age.txt
	@asdf install || true

.PHONY: gen
gen: $(GOBIN)/sqlc buf.lock
	@$(GOBIN)/sqlc generate -f ./src/sqlc.yaml
	@cd ./src && $(GOBIN)/buf generate

.PHONY: dev
dev: .kubeconfig
	KUBECONFIG=$(KUBECONFIG) tilt up

.PHONY: clobber
clobber:
	KUBECONFIG=$(KUBECONFIG) ctlptl delete registry kind-bh-registry || true
	KUBECONFIG=$(KUBECONFIG) ctlptl delete cluster kind-bh-local || true
	@rm -f $(KUBECONFIG)


.PHONY: acceptance-test
acceptance-test:
	docker run -it --rm \
		-v $(pwd)/src/acceptance-tests.yml:/tests/tests.yml \
		-e "TEST_HOST=bh.localhost" \
		-e "TEST_ENV=local" \
		nytimes/httptest


##############################
# Container Image Building
##############################

SERVICE = "catalog"
ENV = ""
ORIGIN = "https://barretthousen.com"
BUILD_INITIATOR = "Development Machine"
VERSION = $(shell git rev-parse --verify --short HEAD)
GIT_REF = $(shell git rev-parse --verify HEAD)
BUILD_DATE := $(shell date +%Y-%m-%d-%T)

.PHONY: build-client-image
build-client-image:
	docker build --target=production \
		--label 'com.barretthousen.service=$(SERVICE)' \
		--label 'com.barretthousen.version=$(VERSION)' \
		--label 'com.barretthousen.git-ref=$(GIT_REF)' \
		--label 'com.barretthousen.build-date=$(BUILD_DATE)' \
		--label 'com.barrethousen.builder=$(BUILD_INITIATOR)' \
		--build-arg 'origin=$(ORIGIN)' \
		--build-arg 'service=$(SERVICE)' \
		-t barretthousen/client-$(SERVICE):$(VERSION) \
		-t git.vdhsn.com/barretthousen/client-$(SERVICE):$(VERSION) \
		-f ./src/Dockerfile.frontend ./src/$(SERVICE)

	@[ ! -z $(ENV) ] && docker tag git.vdhsn.com/barretthousen/client-$(SERVICE):$(VERSION) git.vdhsn.com/barretthousen/client-$(SERVICE):$(ENV) || true
	@[ ! -z $(ENV) ] && docker push git.vdhsn.com/barretthousen/client-$(SERVICE):$(VERSION) || true
	@[ ! -z $(ENV) ] && docker push git.vdhsn.com/barretthousen/client-$(SERVICE):$(ENV) || true


.PHONY: build-backend-image
build-backend-image:
	docker build --target=production \
		--label 'com.barretthousen.service=$(SERVICE)' \
		--label 'com.barretthousen.version="$(VERSION)"' \
		--label 'com.barretthousen.git-ref="$(GIT_REF)"' \
		--label 'com.barretthousen.build-date=$(BUILD_DATE)' \
		--label 'com.barrethousen.builder=$(BUILD_INITIATOR)' \
		--build-arg 'service=$(SERVICE)' \
		-t barretthousen/service-$(SERVICE):$(VERSION) \
		-t git.vdhsn.com/barretthousen/service-$(SERVICE):$(VERSION) \
		-t git.vdhsn.com/barretthousen/service-$(SERVICE):$(ENV) \
		-f ./src/Dockerfile.prod-backend ./src

	@[ ! -z $(ENV) ] && docker tag git.vdhsn.com/barretthousen/service-$(SERVICE):$(VERSION) git.vdhsn.com/barretthousen/service-$(SERVICE):$(ENV) || true
	@[ ! -z $(ENV) ] && docker push git.vdhsn.com/barretthousen/service-$(SERVICE):$(VERSION) || true
	@[ ! -z $(ENV) ] && docker push git.vdhsn.com/barretthousen/service-$(SERVICE):$(ENV) || true


buf.lock: $(GOBIN)/buf
	@$(GOBIN)/buf mod update ./src

$(GOBIN)/buf:
	@go install github.com/bufbuild/buf/cmd/buf@v1.17.0

$(GOBIN)/sqlc:
	@go install github.com/kyleconroy/sqlc/cmd/sqlc@latest

.kubeconfig:
	@KUBECONFIG=$(KUBECONFIG) ctlptl create registry kind-bh-registry --port=5005
	@KUBECONFIG=$(KUBECONFIG) ctlptl create cluster kind --name=kind-bh-local --registry=kind-bh-registry --kubernetes-version $(K8S_VERSION)
	@kind get kubeconfig --name=bh-local > .kubeconfig

# used to encrypt/decrypt sensitive values with sops
age_identity=$(shell sops -d ./env/master.json)
./env/.age.txt:
	@echo "# created: $(shell echo '$(age_identity)' | jq -r '.created')" >> $@
	@echo "# public key: $(shell echo '$(age_identity)' | jq -r '.public_key')" >> $@
	@echo "$(shell echo '$(age_identity)' | jq -r '.private_key')" >> $@
	@echo "$@ created!"
