MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MAKEFILE_DIR  := $(dir $(MAKEFILE_PATH))
TOOLS_DIR:= build-tools
GOSDN_TMP_DIR := /tmp/gosdn
CLAB_DIR := $(GOSDN_TMP_DIR)/clab
GOSDN_PRG := $(MAKEFILE_DIR)$(TOOLS_DIR)
GOPATH := $(~/go)
GOBIN := $(GOSDN_PRG)

GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
BUILD_ARTIFACTS_PATH=artifacts

PLUGIN_NAME= bundled_plugin.zip

all: build

pre:
	mkdir -p $(BUILD_ARTIFACTS_PATH)

create-gosdn-tmp-dir:
	mkdir -p $(GOSDN_TMP_DIR)

create-clab-dir: create-gosdn-tmp-dir
	mkdir -p $(CLAB_DIR)

install-tools:
	@echo Install development tooling
	mkdir -p $(GOSDN_PRG)
	export GOBIN=$(GOSDN_PRG) && go install gotest.tools/gotestsum@v1.8.1 &&\
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2 &&\
	go install github.com/vektra/mockery/v2@v2.20.0 &&\
	go install github.com/openconfig/ygot/generator@v0.27.0 &&\
	go install github.com/andresterba/go-ygot-generator-generator@v0.0.4
	@echo Finished installing development tooling

ci-install-tools:
	go install gotest.tools/gotestsum@v1.7.0

ci-lint: ci-install-tools
	golangci-lint run --config .golangci.yml --out-format code-climate

lint: install-tools
	./$(TOOLS_DIR)/golangci-lint run --config .golangci.yml | jq

lint-fix: install-tools
	./$(TOOLS_DIR)/golangci-lint run --config .golangci.yml --fix | jq

generate-controller-mocks: install-tools
	./$(TOOLS_DIR)/mockery --all --output './controller/mocks' --dir './controller/interfaces/'

generate-yang-models: install-tools
	cd models/generated/openconfig &&\
	../../../$(TOOLS_DIR)/go-ygot-generator-generator config.yaml openconfig.go &&\
	go generate &&\
	cat additions.patch >> yang.go
	cd models/generated/arista &&\
	../../../$(TOOLS_DIR)/go-ygot-generator-generator config.yaml arista.go &&\
	go generate &&\
	cat additions.patch >> yang.go

generate-csbi-yang-models: install-tools
	cd csbi/resources &&\
	../../$(TOOLS_DIR)/go-ygot-generator-generator config.yaml gostructs.go &&\
	go generate

build: pre build-gosdn build-gosdnc build-plugin-registry build-venv-manager build-arista-routing-engine-app build-hostname-checker-app build-basic-interface-monitoring-app


