.PHONY: FORCE

IMAGE_TAG := $(shell git rev-parse --short HEAD)
IMAGE_REPO := registry.gitlab.com/euphon/themblem
IMAGE_REPO_PROD := registry.cn-shenzhen.aliyuncs.com/emblem/themblem
IMAGE := $(IMAGE_REPO):$(IMAGE_TAG)
IMAGE_PROD := $(IMAGE_REPO_PROD):$(IMAGE_TAG)

ifeq ($(shell uname), Darwin)
	BUILD_SHARED_LIBS := ON
else
	BUILD_SHARED_LIBS := OFF
endif

API_FILES = $(addprefix build/, $(shell find -L api -type f))

WEB_FILES = \
	$(addprefix build/, \
		$(shell find -L \
			web/dist \
			-type f)\
	)

DETECTION_FILES = \
	$(addprefix build/, \
		$(shell find -L \
			detection \
			-type f \
			-not -name '*.pyc' \
			) \
	)

SCRIPTS_FILES = \
	$(addprefix build/, \
		$(shell find -L \
			scripts \
			-type f \
			) \
	)

DATASET_FILES = \
	$(addprefix build/, \
		$(shell find -L \
			dataset \
			-name topleft-0518.jpeg \
			) \
	)

ALG_FILES = \
	$(addprefix build/, \
		$(shell find -L \
			alg/qrtool \
			alg/wechat_qrcode \
			) \
	)

docker-build: build/Dockerfile build/packages.txt build/requirements.txt download-models \
		build/nginx.conf $(WEB_FILES) $(API_FILES) $(ALG_FILES) $(DETECTION_FILES) $(SCRIPTS_FILES) $(DATASET_FILES)
	find build
	docker build --network=host -t $(IMAGE) build

docker-push:
	docker push $(IMAGE)

docker-push-prod:
	docker tag $(IMAGE) $(IMAGE_PROD)
	docker push $(IMAGE_PROD)

web: FORCE
	cd web && npm run build
	mkdir -p build/web
	cp -r web/dist build/web/dist

build/Dockefile: FORCE
	mkdir -p build
	cp Dockerfile build/Dockerfile
	echo ENV EMBLEM_IMAGE=$(IMAGE) >> build/Dockerfile

build/%: %
	mkdir -p $(shell dirname $@)
	cp -a $^ $@

deploy-api-dev:
	curl -X POST https://euphon-alert-23358.famzheng.workers.dev/ -d 'Deploying Emblem API to dev: $(IMAGE)'
	kubectl --kubeconfig deploy/kubeconfig.dev set image deploy api emblem=$(IMAGE)
	kubectl --kubeconfig deploy/kubeconfig.dev rollout status --timeout=1h deploy api

deploy-api-prod:
	curl -X POST https://euphon-alert-23358.famzheng.workers.dev/ -d 'Deploying Emblem API to prod: $(IMAGE)'
	kubectl --kubeconfig deploy/kubeconfig.themblem set image deploy api emblem=$(IMAGE)
	kubectl --kubeconfig deploy/kubeconfig.themblem rollout status --timeout=1h deploy api

deploy-roi-worker:
	curl -X POST https://euphon-alert-23358.famzheng.workers.dev/ -d 'Deploying ROI Worker to emblem-s1: $(IMAGE)'
	kubectl --kubeconfig deploy/kubeconfig.emblem-s1 set image deploy roi-worker alg=$(IMAGE)
	kubectl --kubeconfig deploy/kubeconfig.emblem-s1 rollout status --timeout=1h deploy roi-worker

test: FORCE
	cd emtest && cargo test -- --nocapture
	cd api; ./manage.py migrate && ./manage.py test tests
	make -C detection test

OPENCV_TAG := 4.9.0
opencv/src/LICENSE:
	rm -rf opencv/src opencv/contrib
	git clone --depth=1 https://gitlab.com/euphon/opencv_contrib opencv/contrib -b $(OPENCV_TAG)
	git clone --depth=1 https://gitlab.com/euphon/opencv opencv/src -b $(OPENCV_TAG)

opencv: opencv/src/LICENSE FORCE
	mkdir -p opencv/build/cpp opencv/install
	cd opencv/build/cpp && cmake \
	  -D CMAKE_BUILD_TYPE=RELEASE  \
	  -D CMAKE_INSTALL_PREFIX=$(PWD)/opencv/install  \
	  -D OPENCV_GENERATE_PKGCONFIG=ON  \
	  -D BUILD_EXAMPLES=OFF  \
	  -D INSTALL_PYTHON_EXAMPLES=OFF  \
	  -D INSTALL_C_EXAMPLES=OFF  \
	  -D BUILD_TESTS=OFF \
	  -D BUILD_PERF_TESTS=OFF \
	  -D OPENCV_EXTRA_MODULES_PATH=$(PWD)/opencv/contrib/modules \
	  -D BUILD_opencv_python2=OFF  \
	  -D BUILD_opencv_python3=OFF  \
	  -D WITH_PROTOBUF=ON \
	  -D BUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) \
	  -D WITH_GTK=OFF \
	  -D WITH_TIFF=OFF \
	  -D WITH_OPENGL=OFF \
	  -D WITH_JAVA=OFF \
	  -D WITH_PYTHON=OFF \
	  -D WITH_VA=OFF \
	  -D WITH_FREETYPE=OFF \
	  ../../src
	$(MAKE) -C opencv/build/cpp
	$(MAKE) -C opencv/build/cpp install

opencv.js: opencv/src/LICENSE FORCE
	mkdir -p opencv/build/wasm
	python3 opencv/src/platforms/js/build_js.py opencv/build/wasm \
	    --build_wasm \
	    --enable_exception \
	    --cmake_option="-DOPENCV_EXTRA_MODULES_PATH=$(PWD)/opencv/contrib/modules/" \
	    --cmake_option="-DWITH_PROTOBUF=off" \
	    --emscripten_dir=../emsdk/upstream/emscripten/ \
	    --disable_single_file

alg/qrtool:
	make -C alg qrtool


download-models: FORCE
	cd detection && python3 app.py --download-models-only
