#!/usr/bin/make -f

#DH_VERBOSE=1

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
VERSION = $(shell  dpkg-parsechangelog|sed -n 's/^Version: //p')
DEBIAN_VERSION = $(shell echo $(VERSION)|sed -nr 's/[^:]+://; s/.*-([^-]+$$)/\1/p')

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
confflags := --build=$(DEB_BUILD_GNU_TYPE)
else
confflags := --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE)
endif

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
confflags += --disable-optimizations --disable-mmx --disable-sse --disable-altivec
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += -j$(NUMJOBS)
else
# on i386 and amd64, we query the system unless overriden by DEB_BUILD_OPTIONS
ifneq (,$(filter $(DEB_HOST_ARCH),i386 amd64))
NUMJOBS := $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
MAKEFLAGS += -j$(NUMJOBS)
endif
endif



include /usr/share/quilt/quilt.make

LDFLAGS = -Wl,--as-needed

# configure flags
confflags += --disable-maintainer-mode --enable-release --prefix=/usr
confflags += \
	--config-cache \
	--enable-fast-install \
	--with-binary-version=$(DEBIAN_VERSION) \
	--disable-update-check \
	--disable-fb \
	--enable-ggi \
	--enable-sdl \
	--enable-mad \
	--enable-jack \
	--enable-pulse \
	--enable-lirc \
	--enable-a52 \
	--enable-aa \
	--enable-dvbpsi \
	--enable-mozilla \
	--with-mozilla-pkg=xulrunner-plugin \
	--enable-dvb \
	--enable-ogg \
	--enable-vorbis \
	--enable-shout \
	--enable-qt4 \
	--enable-flac \
	--enable-skins2 \
	--enable-freetype \
	--enable-mkv \
	--enable-speex \
	--enable-caca \
	--enable-live555 \
	--enable-libmpeg2 \
	--enable-fribidi \
	--enable-mod \
	--enable-theora \
	--enable-dvdnav \
	--enable-gnutls \
	--enable-ncurses \
	--enable-smb \
	--disable-gnomevfs \
	--enable-bonjour \
	--enable-mpc \
	--enable-vcd \
	--enable-vcdx \
	--enable-notify \
	--enable-twolame \
	--enable-faad \
	--disable-zvbi \
	--enable-telx \
	--enable-x264 \
	--disable-atmo \
	--enable-taglib \
	--enable-libass \
	--enable-dca \
	--enable-realrtsp \
	--disable-dv \
	--disable-fluidsynth \
	--disable-kate \
	--disable-mtp \
	$(NULL)


# Linux specific flags
ifeq ($(DEB_HOST_ARCH_OS),linux)
confflags += --enable-alsa --enable-v4l --enable-v4l2 --enable-pvr
endif

# svgalib is only for x86 and x86-64 on Linux
ifneq ($(filter $(DEB_HOST_ARCH), amd64 i386),)
confflags += --enable-svgalib
endif

# A few shortcuts
VIDDIR := usr/share/vlc
PIXDIR := usr/share/pixmaps

build: build-stamp
build-stamp: $(QUILT_STAMPFN)
	dh_testdir
	# We need to build the static library apart
	# Else it's a mess when we build the modules
	./configure --enable-static $(confflags)
	$(MAKE) -C compat
	$(MAKE) -C src libvlccore.la libvlc.la
	mkdir -p tmp
	cp src/.libs/libvlccore.a tmp/libvlccore.a
	cp src/.libs/libvlc.a tmp/libvlc.a
	# build an altivec free version on powerpc for the G3
ifneq ($(filter $(DEB_HOST_ARCH), powerpc),)
	./configure --disable-altivec $(confflags)
	$(MAKE) -C compat
	$(MAKE) -C src libvlccore.la 
	cp -vd src/.libs/libvlccore.so.* tmp/
endif
	./configure $(confflags)
	$(MAKE)
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	# Check which plugins were built and whether they load properly.
	@if test $$( id -u ) -eq 0 ; then \
	   echo "Not runing the test as you are compiling as root"; \
	   echo "Use 'dpkg-buildpackage -rfakeroot' rather than 'fakeroot dpkg-buildpackage'"; \
	else \
	   command="./vlc -vvv --ignore-config --no-plugins-cache --list --no-color"; \
	   echo "$${command}"; $${command} ; \
	fi
