#!/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-firewire \
				--enable-joystick-menu \
				--with-bindings=perl \
				--enable-ffmpeg-pthreads \
				--enable-pic \
				--perl-config-opts="INSTALLDIRS=vendor" \
				--enable-libvpx \
				--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 \
                              --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 python3

#Rather than have a configure/build/install phase, combine them all so we can do each part in order
override_dh_auto_install:
	# create the DESCRIBE file that Myth's version.sh will use to get the version
	DEB_VERSION="$(shell dpkg-parsechangelog -S version)" ;\
	GIT_VERSION="$(shell echo $(DEB_VERSION) | sed 's/[0-9]:/v/')" ;\
	GIT_MAJOR="$(shell echo $(DEB_VERSION) | sed -e 's/~ubuntu.*//' -e 's/.*://' -e 's/\..*//')" ;\
	GIT_BRANCH="$(shell echo $(DEB_VERSION) | sed -e 's/~ubuntu.*//' -e 's/.*[~+]//' -e 's/\..*//')" ;\
	if [ "$$GIT_BRANCH" = "fixes" ]; then \
		echo "BRANCH=\"$$GIT_BRANCH/$$GIT_MAJOR\"" > mythtv/DESCRIBE ;\
	else \
		echo "BRANCH=\"$$GIT_BRANCH\"" > mythtv/DESCRIBE ;\
	fi ;\
	echo "SOURCE_VERSION=\"$$GIT_VERSION\"" >> mythtv/DESCRIBE ;\

	cd mythtv && sh version.sh `pwd`

	install -m0755 -d $(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 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

override_dh_install:
	install -m0755 -d ${buildroot}/etc/php5/apache2/conf.d
	install -m0644 debian/20-mythweb.ini   ${buildroot}/etc/php5/apache2/conf.d
	install -m0755 -d ${buildroot}/etc/php/7.0/apache2/conf.d
	install -m0644 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

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

override_dh_compress:
	dh_compress -X.pl -X.py

override_dh_fixperms:
	dh_fixperms -X.pl -X.py

	#(All) Make sure python/perl scripts are executable
	find debian/tmp/usr/share -type f \( -iname "*.p[yl]" ! -iname "oauth_api.py" \) -size +1b -exec chmod 755 {} \;
	#(All) Remove execute permission from data files
	find debian/tmp \( -name '*.png' -o -name '*.html' -o -name '*.txt' -o -name '*.jpg' -o -name '*.xml' \) -exec chmod 644 {} +

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
