#!/usr/bin/make -f

export PYBUILD_NAME=google-compute-engine
export PYBUILD_TEST_PYTEST=1
export PYBUILD_TEST_ARGS={dir}/google_compute_engine/

OSLOGIN_DIR=google_compute_engine_oslogin

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $@ --with python2,python3,systemd --buildsystem=pybuild

override_dh_auto_configure:
	cp -r /usr/src/gtest/ ./
	dh_auto_configure

override_dh_auto_test:
	(cd gtest && cmake . && $(MAKE) )
	(cd $(OSLOGIN_DIR)/utils && \
		$(CXX) $(CXXFLAGS) -L../../gtest -o test_runner oslogin_utils_test.cc oslogin_utils.cc -lcurl -ljson-c -lgtest -lpthread && \
		./test_runner && \
		rm test_runner)

override_dh_auto_build:
	dh_auto_build
	$(MAKE) -C $(OSLOGIN_DIR) all

override_dh_auto_install:
	dh_auto_install
	$(MAKE) -C $(OSLOGIN_DIR) DESTDIR=$(CURDIR)/debian/tmp PAM_INSTALL_PATH=/lib/$(DEB_HOST_MULTIARCH)/security BIN_INSTALL_PATH=/usr/bin install

override_dh_systemd_start:
	dh_systemd_start --no-restart-on-upgrade --no-start --name=google-startup-scripts
	dh_systemd_start --no-restart-on-upgrade --no-start --name=google-shutdown-scripts

override_dh_python3:
	dh_python3
	# We want to split the Python 3 scripts out to the
	# gce-compute-image-packages package
	mkdir -p debian/gce-compute-image-packages/usr/bin/
	mv debian/python3-google-compute-engine/usr/bin/* debian/gce-compute-image-packages/usr/bin/
	rmdir debian/python3-google-compute-engine/usr/bin

override_dh_python2:
	dh_python2
	# We don't want the Python 2 scripts at all
	rm -rf debian/python-google-compute-engine/usr/bin

override_dh_clean:
	rm -rf google_compute_engine.egg-info gtest
	dh_clean
