#!/usr/bin/make -f

include /usr/share/quilt/quilt.make

# This has to be exported to make some magic below work.
export DH_OPTIONS

# do not run the actual rules of this makefile in parallel. sub-makes
# can go in parallel
.NOTPARALLEL:

############

DEB_SOURCE := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
DEB_VERSION := $(shell dpkg-parsechangelog | sed -n 's/^Version: //p')
UPSTREAM_VERSION := $(shell echo $(DEB_VERSION) | sed -r 's/[^:]+://; s/-[^-]+$$//')
SVN_VERSION := $(shell echo $(UPSTREAM_VERSION) | sed -nr 's/^[0-9.:-~]+svn([0-9]+)$$/\1/p')


#UPSTREAMSOURCE := upstream SVN repository
UPSTREAMSOURCE := http://www1.mplayerhq.hu/MPlayer/releases/MPlayer-1.0rc1.tar.bz2

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_ARCH_CPU   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)


# XXX enable-menu is frowned upon by upstream
DEB_BUILD_CONFIGURE = \
	--prefix=/usr \
	--confdir=/etc/mplayer \
	--enable-xvmc \
	--enable-menu \
	--enable-largefiles \
	--language=all \
	--disable-libdvdcss-internal \
	--disable-dvdread-internal \
	--disable-libavutil_a \
	--disable-libavcodec_a \
	--disable-libavformat_a \
	--disable-libpostproc_a \
	--disable-libswscale_a \
	--disable-real \
	--disable-xanim \
	--disable-qtx \
	--disable-win32dll \
	$(archconf)

ifeq ($(DEB_HOST_ARCH),i386)
#configure optimizes for the cpu detected at ./configure time
#in order to build a generic binary, avoid non-standard opcodes through gcc
  archconf += --target=i586-linux
  DEB_BUILD_CONFIGURE += --enable-runtime-cpudetection
endif
ifeq ($(DEB_HOST_ARCH),amd64)
  DEB_BUILD_CONFIGURE += --enable-runtime-cpudetection
endif
ifeq ($(DEB_HOST_ARCH),kfreebsd-i386)
  DEB_BUILD_CONFIGURE += --enable-runtime-cpudetection
endif
ifeq ($(DEB_HOST_ARCH),kfreebsd-amd64)
  DEB_BUILD_CONFIGURE += --enable-runtime-cpudetection
endif
ifeq ($(DEB_HOST_ARCH),powerpc)
  DEB_BUILD_CONFIGURE += --enable-runtime-cpudetection
endif
ifeq ($(DEB_HOST_ARCH),alpha)
#Avoid high optimization through gcc.
# see http://permalink.gmane.org/gmane.linux.debian.ports.alpha/7295
  archconf += --target=alpha_ev5-linux
endif

# generate debugging symbols for mplayer-dbg for all archs but mipsen,
# see bug #520113
ifeq (,$(findstring mips,$(DEB_HOST_ARCH)))
DEB_BUILD_CONFIGURE += --enable-debug
endif


ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
         NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
         MAKEFLAGS += -j$(NUMJOBS)
endif


ifeq (linux,$(DEB_HOST_ARCH_OS))
    DEB_BUILD_CONFIGURE += --enable-mga --enable-3dfx --enable-tdfxfb
endif

####### build-arch


# See http://wiki.debian.org/Teams/Dpkg/DebianRules; #465282  and
# https://wiki.ubuntu.com/DistCompilerFlags
CLEAN_ENV=env -u CFLAGS -u CPPFLAGS -u LDFLAGS -u FFLAGS -u CXXFLAGS


install-arch: $(QUILT_STAMPFN)
	dh_testdir
	dh_prep -a
	[ ! -f config.mak ] || $(MAKE) distclean
	echo "$(UPSTREAM_VERSION)" > snapshot_version
	$(CLEAN_ENV) \
	./configure $(COMMON_CONFIGURE_FLAGS) $(DEB_BUILD_CONFIGURE) --disable-gui
	$(CLEAN_ENV) \
	$(MAKE)
	# build non-gui version
	$(MAKE) install-mplayer DESTDIR=$(CURDIR)/debian/mplayer
	$(MAKE) install-mplayer-man DESTDIR=$(CURDIR)/debian/mplayer
	$(MAKE) install-mencoder-man DESTDIR=$(CURDIR)/debian/mplayer
	install -D -m 644 debian/mplayer.conf \
	                  $(CURDIR)/debian/mplayer/etc/mplayer/mplayer.conf
	install -D -m 644 debian/apport-hook.py \
	                  $(CURDIR)/debian/mplayer/usr/share/apport/package-hooks/source_mplayer.py
	#install mencoder
	$(MAKE) install-mencoder DESTDIR=$(CURDIR)/debian/mencoder
	# build gui version
	[ ! -f config.mak ] || $(MAKE) distclean
	$(CLEAN_ENV) \
	./configure $(COMMON_CONFIGURE_FLAGS) $(DEB_BUILD_CONFIGURE) --enable-gui
	$(CLEAN_ENV) \
	$(MAKE)
	install -D -m 755 mplayer $(CURDIR)/debian/mplayer-gui/usr/bin/gmplayer
	install -D -m 644 etc/mplayer.desktop \
	                  $(CURDIR)/debian/mplayer-gui/usr/share/applications/mplayer.desktop
	install -D -m 644 etc/mplayer.xpm \
	                  $(CURDIR)/debian/mplayer-gui/usr/share/pixmaps/mplayer.xpm

###### build-indep

# commands to compile the documentation
install-indep: install-indep-stamp
install-indep-stamp:
	dh_testdir
	sh debian/all-lang-config-mak.sh > config.mak
	cd DOCS/xml && ./configure
	$(MAKE) -C DOCS/xml html-chunked
	dh_installchangelogs -i
	dh_installdocs -i
	dh_compress -i
	dh_fixperms -i
	touch $@

################ clean

clean: clean-real unpatch
clean-real:
	dh_testdir
	dh_testroot
	[ ! -f config.mak ] || $(MAKE) -C DOCS/xml distclean || true
	[ ! -f config.mak ] || $(MAKE) distclean
	dh_clean build-arch-stamp configure-arch-stamp snapshot_version

# Build architecture-independent packages
binary-indep: install-indep

# Build architecture-dependent packages
binary-arch: install-arch
	dh_testdir
	dh_testroot
	dh_install -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installmime -a
	dh_installman -a
	dh_strip -a --dbg-package=mplayer-dbg
	dh_link
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

###############################################
##These targets generate the DFSG-free tar.gz.
## They are not automatically invoked.

get-orig-source:
	dh_testdir
	# strip patented code
	chmod +x debian/strip.sh
	sh debian/get-orig-source.sh -d $(SVN_VERSION)

PHONY += build clean binary-indep binary-arch binary-common binary 
PHONY += install binary binary-arch binary-indep clean checkroot get-orig-source
PHONY += autocontrol fix-orig-source copy-orig-from-svn download-and-unpack-orig
PHONY += build-gui build-nogui install-gui install-nogui

.PHONY: $(PHONY)
