SHELL := /usr/bin/env bash

.PHONY: lint-%
lint-%: BUILD_TARGET=$*
lint-%:
	cd $(BUILD_TARGET) && golangci-lint run

lint-helm:
	make -C ./helm-charts lint

.PHONY: build-%
build-%: BUILD_TARGET=$*
build-%: OUTPUT_DIR ?= bin
build-%:
	cd $(BUILD_TARGET) && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o $(OUTPUT_DIR)/$(BUILD_TARGET)

.PHONY: docker-build-%
docker-build-%: BUILD_TARGET=$*
docker-build-%: IMG ?= $(BUILD_TARGET):latest
docker-build-%: build-%
	docker build -t $(IMG) -f $(BUILD_TARGET)/Dockerfile $(BUILD_TARGET)

.PHONY: docker-push
docker-push:
	docker push ${IMG}

# uncomment it if pushd doesn't work
# SHELL := /bin/bash

# Run a go command in a module directory and strip the toolchain directive.
# Usage: $(call go-run-in-module,<dir>,<go-command>)
define go-run-in-module
	pushd $(1) && $(2) && go mod edit -toolchain=none && popd
endef

.PHONY: tidy
tidy:
	$(call go-run-in-module,./util,go mod tidy)
	$(call go-run-in-module,./api/tidbcloud/cluster-server/,go mod tidy)
	$(call go-run-in-module,./api/tidbcloud/cluster-agent,go mod tidy)
	$(call go-run-in-module,./pkg/gitops-sdk,go mod tidy)
	$(call go-run-in-module,./pkg,go mod tidy)
	$(call go-run-in-module,./chaos-provider,go mod tidy)
	$(call go-run-in-module,./cluster-controller,go mod tidy)
	$(call go-run-in-module,./cluster-controller/tests/integration,go mod tidy)
	$(call go-run-in-module,./api,go mod tidy)
	$(call go-run-in-module,./cluster-server,go mod tidy)
	$(call go-run-in-module,./cluster-prober,go mod tidy)
	$(call go-run-in-module,./clusterproxy,go mod tidy)
	$(call go-run-in-module,./config-manager,go mod tidy)
	$(call go-run-in-module,./config-manager/test/integration,go mod tidy)
	$(call go-run-in-module,./cluster-agent,go mod tidy)
	$(call go-run-in-module,./infra-exporter,go mod tidy)
	$(call go-run-in-module,./infra-inspector,go mod tidy)
	$(call go-run-in-module,./network-policy-controller,go mod tidy)
	$(call go-run-in-module,./quota-exporter,go mod tidy)
	$(call go-run-in-module,./regional-server,go mod tidy)
	$(call go-run-in-module,./infra-webhook,go mod tidy)
	$(call go-run-in-module,./ticdc-service,go mod tidy)
	$(call go-run-in-module,./infra-api,go mod tidy)

.PHONY: sync-op-v2-crd
sync-op-v2-crd:
	./.github/scripts/sync-tidb-operator-v2-crd.sh

.PHONY: sync-op-v1-crd
sync-op-v1-crd:
	tidb_operator_version=${version}  make -C ./helm-charts bump-tidb-operator

# usage: GOWORK=off version=v1.7.0-alpha.12 make bump-tidb-operator-in-gomod
.PHONY: bump-tidb-operator-in-gomod
bump-tidb-operator-in-gomod:
	$(call go-run-in-module,./infra-api,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./pkg/gitops-sdk,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./pkg,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./chaos-provider,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./cluster-controller,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./cluster-controller/tests/integration,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./cluster-server,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./cluster-prober,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./clusterproxy,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./clusterproxy,go get github.com/pingcap/tidb-operator/pkg/client@${version})
	$(call go-run-in-module,./config-manager,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./config-manager/test/integration,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./cluster-agent,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./infra-exporter,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./infra-inspector,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./quota-exporter,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./regional-server,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./infra-webhook,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./ticdc-service,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
	$(call go-run-in-module,./tools/meta-backup,go get github.com/pingcap/tidb-operator/pkg/apis@${version})