build-plugins:
	for d in ./plugins/examples/*/ ; do\
		d="$${d%*/}";\
	    d="$${d##*/}";\
		rm plugin-registry/plugins/$$d/$(PLUGIN_NAME);\
		$(GOBUILD) -o plugin-registry/plugins/$$d/plugin ./plugins/examples/$$d/cmd;\
		cp plugins/examples/$$d/plugin.yaml plugin-registry/plugins/$$d/;\
		zip -r -j plugin-registry/plugins/$$d/$(PLUGIN_NAME) plugin-registry/plugins/$$d/;\
		rm plugin-registry/plugins/$$d/plugin;\
	done

build-gosdn: pre
	$(GOBUILD) -trimpath -o $(BUILD_ARTIFACTS_PATH)/gosdn ./controller/cmd/gosdn

build-gosdnc: pre
	CGO_ENABLED=0 $(GOBUILD) -o $(BUILD_ARTIFACTS_PATH)/gosdnc ./cli/

build-orchestrator: pre
	CGO_ENABLED=0 $(GOBUILD) -o $(BUILD_ARTIFACTS_PATH)/orchestrator ./csbi/cmd/csbi/

build-plugin-registry: pre build-plugins
	CGO_ENABLED=0 $(GOBUILD) -o $(BUILD_ARTIFACTS_PATH)/plugin-registry ./plugin-registry/

build-venv-manager: pre
	$(GOBUILD) -o $(BUILD_ARTIFACTS_PATH)/venv-manager ./applications/venv-manager

build-arista-routing-engine-app: pre
	$(GOBUILD) -trimpath -o $(BUILD_ARTIFACTS_PATH)/arista-routing-engine ./applications/arista-routing-engine

build-hostname-checker-app: pre
	$(GOBUILD) -trimpath -o $(BUILD_ARTIFACTS_PATH)/hostname-checker ./applications/hostname-checker

build-basic-interface-monitoring-app: pre
	$(GOBUILD) -trimpath -o $(BUILD_ARTIFACTS_PATH)/basic-interface-monitoring ./applications/basic-interface-monitoring

containerize-all: containerize-gosdn containerize-gosdnc containerize-plugin-registry containerize-target

containerize-slim: containerize-gosdn containerize-gosdnc containerize-plugin-registry

containerize-gosdn:
	docker buildx build --rm -t gosdn --load -f controller/controller.Dockerfile .

containerize-gosdnc:
	docker buildx build --rm -t gosdnc --load -f cli/cli.Dockerfile .

containerize-venv-manager:
	docker buildx build --rm -t venv-manager --load -f applications/venv-manager/venv-manager.Dockerfile .

containerize-orchestrator:
	docker buildx build --rm -t orchestrator --load -f csbi/csbi.Dockerfile .

containerize-target:
	docker buildx build --rm -t gnmi-target --load -f csbi/gnmi-target/gnmitarget.Dockerfile .

containerize-plugin-registry:
	docker buildx build --rm -t plugin-registry --load -f plugin-registry/plugin-registry.Dockerfile .

containerize-arista-routing-engine-app:
	docker buildx build --rm -t arista-routing-engine-app -f applications/arista-routing-engine/arista-routing-engine.Dockerfile .

containerize-hostname-checker-app:
	docker buildx build --rm -t hostname-checker-app -f applications/hostname-checker/hostname-checker.Dockerfile .

containerlab-start: create-clab-dir containerize-all
	cd $(CLAB_DIR) &&\
	sudo containerlab deploy --topo $(MAKEFILE_DIR)dev_env_data/clab/gosdn.clab.yaml

# build gnmi-target docker file before use
containerlab-slim-start: create-clab-dir containerize-slim
	cd $(CLAB_DIR) &&\
	sudo containerlab deploy --topo $(MAKEFILE_DIR)dev_env_data/clab/gosdn_slim.clab.yaml

containerlab-slim-start-reconfigure: create-clab-dir containerize-slim
	cd $(CLAB_DIR) &&\
	sudo containerlab deploy --topo $(MAKEFILE_DIR)dev_env_data/clab/gosdn_slim.clab.yaml --reconfigure

containerlab-start-reconfigure: create-clab-dir containerize-all
	cd $(CLAB_DIR) &&\
	sudo containerlab deploy --topo $(MAKEFILE_DIR)dev_env_data/clab/gosdn.clab.yaml --reconfigure

containerlab-stop: create-clab-dir
	cd $(CLAB_DIR) &&\
	sudo containerlab destroy --topo $(MAKEFILE_DIR)dev_env_data/clab/gosdn.clab.yaml

containerlab-slim-stop: create-clab-dir
	cd $(CLAB_DIR) &&\
	sudo containerlab destroy --topo $(MAKEFILE_DIR)dev_env_data/clab/gosdn_slim.clab.yaml

containerlab-graph: create-clab-dir
	cd $(CLAB_DIR) &&\
	sudo containerlab graph --topo $(MAKEFILE_DIR)dev_env_data/clab/gosdn.clab.yaml

shell-gosdn:
	docker exec -it clab-gosdn_csbi_arista_base-gosdn bash

shell-orchestrator:
	docker exec -it clab-gosdn_csbi_arista_base-orchestrator bash

start: build-gosdn
	./$(BUILD_ARTIFACTS_PATH)/gosdn -l debug --config ./controller/configs/gosdn.toml

start-virt-manager-example: build containerize-plugin-registry
	./scripts/manage_virt_env.sh --mode start --topology dev_env_data/clab/basic_two_aristas.yaml --sdnconfig dev_env_data/sdn/basic_two_aristas.json

stop-virt-manager-example:
	./scripts/manage_virt_env.sh --mode stop --topology dev_env_data/clab/basic_two_aristas.yaml

start-dev-env: containerize-gosdn containerize-plugin-registry
	./scripts/simple-dev-setup.sh --mode start --topology dev_env_data/clab/basic_two_aristas.yaml

stop-dev-env:
	./scripts/simple-dev-setup.sh --mode stop --topology dev_env_data/clab/basic_two_aristas.yaml


clean:
	$(GOCLEAN)
	rm -rf $(BUILD_ARTIFACTS_PATH)
