#!/usr/bin/make -f

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

LDFLAGS = -Wl,--as-needed

# configure flags
confflags += \
	--config-cache \
	--disable-maintainer-mode \
	--disable-silent-rules \
	--disable-update-check \
	--enable-fast-install \
	--prefix=/usr \
	--sysconfdir=/etc \
	--with-binary-version=$(DEBIAN_VERSION) \
	$(NULL)
# configure features
confflags += \
	--enable-a52 \
	--enable-aa \
	--enable-bonjour \
	--enable-caca \
	--enable-dca \
	--enable-dirac \
	--enable-dvb \
	--enable-dvbpsi \
	--enable-dvdnav \
	--enable-faad \
	--enable-flac \
	--enable-fluidsynth \
	--enable-freetype \
	--enable-fribidi \
	--enable-ggi \
	--enable-gnutls \
	--enable-jack \
	--enable-kate \
	--enable-libass \
	--enable-libmpeg2 \
	--enable-libproxy \
	--enable-libxml2 \
	--enable-lirc \
	--enable-live555 \
	--enable-mad \
	--enable-mkv \
	--enable-mod \
	--enable-mozilla \
	--enable-mpc \
	--enable-mtp \
	--enable-mux_ogg \
	--enable-ncurses \
	--enable-notify \
	--enable-ogg \
	--enable-pulse \
	--enable-qt4 \
	--enable-realrtsp \
	--enable-schroedinger \
	--enable-sdl \
	--enable-shout \
	--enable-skins2 \
	--enable-smb \
	--enable-speex \
	--enable-svg \
	--enable-taglib \
	--enable-theora \
	--enable-twolame \
	--enable-upnp \
	--enable-vcd \
	--enable-vcdx \
	--enable-vorbis \
	--enable-zvbi \
	--with-kde-solid=/usr/share/kde4/apps/ \
	--with-mozilla-pkg=libxul \
	$(NULL)
# Reasons for disabling features:
# dxva2 -> Windows only
# gnomevfs -> poorly maintained
# goom -> not in Debian
# libva -> needs a vaapi enabled avcodec
# osso_screensaver -> not targetting maemo
# portaudio -> not needed
# projectm -> broken
# sqlite -> still in development
# telx -> incompatible with zvbi
# x264 -> not in Debian
confflags += \
	--disable-dxva2 \
	--disable-gnomevfs \
	--disable-goom \
	--disable-libva \
	--disable-osso_screensaver \
	--disable-portaudio \
	--disable-projectm \
	--disable-sqlite \
	--disable-telx \
	--disable-x264 \
	$(NULL)

# Linux specific flags
ifeq ($(DEB_HOST_ARCH_OS),linux)
confflags += \
	--enable-alsa \
	--enable-atmo \
	--enable-dc1394 \
	--enable-dv \
	--enable-pvr \
	--enable-udev \
	--enable-v4l \
	--enable-v4l2 \
	$(NULL)
endif

ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
confflags += \
	--disable-alsa \
	$(NULL)
endif

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

build: build-stamp
build-stamp:
	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
	./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
	[ ! -f debian/vlc-nox.install.bak ] || mv -f debian/vlc-nox.install.bak \
        debian/vlc-nox.install
	rm -f debian/vlc-nox.install.kfreebsd
	rm -f debian/vlc-nox.install.hurd
	# Clean VLC
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf tmp/
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
# Add the optimization dirs only if present
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp/
	cp debian/vlc-nox.install debian/vlc-nox.install.bak
	for dir in sse2 3dnow altivec arm_neon mmx mmxext ; do \
	        if test -d debian/tmp/usr/lib/vlc/plugins/$${dir}; then \
	                echo usr/lib/vlc/plugins/$${dir} >> debian/vlc-nox.install ; \
	        fi ; \
	done
# Remove some modules on non-linux arch
	sed -e '/\(lib\|libaccess_\)\(alsa\|atmo\|dc1394\|dv\|dvb\|fb\|v4l\|v4l2\|pvr\|udev\)_/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
	cp tmp/libvlc.a debian/tmp/usr/lib
	cp tmp/libvlccore.a debian/tmp/usr/lib
	# Clean up libtool crap
	find debian/tmp -name '*.la' -exec rm '{}' ';'
	# Remove useless stuff
	ln -sf /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf debian/tmp/usr/share/vlc/skins2/fonts/FreeSans.ttf
	ln -sf /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf debian/tmp/usr/share/vlc/skins2/fonts/FreeSansBold.ttf
	rm -f debian/tmp/usr/share/man/man1/vlc-config.1
	# Install stuff
	dh_install -si --fail-missing
	# 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
	mkdir -p debian/vlc-data/etc/vlc/lua/http
	mv debian/vlc-data/usr/share/vlc/lua/http/.hosts debian/vlc-data/etc/vlc/lua/http
	ln -s /etc/vlc/http/.hosts debian/vlc-data/usr/share/vlc/lua/http/.hosts


ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	# Check that we did not install a plugin linked with libX11 or 
	# libxcb in vlc-nox
	BORKED=no; \
	for file in $$(find debian/vlc-nox/usr/lib/vlc -name '*.so'); do \
	  if objdump -x $$file | egrep -q -e '^ +NEEDED +libX11\.so' \
	                                  -e '^ +NEEDED +libxcb\.so'; then \
	    BORKED=yes; \
	    echo $$file depends on libX11 or libxcb; \
	  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 -s
	dh_installchangelogs ChangeLog -p vlc
	dh_installchangelogs ChangeLog -p vlc-nox
	dh_lintian 
	dh_bugfiles
	dh_strip -s --dbg-package=vlc-dbg
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -plibvlc5
	dh_makeshlibs -plibvlccore4
	dh_installdeb -s
	dh_shlibdeps -s -ldebian/libvlc5/usr/lib:debian/libvlccore4/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
