#!/usr/bin/make -f

# You must remove unused comment lines for the released package.
#export DH_VERBOSE = 1
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# that will fill DEB_VERSION and DEB_SOURCE variables
include /usr/share/dpkg/pkg-info.mk
export LIB_VERSION := $(shell echo $(DEB_VERSION) | cut -d'-' -f1)

# temporary build path (see http://golang.org/doc/code.html#GOPATH)
export GOPATH := $(CURDIR)/_build
export GOCACHE := $(CURDIR)/_cache
export GO111MODULE := on

%:
	dh $@ --buildsystem=golang --builddirectory=_build

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--warnings=6

override_dh_auto_configure:
	dh_auto_configure
	# Copy some files from upstream packaging
	cp -r deployments/systemd/packages/debian/postinst debian/nvidia-mig-manager.postinst
	cp -r deployments/systemd/packages/debian/postrm debian/nvidia-mig-manager.postrm

override_dh_auto_build:
	dh_auto_build -v -- \
		-buildmode=pie \
		-mod=vendor \
		-modcacherw \
		-ldflags="-extldflags=-Wl,-z,lazy"

	help2man --name="nvidia-mig-parted" --version-string="$(LIB_VERSION)" ./_build/bin/nvidia-mig-parted > nvidia-mig-parted.1
	help2man --name="nvidia-mig-manager" --version-string="$(LIB_VERSION)" ./_build/bin/nvidia-mig-manager > nvidia-mig-manager.1

override_dh_auto_install:
	# Install binaries only, skip source installation
	dh_auto_install -- --no-source

override_dh_fixperms:
	dh_fixperms
	chmod +x debian/nvidia-mig-manager/etc/nvidia-mig-manager/hooks.sh
	chmod +x debian/nvidia-mig-manager/etc/nvidia-mig-manager/service.sh
	chmod +x debian/nvidia-mig-manager/etc/nvidia-mig-manager/utils.sh

override_dh_installsystemd:
	# Do not start nvidia-mig-manager.service when installing this package
	dh_installsystemd --no-start

override_dh_dwz:
