#!/usr/bin/make -f

#For our custom rules on all mythtv packages
# get-orig-source
# info
# newest-revision
include debian/mythtv.make

#build goes here
buildroot       := debian/tmp

# Set this to yes for releases
MYTHTV_RELEASE ?= no
ifeq (yes,$(MYTHTV_RELEASE))
MYTHTV_COMPILE_TYPE = release
else
MYTHTV_COMPILE_TYPE = profile
endif

# Use the 'profile' compile type when nostrip or noopt is used
ifneq (,$(filter nostrip noopt,$(DEB_BUILD_OPTIONS)))
MYTHTV_COMPILE_TYPE = profile
endif

## Export QT version explicitely (fixes issues for non x86/arm builders)
export QT_SELECT = 5

#                                      #
# Architecture Independent Build flags #
#                                      #
MYTHTV_CONFIGURE_OPTS +=    --compile-type=$(MYTHTV_COMPILE_TYPE) \
				--prefix=/usr \
				--runprefix=/usr \
				--enable-lirc \
				--enable-audio-alsa \
				--enable-audio-oss \
				--enable-dvb \
				--enable-ivtv \
				--enable-firewire \
				--enable-joystick-menu \
				--with-bindings=perl \
				--enable-ffmpeg-pthreads \
				--enable-pic \
				--perl-config-opts="INSTALLDIRS=vendor" \
				--enable-libvpx \
				--enable-sdl \
				--enable-libmp3lame \
				--enable-libx264 \
				--enable-vdpau

#                                    #
# Architecture dependent build flags #
#                                    #
DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq "$(DEB_BUILD_ARCH)" "armhf"
	MYTHTV_CONFIGURE_OPTS += --disable-opengl --disable-vaapi
else ifeq "$(DEB_BUILD_ARCH)" "arm64"
	MYTHTV_CONFIGURE_OPTS += --disable-opengl --disable-vaapi
else ifeq "$(DEB_BUILD_ARCH)" "s390x"
	MYTHTV_CONFIGURE_OPTS += --disable-opengl --disable-vaapi
else ifeq "$(DEB_BUILD_ARCH)" "ppc64el"
	MYTHTV_CONFIGURE_OPTS += --disable-opengl --disable-vaapi
else
	MYTHTV_CONFIGURE_OPTS += --enable-vaapi
endif
ifeq "$(DEB_BUILD_ARCH)" "i386"
	MYTHTV_CONFIGURE_OPTS +=  --cpu=i686 --enable-mmx
endif
ifeq "$(DEB_BUILD_ARCH)" "powerpc"
	MYTHTV_CONFIGURE_OPTS += --enable-altivec --extra-cxxflags="-maltivec"
endif
#ifeq "$(DEB_BUILD_ARCH)" "armhf"
#        MYTHTV_CONFIGURE_OPTS += --extra-cflags="-I/opt/vc/include" --extra-cxxflags="-I/opt/vc/include" --extra-cflags="-I/opt/vc/include/IL" --extra-cxxflags="-I/opt/vc/include/IL" --extra-libs="-L/opt/vc/lib" --extra-cflags="-I/opt/vc/include/interface/vcos/pthreads" --extra-cxxflags="-I/opt/vc/include/interface/vcos/pthreads" --extra-cflags="-I/opt/vc/include/interface/vmcs_host/linux" --extra-cxxflags="-I/opt/vc/include/interface/vmcs_host/linux" --extra-libs="-lEGL"
#endif

#Mythplugins configure opts
MYTHPLUGINS_CONFIGURE_OPTS += --prefix=/usr \
                              --zm-version=1.22.3 \
                              --qmake=/usr/bin/qmake \

ifeq "$(DEB_BUILD_ARCH)" "armhf"
	MYTHPLUGINS_CONFIGURE_OPTS += --disable-opengl
endif

#Use moar CPUs for jya
PROCESSORS       ?= $(shell grep -ic ^processor /proc/cpuinfo)
ifeq (0,$(PROCESSORS))
PROCESSORS := 1
endif

#link problem with binutils 2.21 in precise
#and ffmpeg sync from 0.26, libswscale
LDFLAGS:=$(shell DEB_LDFLAGS_MAINT_STRIP="-Wl,-Bsymbolic-functions" dpkg-buildflags --get LDFLAGS)

# Need a writable home directory for ccache
export HOME=$(CURDIR)/debian/home_$(DEB_BUILD_ARCH)

%:
	dh $@ --parallel --with systemd

#Rather than have a configure/build/install phase, combine them all so we can do each part in order
override_dh_auto_install:
	if [ -f mythtv/EXPORTED_VERSION ] && [ -f debian/DESCRIBE ]; then \
		mv mythtv/EXPORTED_VERSION mythtv/EXPORTED_VERSION.old ;\
		cp debian/DESCRIBE mythtv/EXPORTED_VERSION ;\
	fi
	cd mythtv && sh version.sh `pwd`

	mkdir $(CURDIR)/debian/home

	#mythtv
	cd mythtv && ./configure $(MYTHTV_CONFIGURE_OPTS)
	$(MAKE) -j$(PROCESSORS) -C mythtv
	$(MAKE) -C mythtv install INSTALL_ROOT=$(CURDIR)/debian/tmp

	#mythplugins
	cd mythplugins && ./configure $(MYTHPLUGINS_CONFIGURE_OPTS) --mythroot=$(CURDIR)/debian/tmp:$(CURDIR)/debian/libmyth-python
	$(MAKE) -j$(PROCESSORS) -C mythplugins
	for plugin in mytharchive \
	mythgame mythmusic mythmovies mythnews \
	mythweather mythzoneminder mythbrowser ; do \
		$(MAKE) -C mythplugins/$$plugin install INSTALL_ROOT=$(CURDIR)/debian/$$plugin; \
	done

	#(MythTV) We replace these with shell scripts
	mv debian/tmp/usr/bin/mythfrontend debian/tmp/usr/bin/mythfrontend.real
	mv debian/tmp/usr/bin/mythtv-setup debian/tmp/usr/bin/mythtv-setup.real

	#(All) Make sure python/perl scripts are executable
	find debian/tmp/usr/share -type f \( -iname "*.p[y|l]" ! -iname "oauth_api.py" \) -size +1b -exec chmod 755 {} \;

	#Set up python package properly
	dh_python3 -plibmyth-python

override_dh_install:
	mkdir -p ${buildroot}/etc/php5/apache2/conf.d; \
	cp debian/20-mythweb.ini   ${buildroot}/etc/php5/apache2/conf.d; \
	mkdir -p ${buildroot}/etc/php/7.0/apache2/conf.d; \
	cp debian/20-mythweb.ini   ${buildroot}/etc/php/7.0/apache2/conf.d; \
	dh_install -Xusr/share/mythtv/fonts/Free \
                   -Xusr/share/mythtv/fonts/Purisa \
                   -Xusr/share/mythtv/fonts/texgyrechorus \
                   -Xusr/lib/libmythzmq.la \
                   -Xusr/share/mythtv/fonts/tiresias_gpl3.txt \
                   -XLICENSE \
                   -X.git \
                   -Xscriptaculous \
                   -Xprototype.js \
                   -Xjquery.min.js \
                   --fail-missing

override_dh_installinit:
	dh_installinit

	#(MythTV) Remove license files
	find debian/tmp -name "COPYING" -delete
	find debian/tmp -name "README.license" -delete

	#(MythTV) Set correct permissions for png and html files
	find debian/tmp -regex '.*\.png$$' -print0 | xargs -0 -r chmod 644
	find debian/tmp -regex '.*\.html$$' -print0 | xargs -0 -r chmod 644

override_dh_strip:
	dh_strip --dbg-package=mythtv-dbg

override_dh_compress:
	dh_compress -X.pl -X.py

override_dh_fixperms:
	dh_fixperms -X.pl -X.py
	#themes shouldn't be like this
	for type in png html txt jpg xml ; do \
		find debian -name "*.$$type" -type f -exec chmod -x {} \; ;\
	done

override_dh_auto_clean:
	[ -f mythtv/config.mak ] && $(MAKE) -C mythtv distclean || true
	[ -f mythplugins/config.pro ] && $(MAKE) -C mythplugins distclean || true
	[ -f mythtv/VERSION.old ] && mv mythtv/VERSION.old mythtv/VERSION || true
	dh_auto_clean
	debconf-updatepo
	rm -rf $(CURDIR)/debian/home

override_dh_installdocs:
	dh_installdocs
	#(MythTV) Remove license files from contrib documentation.
	find debian/mythtv-backend/usr/share/doc/mythtv-backend/contrib \
		-name "COPYING" -delete
	rm -f debian/mythtv-common/usr/share/mythtv/themes/Terra/watermarks/README.license

	#(MythWeb)
	for file in LICENSE README INSTALL; do \
		rm -f debian/mythweb/usr/share/mythtv/mythweb/$$file ;\
	done
	find debian/mythweb -name ".gitignore" -type f -delete || true
	rm -rf debian/mythweb/usr/share/mythtv/mythweb/data/tv_icons
	rm -rf debian/mythweb/usr/share/mythtv/mythweb/data/cache

	#(Myththemes)
	rm -f debian/mythtv-theme-childish/usr/share/mythtv/themes/Childish/COPYING

override_dh_installman:
	debian/generate_manpages.sh
	dh_installman
