#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
export DEB_LDFLAGS_MAINT_APPEND =

include /usr/share/dpkg/architecture.mk

# only enable GAV1 on certain architectures
# sync with debian/control
LIBGAV1_FLAG := -DAVIF_CODEC_LIBGAV1=OFF

ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf i386 mips64el mipsel ppc64el s390x hppa ppc64 loong64 riscv64 x32))
  LIBGAV1_FLAG := -DAVIF_CODEC_LIBGAV1=ON
endif


# only enable SVT-AV1 on certain architectures
# sync with debian/control
SVT_FLAG := -DAVIF_CODEC_SVT=OFF

ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf mips64el mipsel ppc64el s390x hppa ia64 m68k powerpc ppc64 riscv64 sh4 sparc64))
  SVT_FLAG := -DAVIF_CODEC_SVT=ON
endif


# only enable RAV1E on certain architectures
# sync with debian/control
RAV1E_FLAG := -DAVIF_CODEC_RAV1E=OFF

ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armel armhf mips64el mipsel ppc64el s390x powerpc ppc64))
  RAV1E_FLAG := -DAVIF_CODEC_RAV1E=ON
endif


# flag for building doc (man page)
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
  MAN_PAGES_FLAG := -DAVIF_BUILD_MAN_PAGES=ON
else
  MAN_PAGES_FLAG := -DAVIF_BUILD_MAN_PAGES=OFF
endif

# disable doc build on architecture with broken pandoc, see also d/control
ifneq (,$(filter $(DEB_HOST_ARCH), i386 kfreebsd-amd64 kfreebsd-i386 m68k powerpc sh4 x32 hppa hurd-i386 loong64 sparc64))
  MAN_PAGES_FLAG := -DAVIF_BUILD_MAN_PAGES=OFF
endif


%:
	dh $@

# rpath: check https://bugs.debian.org/1003371
#        drop when debhelper-compat >= 14
override_dh_auto_configure:
	dh_auto_configure -- \
	    -DAVIF_BUILD_APPS=ON \
	    -DAVIF_CODEC_DAV1D=ON \
	    -DAVIF_CODEC_AOM=ON \
	    $(LIBGAV1_FLAG) \
	    $(SVT_FLAG) \
	    $(RAV1E_FLAG) \
	    -DAVIF_BUILD_GDK_PIXBUF=ON \
	    -DCMAKE_BUILD_RPATH_USE_ORIGIN=ON \
	    $(MAN_PAGES_FLAG) \
	    $(NULL)

# Manual man page installation for nodoc profile/option
# to avoid hard build-dependency on pandoc
execute_after_dh_install:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
ifeq ($(MAN_PAGES_FLAG),-DAVIF_BUILD_MAN_PAGES=ON)
	# Install fresh built man pages to libavif-bin package
	install -d $(CURDIR)/debian/libavif-bin/usr/share/man/man1/
	install -m644 debian/tmp/usr/share/man/man1/avifdec.1 debian/libavif-bin/usr/share/man/man1/
	install -m644 debian/tmp/usr/share/man/man1/avifenc.1 debian/libavif-bin/usr/share/man/man1/
	rm -f debian/tmp/usr/share/man/man1/avifdec.1
	rm -f debian/tmp/usr/share/man/man1/avifenc.1
else
	# Fallback when man pages needed but unable to build, use prebuilt man pages (may be outdated)
	install -d $(CURDIR)/debian/libavif-bin/usr/share/man/man1/
	install -m644 debian/avifdec.1 debian/libavif-bin/usr/share/man/man1/
	install -m644 debian/avifenc.1 debian/libavif-bin/usr/share/man/man1/
endif
endif
