#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Ensure compatibility with GCC 15/16 (default -std=gnu23) for legacy jfs headers
export DEB_CFLAGS_MAINT_APPEND = -std=gnu17
export DEB_CPPFLAGS_MAINT_APPEND = -DHAVE_STDBOOL_H

# Check if PARTCLONE_STATIC is set to 1 OR if "static" is in DEB_BUILD_OPTIONS
ifneq (,$(filter static,$(DEB_BUILD_OPTIONS)))
    IS_STATIC = 1
endif
ifeq ($(PARTCLONE_STATIC),1)
    IS_STATIC = 1
endif

ifeq ($(IS_STATIC),1)
    PKG_NAME = partclone-static
    STATIC_OPT = --enable-static-linking
else
    PKG_NAME = partclone
    STATIC_OPT =
endif

# zfs support is the clean-room parser (liblz4/zlib only; static-safe).
# The CDDL-linked OpenZFS variant (--enable-openzfs) is intentionally not
# shipped in the Debian package.
ZFS_OPT = --enable-zfs

%:
	dh $@ --with autoreconf -p$(PKG_NAME)


FAILBOOTDIR = $(CURDIR)/debian/$(PKG_NAME)/usr/share/partclone
MAN8DIR = $(CURDIR)/debian/$(PKG_NAME)/usr/share/man/man8

# default configuration options
CONFIG_OPTS = \
	--prefix=/usr \
	--enable-ncursesw \
	--enable-xfs \
	--enable-extfs \
	--disable-reiserfs \
	--disable-reiser4 \
	--enable-hfsp \
	--enable-apfs \
	--enable-fat \
	--enable-exfat \
	--enable-ntfs \
	--enable-btrfs \
	--enable-minix \
	--enable-f2fs \
	$(ZFS_OPT) \
	$(STATIC_OPT)

# isal support architectures
ISAL_ARCHES = amd64 arm64 ppc64el riscv64 s390x

# ufs, vmfs, jfs, nilfs2 support architectures
EXTRA_FS_ARCHES = amd64 arm64

override_dh_auto_configure:
	ARCH=$$(dpkg-architecture -qDEB_HOST_ARCH); \
	EXTRA_OPTS=""; \
	if echo "$(ISAL_ARCHES)" | grep -w $$ARCH >/dev/null; then \
		EXTRA_OPTS="$$EXTRA_OPTS --enable-isal"; \
	fi; \
	if echo "$(EXTRA_FS_ARCHES)" | grep -w $$ARCH >/dev/null; then \
		EXTRA_OPTS="$$EXTRA_OPTS --enable-ufs --enable-vmfs --enable-jfs --enable-nilfs2"; \
	else \
		EXTRA_OPTS="$$EXTRA_OPTS --disable-ufs --disable-vmfs --disable-jfs --disable-nilfs2"; \
	fi; \
	dh_auto_configure -- $(CONFIG_OPTS) $$EXTRA_OPTS

