#!/usr/bin/make -f
# debian/rules for the VLC media player - uses debhelper.
# $Id: rules 161 2007-02-21 15:19:07Z sam $
# based on Joey Hess's one.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
confflags = --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) 

#
# Compilation options
#
UBUNTU_ENV = MOZILLA_CONFIG=/usr/bin/firefox-config \
	XPIDL=/usr/lib/firefox/xpidl
vlc_confflags = --enable-release --prefix=/usr
vlc_confflags += \
	--enable-libtool \
	--disable-gnome \
	--disable-gtk \
	--disable-familiar \
	--disable-fb \
	--enable-ggi \
	--enable-sdl \
	--enable-esd \
	--disable-qt \
	--enable-mad \
	--enable-arts \
	--enable-alsa \
	--enable-lirc \
	--enable-a52 \
	--enable-aa \
	--enable-dvbpsi \
	--enable-mozilla \
	--disable-kde \
	--enable-mp4 \
	--enable-dvb \
	--enable-dv \
	--disable-satellite \
	--enable-ogg \
	--enable-vorbis \
	--enable-wxwidgets \
	--with-wx-config=wx-config \
	--disable-slp \
	--enable-flac \
	--disable-skins \
	--disable-basic-skins \
	--enable-skins2 \
	--enable-freetype \
	--enable-mkv \
	--enable-v4l \
	--enable-pvr \
	--enable-speex \
	--enable-caca \
	--enable-live555 \
	--enable-libmpeg2 \
	--enable-dts \
	--enable-fribidi \
	--enable-cdio \
	--enable-mod \
	--enable-theora \
	--enable-modplug \
	--enable-dvdnav \
	--enable-gnutls \
	--enable-ffmpeg \
	--enable-ncurses \
	--enable-smb \
	--disable-gnomevfs \
	--enable-bonjour \
	--enable-mpc \
	--enable-vcd \
	--enable-vcdx \
	--enable-xosd \
        --enable-notify \
	--enable-faad \
        --enable-mediacontrol-bindings \
	--enable-twolame \
	--enable-notify \
	$(NULL)

# These ones are currently shipped with VLC
vlc_confflags += --enable-faad --with-faad-tree=extras/faad2
vlc_confflags += --enable-x264 --with-x264-tree=extras/x264
vlc_confflags += --enable-twolame --with-twolame-tree=extras/twolame

# Glide and svgalib are only for x86
ifeq ($(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU),i386)
vlc_confflags += --enable-glide
vlc_confflags += --enable-svgalib
endif

# Remove --without-dvdcss in non-totalitarian countries
vlc_confflags += --enable-dvd --without-dvdcss

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


#
# Build rules
#

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Patch package
	QUILT_PATCHES=debian/patches quilt push -a || test $$? = 2
	# Touch stuff
	touch configure.ac
	touch aclocal.m4
	touch configure
	touch config.h.in
	touch `find . -name Makefile.in`
	# Check that we have an x264 tree in here (can be a symlink)
	test -d extras/x264
	cd extras/x264 \
	 && ./configure $(confflags) --extra-cflags='$$(ECFLAGS)' \
	                             --extra-asflags='$$(EASFLAGS)' \
	 && $(MAKE)
	# Check that we have an twolame tree in here (can be a symlink)
	test -d extras/twolame
	cd extras/twolame \
	 && sh autogen.sh && $(MAKE)
	 
	# Check that we have an faad2 tree in here (can be a symlink)
	test -d extras/faad2
	cd extras/faad2 \
	 && touch `find . -name configure.in` \
	 && touch `find . -name aclocal.m4` \
	 && touch `find . -name configure` \
	 && touch `find . -name config.h.in` \
	 && touch `find . -name Makefile.in` \
	 && ./configure $(confflags) --disable-shared \
	 && cd libfaad \
	 && $(MAKE)
	# Configure VLC
	$(UBUNTU_ENV) \
	./configure --mandir=$${prefix}/share/man \
	  --infodir=$${prefix}/share/info $(confflags) $(vlc_confflags)
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	#### Build only VLC, with the non-PIC libs
	###$(MAKE) include/vlc_symbols.h
	###cd modules && AM_MAKEFLAGS='plugin=no pic=no' $(MAKE)
	###$(MAKE) vlc
	# Hackety hackety hack
	cd extras/x264 && $(MAKE) clean && $(MAKE) ECFLAGS=-fPIC EASFLAGS=-D__PIC__
	# HACKETY HACKETY HACK
	cd extras/faad2/libfaad && $(MAKE) clean && $(MAKE) AM_CFLAGS=-fPIC
	#### Build all the rest, with the PIC libs
	###AM_MAKEFLAGS='builtin=no' $(MAKE)
	$(MAKE)

	# Minor test, to see which plugins were built and whether they load
	# properly.
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	./vlc --reset-plugins-cache -l -I rc vlc:quit
endif
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f configure-stamp build-stamp
	# Check that we have an x264 tree in here (can be a symlink)
	test -d extras/x264
	-cd extras/x264 && $(MAKE) distclean
	# Check that we have an faad2 tree in here (can be a symlink)
	test -d extras/faad2
	-cd extras/faad2 && $(MAKE) distclean
	# Clean VLC
	-$(MAKE) distclean
	# 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
	QUILT_PATCHES=debian/patches quilt pop -a -R || test $$? = 2
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=`pwd`/debian/tmp/

	# Clean up libtool crap
	find debian/tmp -name '*.la' -exec rm '{}' ';'

	# Remove useless stuff
	rm -Rf debian/tmp/$(VIDDIR)/skins
	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

	# 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
	-cp debian/vlc.desktop debian/vlc/$(APPDIR)/vlc.desktop

	# Install stuff
	dh_install -si --fail-missing --sourcedir=debian/tmp

	# Check that we did not install a plugin linked with libX11 in vlc-nox
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	BORKED=no; \
	for file in $$(find debian/vlc-nox/usr/lib/vlc -name '*.so'); do \
	  if ldd $$file | grep -q libX11; then \
	    BORKED=yes; \
	    echo $$file depends on libX11; \
	  fi; \
	done; \
	if test "$$BORKED" = yes; then exit 1; fi
