#!/usr/bin/make -f

ifeq (nostrip,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	DEBUG_BUILD=1
endif

ifeq (noopt,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	DEBUG_BUILD=1
endif
ifdef DEBUG_BUILD
	CONFIGURE_OPTS += --compile-type=debug
endif

ifeq (ccache,$(findstring ccache,$(PATH)))
	CONFIGURE_OPTS += --cc=/usr/lib/ccache/cc --cxx=/usr/lib/ccache/c++
endif

ifeq "$(DEB_BUILD_ARCH)" "i386"
	CONFIGURE_OPTS += --enable-vaapi
else
ifeq "$(DEB_BUILD_ARCH)" "amd64"
	CONFIGURE_OPTS += --enable-vaapi
else
ifeq "$(DEB_BUILD_ARCH)" "powerpc"
        CONFIGURE_OPTS += --disable-altivec
else
ifeq "$(DEB_BUILD_ARCH)" "armhf"
        CONFIGURE_OPTS += --disable-mmal
endif
endif
endif
endif

MYTH_BINARY_VERSION = $(shell grep SOURCE_VERSION SRC_VERSION | cut -d'"' -f2 | cut -d'v' -f2)

# ffmpeg version
# cat external/FFmpeg/RELEASE

ifneq "$(MYTH_BINARY_VERSION)" "35.0"
	exit 1
endif

%:
	dh $@ --with quilt,linktree -XREADME.license

override_dh_auto_configure:
	./configure \
	--arch=$(DEB_HOST_GNU_CPU) \
	--compile-type=release \
	--disable-ffprobe \
	--disable-qtwebkit \
	--enable-hdhomerun \
	--enable-libdav1d \
	--enable-libmp3lame \
	--enable-libvpx \
	--enable-libx264 \
	--enable-libx265 \
	--enable-libxvid \
	--enable-nonfree \
	--enable-pic \
	--enable-vulkan \
	--extra-cflags="$(CFLAGS)" \
	--libdir-name=lib/$(DEB_HOST_MULTIARCH) \
	--perl-config-opts=INSTALLDIRS=vendor \
	--prefix=/usr \
	--runprefix=/usr \
	--with-bindings="perl python php" \
	$(CONFIGURE_OPTS)

## --enable-libglslang
##	--enable-libmpeg2external \

ifeq "$(DEB_BUILD_ARCH)" "armhf"
	perl -i -pe 's,CONFIG_THUMB 0,CONFIG_THUMB 1,' config.h
endif

ifeq "$(DEB_BUILD_ARCH)" "armel"
	perl -i -pe 's,-DHAVE_CONFIG_H,-DHAVE_CONFIG_H -DHAVE_PTHREAD_H,' external/libudfread/Makefile
endif

#	exit 1

override_dh_auto_install:
	$(MAKE) install PREFIX=usr INSTALL_ROOT=$(CURDIR)/debian/tmp/

#	exit 1

# Not needed
	rm debian/tmp/usr/bin/mythwikiscripts
	find debian/tmp -name .npmignore -delete

	find debian/tmp/usr/share -type f -exec chmod 0644 {} \;

# Keep executable
	chmod +x debian/tmp/usr/share/mythtv/*.pl
	chmod +x debian/tmp/usr/share/mythtv/metadata/Television/ttvdb*.py
	chmod +x debian/tmp/usr/share/mythtv/metadata/Movie/tmdb*.py
	chmod +x debian/tmp/usr/share/mythtv/internetcontent/*.py
	chmod +x debian/tmp/usr/share/mythtv/internetcontent/*.pl
	chmod +x debian/tmp/usr/share/mythtv/metadata/Television/tmdb3tv.py
	chmod +x debian/tmp/usr/share/mythtv/metadata/Television/tvmaze.py
	chmod +x debian/tmp/usr/share/mythtv/hardwareprofile/sendProfile.py

override_dh_install:
	rm debian/tmp/usr/share/mythtv/fonts/*.txt

# Not needed. All svg are already in the primeicons.svg file
	rm -rf debian/tmp/usr/share/mythtv/html/3rdParty/primeicons-*/raw-svg
	rm -rf debian/tmp/usr/share/mythtv/html/3rdParty/primeicons-*/demo-files
	rm debian/tmp/usr/share/mythtv/html/3rdParty/primeicons-*/demo*
	rm debian/tmp/usr/share/mythtv/html/3rdParty/primeicons-*/CHANGELOG.md
	rm debian/tmp/usr/share/mythtv/html/3rdParty/primeicons-*/README.md
	rm debian/tmp/usr/share/mythtv/html/3rdParty/primeicons-*/package.json
	rm debian/tmp/usr/share/mythtv/html/3rdParty/primeicons-*/selection.json

	dh_install -X.a -XCOPYING -XLICENSE

	dh_install -pmythtv-frontend debian/mythtv.desktop usr/share/applications
	dh_install -XCOPYING -XLICENSE -pmythtv-backend debian/mythtv-setup.desktop usr/share/applications

	rm -f debian/mythtv/usr/share/mythtv/setup.xml

override_dh_installdocs:
	dh_installdocs -XCOPYING -A README debian/README.Debian AUTHORS

override_dh_strip:
ifneq (nostrip,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -a
endif

override_dh_installexamples-indep:
	dh_installexamples -pmythtv-doc contrib/*

override_dh_installinit:
	dh_installinit -u'defaults 40 16'

override_dh_compress-arch:
	dh_compress -X.py -X.pl

override_dh_fixperms-arch:
	dh_fixperms -X.py -X.pl

override_dh_clean:
	[ ! -f config.mak ] || $(MAKE) distclean

	dh_quilt_unpatch

	debconf-updatepo

	dh_clean debian/libmythtv-recommends-dummy.so \
	bindings/perl/Makefile.PL \
	bindings/python/MythTV.egg-info/* \
	bindings/python/MythTV/services_api/mythversions.py \
	bindings/python/MythTV/_versions.py \
	bindings/python/setup.cfg \
	config.fate \
	config.log \
	external/FFmpeg/doc/config.texi \
	external/FFmpeg/ffbuild/bin2c_host.d \
	external/FFmpeg/ffbuild/bin2c_host.o \
	external/FFmpeg/libavfilter/cuda/load_helper.d \
	external/FFmpeg/libavfilter/cuda/load_helper.o \
	external/FFmpeg/mythffmpeg \
	external/FFmpeg/mythffprobe \
	libs/libmythbase/mythversion.h \
	settings2.pro \
	src

	${RM} -rf android-package-source
	find -name moc -delete
	find -name obj -delete
	find -name .gitignore -delete
	find -name .gitattributes -delete

override_dh_shlibdeps: debian/libmythtv-recommends-dummy.so
	dh_shlibdeps

	dpkg-shlibdeps -dRecommends -edebian/libmythtv-recommends-dummy.so -xlibc6 -O >> debian/libmyth-35.substvars

debian/libmythtv-recommends-dummy.so:
	cc -xc -shared -Wl,--no-as-needed -o $@ /dev/null \
	-lcec \
	$(ARCH_SPECIFIC_LIB_RECOMMENDS)

override_dh_dwz override_dh_quilt_unpatch override_dh_auto_test override_dh_auto_clean:
