#!/usr/bin/make -f

ifeq "$(DEB_BUILD_ARCH)" "armel"
	ARCH_SPECIFIC_OPTIONS = -DENABLE_NEON=OFF
	export DEB_LDFLAGS_MAINT_APPEND = -latomic
endif

ifneq (,$(filter armel armhf arm64,$(DEB_HOST_ARCH)))
	ARCH_SPECIFIC_OPTIONS += -DAPP_RENDER_SYSTEM=gles
else
	ARCH_SPECIFIC_OPTIONS += -DAPP_RENDER_SYSTEM=gl
endif

# ffmpeg version :
# grep VERSION= tools/depends/target/ffmpeg/FFMPEG-VERSION
# 4.4.1 for 20.0

%:
	dh $@ --with quilt -Scmake+ninja

override_dh_auto_configure:
	dh_auto_configure -- \
	-DCMAKE_BUILD_TYPE=RelWithDebInfo \
	-DENABLE_AIRTUNES=ON \
	-DENABLE_ALSA=ON \
	-DENABLE_AVAHI=ON \
	-DENABLE_BLURAY=ON \
	-DENABLE_CCACHE=OFF \
	-DENABLE_CEC=ON \
	-DENABLE_DBUS=ON \
	-DENABLE_DEBUGFISSION=OFF \
	-DENABLE_DVDCSS=ON \
	-DENABLE_EVENTCLIENTS=ON \
	-DENABLE_INTERNAL_CROSSGUID=OFF \
	-DENABLE_INTERNAL_FFMPEG=OFF \
	-DENABLE_INTERNAL_KISSFFT=OFF \
	-DENABLE_INTERNAL_RapidJSON=OFF \
	-DENABLE_LIRCCLIENT=ON \
	-DENABLE_MARIADBCLIENT=ON \
	-DENABLE_MICROHTTPD=ON \
	-DENABLE_NFS=ON \
	-DENABLE_OPTICAL=ON \
	-DENABLE_PULSEAUDIO=ON \
	-DENABLE_UDEV=ON \
	-DENABLE_UPNP=ON \
	-DENABLE_VAAPI=ON \
	-DENABLE_VDPAU=ON \
	-DENABLE_XSLT=ON \
	-Wno-dev \
	$(ARCH_SPECIFIC_OPTIONS)

#	exit 1

override_dh_clean:
	dh_clean lib/cpluff/console/cpluff-console lib/cpluff/loader/cpluff-loader \
	lib/cpluff/Makefile.in lib/cpluff/auxliary/ltmain.sh lib/cpluff/config.log \
	lib/cpluff/config.status lib/cpluff/console/Makefile.in lib/cpluff/examples/cpfile/cpfile \
	lib/cpluff/libcpluff/Makefile.in lib/cpluff/libcpluff/libcpluff.la lib/cpluff/libtool \
	lib/cpluff/loader/Makefile.in lib/cpluff/po/Makefile lib/cpluff/po/Makefile.in \
	tools/TexturePacker/TexturePacker \
	debian/*.so kodi_build_* tools/depends/native/TexturePacker/.installed-native

	find -name .deps | xargs -r rm -rf
	find -name .libs | xargs -r rm -rf
	find -name *.exe -delete
	find -name *.chm -delete
	find -name *.dll -delete

	find -name *.o -delete
	find -name *.lo -delete

	find -name Makefile -exec grep -l "automake-1" {} \; | xargs -r rm

	[ ! -f tools/depends/native/TexturePacker/native/Makefile ] || $(MAKE) -C tools/depends/native/TexturePacker/native distclean

override_dh_install:
	rm -rf debian/tmp/usr/share/doc/kodi/kodi-eventclients-dev/examples
	rm debian/tmp/usr/share/kodi/media/Fonts/arial.ttf
	rm debian/tmp/usr/share/kodi/system/settings/darwin*.xml
	rm debian/tmp/usr/share/kodi/system/settings/android.xml
	rm debian/tmp/usr/share/kodi/system/settings/win32.xml
	rm debian/tmp/usr/share/doc/kodi/version.txt
	rm debian/tmp/usr/share/doc/kodi/LICENSE.md
	rm debian/tmp/usr/share/doc/kodi/README.Linux.md

	find debian/tmp -name copying.txt -delete

	rm debian/tmp/usr/share/kodi/addons/skin.estuary/fonts/*.txt

#	find debian/tmp/usr -name '*.in' | xargs -r rm

	dh_install

override_dh_makeshlibs:
	dh_makeshlibs -n

override_dh_shlibdeps: debian/kodi-suggests-dummy.so
	dh_shlibdeps -lusr/lib/$(DEB_HOST_MULTIARCH)/kodi/system/players/dvdplayer

#	dpkg-shlibdeps -dRecommends -edebian/kodi-recommends-dummy.so -xlibc6 -O >> debian/kodi.substvars
	dpkg-shlibdeps -dSuggests -edebian/kodi-suggests-dummy.so -xlibc6 -O >> debian/kodi.substvars

# Nothing as 01/11/2021
#debian/kodi-recommends-dummy.so:
# See obj-*/build/DllPaths_generated.h after a call to dh_auto_configure

#	cc -xc -shared -Wl,--no-as-needed -o $@ /dev/null \
#	-lplist -lshairplay

debian/kodi-suggests-dummy.so:
	cc -xc -shared -Wl,--no-as-needed -o $@ /dev/null \
	-ldvdcss

override_dh_gencontrol:
	debian/dh-addon/dh_kodiaddon_depends
	dh_gencontrol

override_dh_strip:
ifeq "$(DEB_BUILD_ARCH)" "arm64"
	objcopy -S debian/kodi/usr/lib/$(DEB_HOST_MULTIARCH)/kodi/kodi.bin
endif

	dh_strip

override_dh_dwz override_dh_auto_test:
