# IMG is the base path for images..
# Individual images will be
# $(IMG)/$(NAME):$(TAG)
IMG ?= gcr.io/code-search-demo/mlapp/base
PROJECT ?= code-search-demo

# List any changed  files. We only include files in the notebooks directory.
# because that is the code in the docker image.
# In particular we exclude changes to the ksonnet configs.
CHANGED_FILES := $(shell git diff-files)

# Whether to use cached images with GCB
USE_IMAGE_CACHE ?= true

ifeq ($(strip $(CHANGED_FILES)),)
# Changed files is empty; not dirty
# Don't include --dirty because it could be dirty if files outside the ones we care
# about changed.
GIT_VERSION := $(shell git describe --always)
else
GIT_VERSION := $(shell git describe --always)-dirty-$(shell git diff | shasum -a256 | cut -c -6)
endif

CONTEXT=label-bot-frontend-prod

hydrate-prod:
	rm -rf .build/prod
	mkdir -p .build/prod
	kustomize build -o .build/prod deployment/overlays/prod

apply-prod: hydrate-prod
	kubectl --context=$(CONTEXT) apply -f .build/prod

TAG := $(shell date +v%Y%m%d)-$(GIT_VERSION)
all: build

# To build without the cache set the environment variable
# export DOCKER_BUILD_OPTS=--no-cache
.PHONY: build
build-dir: ./deployment/Dockerfile ./requirements.txt ./flask_app
	rm -rf ./build
	mkdir  -p build
	cp ./requirements.txt ./build
	cp ./deployment/Dockerfile ./build
	cp -r ./flask_app ./build

build: build-dir
	cd build && docker build ${DOCKER_BUILD_OPTS} -t $(IMG):$(TAG) -f Dockerfile . \
           --label=git-verions=$(GIT_VERSION)
	docker tag $(IMG):$(TAG) $(IMG):latest
	@echo Built $(IMG):latest
	@echo Built $(IMG):$(TAG)


build-gcb: build-dir	
	gcloud builds submit --machine-type=n1-highcpu-32 --project=$(PROJECT) --tag=$(IMG):$(TAG) \
		--timeout=3600 ./build
	@echo Built $(IMG):$(TAG)

# Build but don't attach the latest tag. This allows manual testing/inspection of the image
# first.
push: build
	docker -- push $(IMG):$(TAG)
	@echo Pushed $(IMG):$(TAG)
