DOCKERUSER=scusemua

# NOTE:
#
# If you're using Windows, then some of these commands may not work if you're using Windows PowerShell
# or Command Prompt. I recommend using a Unix shell, such as Git Bash, on Windows when building these targets.

ifeq ($(OS),Windows_NT)     # is Windows_NT on XP, 2000, 7, Vista, 10...
    detected_OS := Windows
else
    detected_OS := $(shell uname)  # same as "uname -s"
endif

build-grpc:
	@echo "Building gRPC now."
	protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative internal/server/api/proto/gateway.proto

build-all: build-grpc build-server

# Alias for build-server.
build-backend: build-server

build-server-linux:
	@echo "Building backend server for Linux now."
	GOOS=linux GOARCH=amd64 go build -o server-linux-amd64 ./cmd/server/main.go

build-server:
	@echo Building backend server now.
	go build -o server ./cmd/server/main.go

build-servers: build-grpc build-server build-server-linux

# Build first, then create Docker image.
build-docker-image: build-grpc build-server-linux
	docker build -t $(DOCKERUSER)/distributed-notebook-dashboard-backend .

# Don't build first; use existing build to create docker image.
docker-image:
	docker build -t $(DOCKERUSER)/distributed-notebook-dashboard-backend .

push-docker-image:
	docker push $(DOCKERUSER)/distributed-notebook-dashboard-backend:latest

run-backend-docker:
	docker run -it -p "8000:8000" \
		-v $(shell pwd)/configs/config-docker-local.yaml:/etc/distributed-notebook-cluster/dashboard.yaml \
		-v $(shell pwd)/configs/workload-presets-file-docker.yaml:/etc/distributed-notebook-cluster/workload-presets-file.yaml \
		-v $(shell pwd)/.production.env:/go/.env
		--add-host host.docker.internal:host-gateway \
		scusemua/distributed-notebook-dashboard-backend:latest

# Alias for run-server.
run-backend: run-server

run-server-spoofed:
	@echo Running backend server now.
	go run ./cmd/server/main.go --yaml ./configs/config-spoofed.yaml

run-server:
	@echo Running backend server now.
	go run ./cmd/server/main.go --yaml ./configs/config.yaml

run-server-prebuilt:
	@echo Running pre-built server now.
	.\server --yaml ./configs/config.yaml

# NOTE: MUST update version number here prior to running 'make release' and edit this file!
VERS=v0.0.1
PACKAGE=main
GIT_COMMIT=`git rev-parse --short HEAD`
VERS_DATE=`date -u +%Y-%m-%d\ %H:%M`
VERS_FILE=version.go

release:
ifeq ($(detected_OS),Windows)
	rm $(VERS_FILE)
else
	/bin/rm -f $(VERS_FILE)
endif
	@echo "// WARNING: auto-generated by Makefile release target -- run 'make release' to update" > $(VERS_FILE)
	@echo "" >> $(VERS_FILE)
	@echo "package $(PACKAGE)" >> $(VERS_FILE)
	@echo "" >> $(VERS_FILE)
	@echo "const (" >> $(VERS_FILE)
	@echo " Version = \"$(VERS)\"" >> $(VERS_FILE)
	@echo " GitCommit = \"$(GIT_COMMIT)\" // the commit JUST BEFORE the release" >> $(VERS_FILE)
	@echo " VersionDate = \"$(VERS_DATE)\" // UTC" >> $(VERS_FILE)
	@echo ")" >> $(VERS_FILE)
	@echo "" >> $(VERS_FILE)
	goimports -w $(VERS_FILE)
ifeq ($(detected_OS),Windows)
	cat $(VERS_FILE)
else
	/bin/cat $(VERS_FILE)
endif
	git commit -am "$(VERS) release"
	git tag -a $(VERS) -m "$(VERS) release"
	git push
	git push origin --tags