endif

	# manpage symlinks
	ln -s vlc.1.gz debian/vlc/usr/share/man/man1/svlc.1.gz
	ln -s vlc.1.gz debian/vlc/usr/share/man/man1/wxvlc.1.gz

	# /usr/share/doc symlinks
	ln -s vlc debian/vlc-nox/usr/share/doc/vlc-nox
	ln -s libvlc0 debian/libvlc0-dev/usr/share/doc/libvlc0-dev
	ln -s vlc debian/vlc-plugin-esd/usr/share/doc/vlc-plugin-esd
	ln -s vlc debian/vlc-plugin-sdl/usr/share/doc/vlc-plugin-sdl
	ln -s vlc debian/vlc-plugin-ggi/usr/share/doc/vlc-plugin-ggi
	ln -s vlc debian/vlc-plugin-arts/usr/share/doc/vlc-plugin-arts
	ln -s vlc debian/mozilla-plugin-vlc/usr/share/doc/mozilla-plugin-vlc
ifeq ($(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU),i386)
	ln -s vlc debian/vlc-plugin-glide/usr/share/doc/vlc-plugin-glide
	ln -s vlc debian/vlc-plugin-svgalib/usr/share/doc/vlc-plugin-svgalib
endif

	# links for firefox
	mkdir -p debian/mozilla-plugin-vlc/usr/lib/mozilla-firefox/plugins/
	ln -s ../../mozilla/plugins/libvlcplugin.so debian/mozilla-plugin-vlc/usr/lib/mozilla-firefox/plugins/
	mkdir -p debian/mozilla-plugin-vlc/usr/lib/mozilla-firefox/components/
	ln -s ../../mozilla/components/vlcintf.xpt debian/mozilla-plugin-vlc/usr/lib/mozilla-firefox/components/

	cp debian/vlc/$(VIDDIR)/vlc.xpm debian/vlc/$(VIDDIR)/wxvlc.xpm

	# Clean up
	rm -f debian/vlc/$(VIDDIR)/*.png

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_link -i
	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_installdebconf	
	dh_installdocs -p vlc-nox
	dh_installdocs -p libvlc0
	dh_buildinfo -p vlc-nox
#	dh_installexamples
	dh_installmenu -s
	dh_installmime -s
	dh_installman doc/vlc.1 -p vlc-nox
	dh_installman doc/vlc-config.1 -p libvlc0-dev
	dh_installchangelogs ChangeLog -p vlc-nox
	dh_installchangelogs ChangeLog -p libvlc0
	dh_link -s
	dh_strip -s
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -plibvlc0 -V # -V is nasty, but I mean it
	dh_installdeb -s
	dh_shlibdeps -s -ldebian/libvlc0/usr/lib
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

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