override_dh_clean:
	dh_clean
	: # remove files generated by autogen and no removed by
	: # dh_clean
	rm -f depcomp missing compile
	rm -f Makefile.in
	rm -f aclocal.m4
	rm -f config.guess
	rm -f config.h.in
	rm -f config.h.in~
	rm -f config.rpath
	rm -f config.sub
	rm -f configure
	rm -f config.log
	rm -f install-sh
	rm -f ltmain.sh
	rm -f docs/Makefile.in
	rm -f src/Makefile.in
	rm -f tests/Makefile.in
	rm -f po/Makefile.in.in
	rm -rf m4/
	: # those files are modified upon build, so debuilding twice fails
	rm -f docs/partclone.8 docs/partclone.info.8
	rm -f po/*.gmo
	rm -f po/stamp-po
	rm -f docs/partclone.chkimg.8 \
	      docs/partclone.dd.8 \
	      docs/partclone.ntfsfixboot.8 \
	      docs/partclone.restore.8

override_dh_auto_test:
	echo Skipping tests.

override_dh_auto_install:
	dh_auto_install -p$(PKG_NAME) -- DESTDIR=$(CURDIR)/debian/$(PKG_NAME)
ifeq ($(PKG_NAME),partclone-static)
	# Rename all regular binary files from partclone.* to partclone-static.* using POSIX parameter expansion
	cd $(CURDIR)/debian/partclone-static/usr/sbin && \
	for f in partclone.*; do \
		if [ -f "$$f" ] && [ ! -L "$$f" ]; then \
			suffix="$${f#partclone.}"; \
			mv "$$f" "partclone-static.$$suffix"; \
		fi; \
	done
	# Remove old symlinks in sbin
	find $(CURDIR)/debian/partclone-static/usr/sbin -type l -delete
	# Recreate binary symlinks with the new names
	cd $(CURDIR)/debian/partclone-static/usr/sbin && \
	if [ -f partclone-static.extfs ]; then \
		ln -sf partclone-static.extfs partclone-static.ext2; \
		ln -sf partclone-static.extfs partclone-static.ext3; \
		ln -sf partclone-static.extfs partclone-static.ext4; \
		ln -sf partclone-static.extfs partclone-static.ext4dev; \
	fi && \
	if [ -f partclone-static.hfsp ]; then \
		ln -sf partclone-static.hfsp partclone-static.hfs+; \
		ln -sf partclone-static.hfsp partclone-static.hfsplus; \
	fi && \
	if [ -f partclone-static.ntfsfixboot ]; then \
		ln -sf partclone-static.ntfsfixboot partclone-static.ntfsreloc; \
	fi && \
	if [ -f partclone-static.fat ]; then \
		ln -sf partclone-static.fat partclone-static.fat12; \
		ln -sf partclone-static.fat partclone-static.fat16; \
		ln -sf partclone-static.fat partclone-static.fat32; \
		ln -sf partclone-static.fat partclone-static.vfat; \
	fi && \
	if [ -f partclone-static.vmfs ]; then \
		ln -sf partclone-static.vmfs partclone-static.VMFS_volume_member; \
		ln -sf partclone-static.vmfs partclone-static.vmfs3; \
	fi
	# Create partclone.* symlinks pointing to partclone-static.*
	cd $(CURDIR)/debian/partclone-static/usr/sbin && \
	for f in partclone-static.*; do \
		[ -e "$$f" ] || continue; \
		suffix="$${f#partclone-static.}"; \
		ln -sf "$$f" "partclone.$$suffix"; \
	done
	# Ensure the directory structures exist
	install -d $(FAILBOOTDIR)
	install -d $(MAN8DIR)
	# Rename man pages from partclone.* to partclone-static.* using POSIX parameter expansion
	cd $(MAN8DIR) && \
	for f in partclone.*; do \
		if [ -f "$$f" ] && [ ! -L "$$f" ]; then \
			suffix="$${f#partclone.}"; \
			mv "$$f" "partclone-static.$$suffix"; \
		fi; \
	done
	# Remove old symlinks for man pages
	find $(MAN8DIR) -type l -delete
	# Recreate symlinks for man pages
	cd $(MAN8DIR) && \
	  ln -s partclone-static.fat.8.gz partclone-static.vfat.8.gz; \
	  ln -s partclone-static.fat.8.gz partclone-static.fat12.8.gz; \
	  ln -s partclone-static.fat.8.gz partclone-static.fat16.8.gz; \
	  ln -s partclone-static.fat.8.gz partclone-static.fat32.8.gz; \
	  ln -s partclone-static.hfsp.8.gz partclone-static.hfs+.8.gz; \
	  ln -s partclone-static.hfsp.8.gz partclone-static.hfsplus.8.gz; \
	  ln -s partclone-static.extfs.8.gz partclone-static.ext2.8.gz; \
	  ln -s partclone-static.extfs.8.gz partclone-static.ext3.8.gz; \
	  ln -s partclone-static.extfs.8.gz partclone-static.ext4.8.gz; \
	  ln -s partclone-static.extfs.8.gz partclone-static.ext4dev.8.gz
	# Create partclone.* symlinks pointing to partclone-static.* for man pages
	cd $(MAN8DIR) && \
	for f in partclone-static.*; do \
		[ -e "$$f" ] || continue; \
		suffix="$${f#partclone-static.}"; \
		ln -sf "$$f" "partclone.$$suffix"; \
	done
	# docs/partclone.dd.8 is a particular case, see bug #850623
	rm -rf $(CURDIR)/debian/partclone-static/usr/share/man/dd
else
	# Ensure the directory structures exist
	install -d $(FAILBOOTDIR)
	install -d $(MAN8DIR)
	# Recreate symlinks for man pages
	cd $(MAN8DIR) && \
	  ln -s partclone.fat.8.gz partclone.vfat.8.gz || true; \
	  ln -s partclone.fat.8.gz partclone.fat12.8.gz || true; \
	  ln -s partclone.fat.8.gz partclone.fat16.8.gz || true; \
	  ln -s partclone.fat.8.gz partclone.fat32.8.gz || true; \
	  ln -s partclone.hfsp.8.gz partclone.hfs+.8.gz || true; \
	  ln -s partclone.hfsp.8.gz partclone.hfsplus.8.gz || true; \
	  ln -s partclone.extfs.8.gz partclone.ext2.8.gz || true; \
	  ln -s partclone.extfs.8.gz partclone.ext3.8.gz || true; \
	  ln -s partclone.extfs.8.gz partclone.ext4.8.gz || true; \
	  ln -s partclone.extfs.8.gz partclone.ext4dev.8.gz || true
	# docs/partclone.dd.8 is a particular case, see bug #850623
	rm -rf $(CURDIR)/debian/partclone/usr/share/man/dd
endif

override_dh_installman:
ifeq ($(PKG_NAME),partclone-static)
	# Man pages are already installed and renamed in override_dh_auto_install
else
	ARCH=$$(dpkg-architecture -qDEB_HOST_ARCH); \
	if echo "$(EXTRA_FS_ARCHES)" | grep -w $$ARCH >/dev/null; then \
		dh_installman -p$(PKG_NAME) \
			docs/partclone.fstype.8 \
			docs/partclone.jfs.8 \
			docs/partclone.nilfs2.8 \
			docs/partclone.ufs.8 \
			docs/partclone.vmfs.8 \
			docs/partclone.vmfs3.8 \
			docs/partclone.vmfs5.8; \
	else \
		dh_installman -p$(PKG_NAME); \
	fi
endif
