#!/usr/bin/make -f

CHMOD ?= chmod
INSTALL ?= install
MAKE ?= make

META_NAME := $(shell awk '$$1 == "Name:" { print $$2; }' META)
META_VERSION := $(shell awk '$$1 == "Version:" { print $$2; }' META)
META_DISTDIR := $(CURDIR)/$(META_NAME)-$(META_VERSION)/

%:
	dh $@ --with autoreconf,dkms

override_dh_auto_configure:
	@# Build the userland, but don't build the kernel modules.
	dh_auto_configure -- \
	  --bindir=/bin \
	  --sbindir=/sbin \
	  --libdir=/lib \
	  --with-udevdir=/lib/udev \
	  --with-config=user

override_dh_auto_test:
	# The dh_auto_test rule is disabled because
	# `make check` cannot run in an unprivileged build environment.

override_dh_auto_install:
	@# Install the utilities.
	$(MAKE) install DESTDIR='$(CURDIR)/debian/tmp'

	@# Zed has dependencies outside of the system root.
	$(INSTALL) -d '$(CURDIR)/debian/tmp/usr/sbin/'
	mv '$(CURDIR)/debian/tmp/sbin/zed' '$(CURDIR)/debian/tmp/usr/sbin/zed'

	@# Get a bare copy of the source code for DKMS.
	@# This creates the $(META_DISTDIR)/ tree.
	$(MAKE) distdir

	@# Delete components that are not required to build the kernel modules.
	@# This prevents several lintian complaints, discourages users from
	@# running `make install` in /usr/src, and reduces the installation
	@# size by one third. The printf creates a dummy automake file.
	umask 0111 && \
	cd '$(META_DISTDIR)' && \
	for ii in $$(find cmd dracut etc lib man rpm scripts udev -type f); do \
	  rm "$$ii"; \
	  test "$$ii" != "$${ii%.in}" && printf '%%:\n\t#\n' >"$$ii"; \
	  true; \
	done

	@# This shunt allows DKMS to copy the Module.symvers and zfs_config.h
	@# files into the ${dkms_tree} area through the POST_INSTALL directive.
	printf '#!/bin/sh\ncp "$$@"\n' >'$(META_DISTDIR)/cp'
	$(CHMOD) 755 '$(META_DISTDIR)/cp'

	@# Install the DKMS source directly, avoiding dh_install.
	$(INSTALL) -d '$(CURDIR)/debian/zfs-dkms/usr/src/'
	mv '$(META_DISTDIR)' '$(CURDIR)/debian/zfs-dkms/usr/src/'

override_dh_dkms:
	dh_dkms -V

override_dh_fixperms:
	dh_fixperms

	@# Prevent execute bits from being stripped by the Launchpad pbuilder.
	$(CHMOD) a+x '$(CURDIR)'/debian/zfsutils-linux/usr/share/initramfs-tools/hooks/*
	$(CHMOD) a+x '$(CURDIR)'/debian/zfs-initramfs/usr/share/initramfs-tools/hooks/*

override_dh_install:
	@# Exclude the upstream /usr/lib/udev/rules.d files.
	dh_install -X/rules.d/

	@# Ensure that /etc/zfs is in the zfsutils package
	@# because git has difficulty tracking empty directories.
	$(INSTALL) -d '$(CURDIR)/debian/zfsutils-linux/etc/zfs/'

	@# Resolve lintian errors regarding Debian Policy Manual Section 8.4
	@# by moving unversioned library links from /lib to /usr/lib in the
	@# development package. Look at libc6-dev for a packaging example if
	@# multiarch support ever becomes a concern here.
	$(INSTALL) -d '$(CURDIR)/debian/libzfslinux-dev/usr/lib/'
	cd '$(CURDIR)/debian/libzfslinux-dev/lib' && \
	for ii in $$(find -type l); do \
	  ln -s "../../lib/$$(readlink "$$ii")" "../usr/lib/$$(basename "$$ii")" \
	  && rm "$$ii"; \
	done

	# No need to ship .la files
	find . -name lib*.la -delete

override_dh_installdocs:
	dh_installdocs --link-doc=zfs-doc

override_dh_installinit:
	@# Ubuntu runs nfs-kernel-server at sysv sequence 20 80 and iscsitarget at 20 20.
	dh_installinit --name=zfs-share --no-restart-on-upgrade --no-start -- defaults 25 15

override_dh_installudev:
	dh_installudev --name=zvol --priority=60
	dh_installudev --name=vdev --priority=69

override_dh_strip:
	dh_strip -plibnvpair1linux --dbg-package=libnvpair1linux-dbg
	dh_strip -plibuutil1linux --dbg-package=libuutil1linux-dbg
	dh_strip -plibzfs2linux --dbg-package=libzfs2linux-dbg
	dh_strip -plibzpool2linux --dbg-package=libzpool2linux-dbg
	dh_strip -pzfs-zed --dbg-package=zfs-zed-dbg
	dh_strip -pzfsutils-linux --dbg-package=zfsutils-linux-dbg
