#!/usr/bin/make -f

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

#                                      #
# Architecture Independent Build flags #
#                                      #
CONFIGURE_OPTS +=   --compile-type=debug \
					--prefix=/usr \
					--runprefix=/usr \
					--enable-lirc \
					--enable-audio-alsa \
					--enable-audio-oss \
					--enable-audio-jack \
					--enable-dvb \
					--enable-ivtv \
					--enable-firewire \
					--enable-joystick-menu \
					--enable-opengl-vsync \
					--enable-libfaad \
					--with-bindings=perl \
					--enable-opengl-video \
					--enable-ffmpeg-pthreads \
					--perl-config-opts="INSTALLDIRS=vendor"

#                                    #
# Architecture dependent build flags #
#                                    #
DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq "$(DEB_BUILD_ARCH)" "i386"
	CONFIGURE_OPTS +=  --cpu=i686 --enable-mmx --enable-xvmc --enable-xvmc-vld --enable-xvmc-pro --enable-glx-procaddrarb --enable-vdpau
endif
ifeq "$(DEB_BUILD_ARCH)" "sparc"
	CONFIGURE_OPTS += --enable-xvmc --enable-xvmc-vld --enable-xvmc-pro --extra-cflags="-mcpu=ultrasparc -mvis"
endif
ifeq "$(DEB_BUILD_ARCH)" "amd64"
	CONFIGURE_OPTS += --enable-xvmc --enable-xvmc-vld --enable-xvmc-pro --enable-glx-procaddrarb --enable-vdpau
endif
ifeq "$(DEB_BUILD_ARCH)" "armel"
	CONFIGURE_OPTS += --enable-vdpau --extra-cflags="-marm -fPIC -DPIC"
endif
ifeq "$(DEB_BUILD_ARCH)" "powerpc"
	CONFIGURE_OPTS += --enable-altivec --extra-cxxflags="-maltivec"
endif


%:
	dh $@

override_dh_auto_configure:
	#update SVN REVISION to show the right version
	sed -i -e "s/^\(\(str  *\)\?SOURCE_VERSION\)=.*$$/\1=$(SVN_REVISION)/g;" version.sh

	#Run configure
	./configure $(CONFIGURE_OPTS)

override_dh_auto_build:
	qmake -o Makefile PREFIX=/usr mythtv.pro
	dh_auto_build --parallel

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

	#Make sure python/perl scripts are executable
	find debian/tmp/usr/share -name "*.p[y|l]" -type f -exec chmod 755 {} \;

	#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_installinit:
	# The upstart script comes from contrib
#	cp contrib/Linux/init_scripts/upstart.mythtv-backend.conf debian/mythtv-backend.upstart
	dh_installinit

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

	#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_makeshlibs:
	dh_makeshlibs -V -Xusr/lib/mythtv/filters

override_dh_compress:
	dh_compress -X.pl -X.py

override_dh_fixperms:
	dh_fixperms -X.pl -X.py
	#On karmic we don't necessarily have scripts set executable (due to non-v3)
	find debian -name "*.sh" -type f -exec chmod 755 {} \;

override_dh_auto_clean:
	dh_auto_clean
	rm -f config.ep
	rm -f config/*
	debconf-updatepo

override_dh_installdocs:
	dh_installdocs
	#Remove license files from contrib documentation.
	find debian/mythtv-backend/usr/share/doc/mythtv-backend/contrib \
		-name "COPYING" -delete
