#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

export HOME=$(CURDIR)/fake-home

# debian package version
version=$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
# upstream version
gst_version=$(shell echo $(version) | cut -d '-' -f 1)

CFLAGS += -Wno-error
CXXFLAGS += -Wno-error
LDFLAGS += -Wl,-z,defs -Wl,-O1 -Wl,--as-needed

# Let's decide the package name and url depending on the distribution
DISTRO = "$(shell dpkg-vendor --query vendor)"

GST_PACKAGE_NAME := "GStreamer Bad Plugins (unknown Debian derivative)"
GST_PACKAGE_ORIGIN="http://packages.qa.debian.org/gst-plugins-bad1.0"

ifeq ($(DISTRO),"Debian")
GST_PACKAGE_NAME := "GStreamer Bad Plugins (Debian)"
GST_PACKAGE_ORIGIN="http://packages.qa.debian.org/gst-plugins-bad1.0"
endif

ifeq ($(DISTRO),"Ubuntu")
GST_PACKAGE_NAME := "GStreamer Bad Plugins (Ubuntu)"
GST_PACKAGE_ORIGIN="https://launchpad.net/distros/ubuntu/+source/gst-plugins-bad1.0"
endif

conf_flags = --libdir=lib/$(DEB_HOST_MULTIARCH) --libexecdir=lib/$(DEB_HOST_MULTIARCH)/gstreamer1.0
conf_flags += -Dpackage-name=$(GST_PACKAGE_NAME) -Dpackage-origin=$(GST_PACKAGE_ORIGIN)
conf_flags += -Dsctp-internal-usrsctp=enabled
conf_flags += -Dnvcodec=disabled
# libva doesn't build with the patches we need for the L5
ifeq ($(DEB_HOST_ARCH),arm64)
  conf_flags += -Dva=disabled
endif

hantro_patches = \
  debian/patches/hantro/Backport-h264-related-parts.patch \
  debian/patches/hantro/Don-t-fail-with-va-disabled.patch \
  debian/patches/hantro/Rank-h264codec-a-bit-higher.patch \
  $(NULL)

%:
	dh $@ --with gir --buildsystem=meson

override_dh_auto_configure:
ifeq ($(DEB_HOST_ARCH),arm64)
	for p in $(hantro_patches); do \
	  patch -p1 < $$p; \
	done;
	touch debian/hantro.stamp
endif
	dh_auto_configure -- --default-library shared $(conf_flags)

override_dh_auto_clean:
	rm -rf $(CURDIR)/fake-home
	if [ -f debian/hantro.stamp ]; then \
	  for p in $(hantro_patches); do \
	    patch -R -p1 < $$p; \
	  done; \
	fi
	rm -f hantro.stamp
	dh_auto_clean

override_dh_auto_test:
	xvfb-run dh_auto_test || true

override_dh_strip:
	dh_strip --dbgsym-migration='gstreamer1.0-plugins-bad-dbg (<< 1.17.1-1~)'

override_dh_makeshlibs:
	dh_makeshlibs -plibgstreamer-plugins-bad1.0-0 -X "/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0" -V 'libgstreamer-plugins-bad1.0-0 (>= $(gst_version))' -- -c4
	dh_makeshlibs -plibgstreamer-opencv1.0-0 -X "/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0" -V 'libgstreamer-opencv1.0-0 (>= $(gst_version))' -- -c4

override_dh_install:
	dh_install

ifeq ($(DEB_HOST_ARCH_OS),linux)
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstdvb.so
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstfbdevsink.so
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstdc1394.so
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstdecklink.so
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstuvch264.so
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstbluez.so
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstsbc.so
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstwaylandsink.so
	dh_install -plibgstreamer-plugins-bad1.0-0 debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libgstwayland-1.0.so.* usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstkms.so
	# Disabled until glslc is available in Debian
	# dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstvulkan.so
ifneq ($(DEB_HOST_ARCH),arm64)
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstva.so
endif
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstv4l2codecs.so
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstopenni2.so
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstsctp.so
ifeq ($(DEB_HOST_ARCH),amd64)
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstmsdk.so
endif
endif

ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstcdaudio.so
endif

ifneq ($(DEB_HOST_ARCH_OS),hurd)
	dh_install -pgstreamer1.0-plugins-bad debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/gstreamer-1.0/libgstresindvd.so
endif

	mkdir -p $(CURDIR)/fake-home
	HOME=$(CURDIR)/fake-home \
	LD_LIBRARY_PATH=debian/libgstreamer-plugins-bad1.0-0/usr/lib/$(DEB_HOST_MULTIARCH):debian/libgstreamer-opencv1.0-0/usr/lib/$(DEB_HOST_MULTIARCH):$(LD_LIBRARY_PATH) \
	    dh_gstscancodecs
	rm -rf $(CURDIR)/fake-home
