#!/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 loong64 riscv64 x32))
  LIBGAV1_FLAG := -DAVIF_CODEC_LIBGAV1=SYSTEM
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 i386 mips64el mipsel ppc64el s390x powerpc ppc64 riscv64))
  RAV1E_FLAG := -DAVIF_CODEC_RAV1E=SYSTEM
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), kfreebsd-amd64 kfreebsd-i386 m68k powerpc sh4 x32 hppa hurd-i386 loong64 sparc64))
  MAN_PAGES_FLAG := -DAVIF_BUILD_MAN_PAGES=OFF
endif


%:
	dh $@

execute_before_dh_auto_configure:
	# Strip embedded libyuv source code, we prefer library from OS
	rm -rfv third_party/libyuv/
	# Copy the libargparse component to the extracted location
	cp -rv libargparse ext/libargparse/

# rpath: check https://bugs.debian.org/1003371
#        drop when debhelper-compat >= 14
override_dh_auto_configure:
	dh_auto_configure -- \
	    -DAVIF_LIBYUV=SYSTEM \
	    -DAVIF_BUILD_APPS=ON \
	    -DAVIF_CODEC_DAV1D=SYSTEM \
	    -DAVIF_CODEC_AOM=SYSTEM \
	    -DAVIF_CODEC_SVT=SYSTEM \
	    $(LIBGAV1_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
