# SPDX-License-Identifier: CDDL-1.0
PHONY += deb-kmod deb-dkms deb-utils deb deb-local native-deb-local \
	native-deb-utils native-deb-kmod native-deb

native-deb-local:
	@(if test "${HAVE_DPKGBUILD}" = "no"; then \
		echo -e "\n" \
	"*** Required util ${DPKGBUILD} missing.  Please install the\n" \
	"*** package for your distribution which provides ${DPKGBUILD},\n" \
	"*** re-run configure, and try again.\n"; \
		exit 1; \
	fi)

deb-local: native-deb-local
	@(if test "${HAVE_ALIEN}" = "no"; then \
		echo -e "\n" \
	"*** Required util ${ALIEN} missing.  Please install the\n" \
	"*** package for your distribution which provides ${ALIEN},\n" \
	"*** re-run configure, and try again.\n"; \
		exit 1; \
	fi; \
        if test "${ALIEN_MAJOR}" = "8" && \
           test "${ALIEN_MINOR}" = "95"; then \
        if test "${ALIEN_POINT}" = "1" || \
           test "${ALIEN_POINT}" = "2" || \
           test "${ALIEN_POINT}" = "3"; then \
                /bin/echo -e "\n" \
        "*** Installed version of ${ALIEN} is known to be broken;\n" \
        "*** attempting to generate debs will fail! See\n" \
        "*** https://github.com/openzfs/zfs/issues/11650 for details.\n"; \
                exit 1; \
        fi; \
        fi)

deb-kmod: deb-local rpm-kmod
	name=${PACKAGE}; \
	version=${VERSION}-${RELEASE}; \
	arch=`$(RPM) -qp $${name}-kmod-$${version}.src.rpm --qf %{arch} | tail -1`; \
	debarch=`$(DPKG) --print-architecture`; \
	pkg1=kmod-$${name}*$${version}.$${arch}.rpm; \
	fakeroot $(ALIEN) --bump=0 --scripts --to-deb --target=$$debarch $$pkg1 || exit 1; \
	$(RM) $$pkg1


deb-dkms: deb-local rpm-dkms
	name=${PACKAGE}; \
	version=${VERSION}-${RELEASE}; \
	arch=`$(RPM) -qp $${name}-dkms-$${version}.src.rpm --qf %{arch} | tail -1`; \
	debarch=`$(DPKG) --print-architecture`; \
	pkg1=$${name}-dkms-$${version}.$${arch}.rpm; \
	fakeroot $(ALIEN) --bump=0 --scripts --to-deb --target=$$debarch $$pkg1 || exit 1; \
	$(RM) $$pkg1

deb-utils: deb-local rpm-utils-initramfs
	name=${PACKAGE}; \
	version=${VERSION}-${RELEASE}; \
	arch=`$(RPM) -qp $${name}-$${version}.src.rpm --qf %{arch} | tail -1`; \
	debarch=`$(DPKG) --print-architecture`; \
	pkg1=$${name}-$${version}.$${arch}.rpm; \
	pkg2=libnvpair3-$${version}.$${arch}.rpm; \
	pkg3=libuutil3-$${version}.$${arch}.rpm; \
	pkg4=libzfs6-$${version}.$${arch}.rpm; \
	pkg5=libzpool6-$${version}.$${arch}.rpm; \
	pkg6=libzfs6-devel-$${version}.$${arch}.rpm; \
	pkg7=$${name}-test-$${version}.$${arch}.rpm; \
	pkg8=$${name}-dracut-$${version}.noarch.rpm; \
	pkg9=$${name}-initramfs-$${version}.$${arch}.rpm; \
	pkg10=`ls python3-pyzfs-$${version}.noarch.rpm 2>/dev/null`; \
	pkg11=`ls pam_zfs_key-$${version}.$${arch}.rpm 2>/dev/null`; \
## Arguments need to be passed to dh_shlibdeps. Alien provides no mechanism
## to do this, so we install a shim onto the path which calls the real
## dh_shlibdeps with the required arguments.
	path_prepend=`mktemp -d /tmp/intercept.XXXXXX`; \
	echo "#!$(SHELL)" > $${path_prepend}/dh_shlibdeps; \
	echo "`which dh_shlibdeps` -- \
	 -xlibuutil3linux -xlibnvpair3linux -xlibzfs6linux -xlibzpool6linux" \
	 >> $${path_prepend}/dh_shlibdeps; \
## These -x arguments are passed to dpkg-shlibdeps, which exclude the
## Debianized packages from the auto-generated dependencies of the new debs,
## which should NOT be mixed with the alien-generated debs created here
	chmod +x $${path_prepend}/dh_shlibdeps; \
	env "PATH=$${path_prepend}:$${PATH}" \
	fakeroot $(ALIEN) --bump=0 --scripts --to-deb --target=$$debarch \
	    $$pkg1 $$pkg2 $$pkg3 $$pkg4 $$pkg5 $$pkg6 $$pkg7 \
	    $$pkg8 $$pkg9 $$pkg10 $$pkg11 || exit 1; \
	$(RM) $${path_prepend}/dh_shlibdeps; \
	rmdir $${path_prepend}; \
	$(RM) $$pkg1 $$pkg2 $$pkg3 $$pkg4 $$pkg5 $$pkg6 $$pkg7 \
	    $$pkg8 $$pkg9 $$pkg10 $$pkg11;

deb: deb-kmod deb-dkms deb-utils

debian:
	cp -r contrib/debian debian; chmod +x debian/rules;

native-deb-utils: native-deb-local debian
	while [ -f debian/deb-build.lock ]; do sleep 1; done && \
	echo "native-deb-utils" > debian/deb-build.lock && \
	trap '$(RM) -f debian/deb-build.lock' EXIT && \
	cp contrib/debian/control debian/control && \
	$(DPKGBUILD) -b -rfakeroot -us -uc

native-deb-kmod: native-deb-local debian
	while [ -f debian/deb-build.lock ]; do sleep 1; done && \
	echo "native-deb-kmod" > debian/deb-build.lock && \
	trap '$(RM) -f debian/deb-build.lock' EXIT && \
	sh scripts/make_gitrev.sh && \
	fakeroot debian/rules override_dh_binary-modules

native-deb: native-deb-utils native-deb-kmod