endif
endif
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -f debian/vlc-nox.install.kfreebsd
	rm -f debian/vlc-nox.install.hurd
	# Clean VLC
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf tmp/
	# Clean zsh completion stuff
	rm -f extras/zsh
	# Removed ugly cruft
	rm -f src/misc/modules_builtin.h
	rm -f mozilla/vlcintf.xpt mozilla/vlcintf.h
	# Remove spurious autotools stuff
	rm -f config.log confdefs.h
	# Unpatch package
	$(MAKE) -f debian/rules unpatch
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	sed -e '/\(lib\|libaccess_\)\(alsa\|dvb\|v4l\|v4l2\|pvr\)_/d' \
	 debian/vlc-nox.install > debian/vlc-nox.install.kfreebsd
	sed -e '/\(lib\|libaccess_\)\(probe_hal\)_/d' \
	 debian/vlc-nox.install.kfreebsd > debian/vlc-nox.install.hurd
	dh_installdirs
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp/
	cp tmp/libvlc.a debian/tmp/usr/lib
	cp tmp/libvlccore.a debian/tmp/usr/lib
ifneq ($(filter $(DEB_HOST_ARCH), powerpc),)
	mkdir -p debian/tmp/usr/lib/altivec/
	mv -v debian/tmp/usr/lib/libvlccore.so.* debian/tmp/usr/lib/altivec/
	cp -vd tmp/libvlccore.so.* debian/tmp/usr/lib/
endif
	# Clean up libtool crap
	find debian/tmp -name '*.la' -exec rm '{}' ';'
	# Remove useless stuff
	rm -f debian/tmp/usr/share/vlc/skins2/fonts/FreeSans.ttf
	rm -f debian/tmp/usr/share/vlc/skins2/fonts/FreeSansBold.ttf
	ln -s /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf debian/tmp/usr/share/vlc/skins2/fonts/FreeSans.ttf
	ln -s /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf debian/tmp/usr/share/vlc/skins2/fonts/FreeSansBold.ttf
	rm -rf debian/tmp/usr/share/man/man1
	# Move stuff around
	mkdir -p debian/tmp/$(PIXDIR)
	mv debian/tmp/$(VIDDIR)/vlc48x48.png debian/tmp/$(PIXDIR)/vlc.png
	mv debian/tmp/$(VIDDIR)/vlc32x32.xpm debian/tmp/$(VIDDIR)/vlc.xpm
	# Install stuff
	dh_install -si --fail-missing --sourcedir=debian/tmp
	# move .hosts
	mkdir -p debian/vlc-data/etc/vlc/http
	mv debian/vlc-data/usr/share/vlc/http/.hosts debian/vlc-data/etc/vlc/http
	ln -s /etc/vlc/http/.hosts debian/vlc-data/usr/share/vlc/http/.hosts

	#install BTS stuff
	mkdir -p  debian/vlc-nox/usr/share/bug/vlc-nox
	cp debian/bug/* debian/vlc-nox/usr/share/bug/vlc-nox
	# Clean up
	rm -f debian/vlc/$(VIDDIR)/*.png
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	# Check that we did not install a plugin linked with libX11 in vlc-nox
	BORKED=no; \
	for file in $$(find debian/vlc-nox/usr/lib/vlc -name '*.so'); do \
	  if objdump -x $$file | egrep -q '^ +NEEDED +libX11\.so'; then \
	    BORKED=yes; \
	    echo $$file depends on libX11; \
	  fi; \
	done; \
	if test "$$BORKED" = yes; then exit 1; fi
endif

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_link -i
	dh_installdocs -p vlc-data
	dh_installchangelogs ChangeLog -p vlc-data
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -p vlc
	dh_installdocs -p vlc-nox
	dh_link -s
	dh_buildinfo -p vlc-nox
	dh_installmenu -s
	dh_installmime -s
	dh_installman doc/vlc.1 -p vlc-nox
	dh_installman doc/vlc-wrapper.1 -p vlc-nox
	dh_installchangelogs ChangeLog -p vlc
	dh_installchangelogs ChangeLog -p vlc-nox
	dh_lintian 
	dh_strip -s --dbg-package=vlc-dbg
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -plibvlc2 -V # -V is nasty, but I mean it
	dh_makeshlibs -plibvlccore2 -V # -V is nasty, but I mean it
	dh_installdeb -s
	dh_shlibdeps -s -ldebian/libvlc2/usr/lib:debian/libvlccore2/usr/lib
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch
.PHONY: build clean install binary-indep binary-arch binary
