#!/usr/bin/make -f
# -*- makefile -*-  

# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

#
# This version is for a multibinary package. It also allows you to build any
# of the binary packages independantly, via binary-<package> targets.

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

# This is the debhelper compatability version to use.
export DH_COMPAT=1

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

INST = install -p
package = avifile
version = 0.6
compile_flags = 
aviversion = $(package)$(version)
libraries = tmp
devel = libavifile$(version)-dev
player = $(package)-player
samples = $(package)-samples
madplugin = $(package)-mad-plugin
mjpegplugin = $(package)-mjpeg-plugin
vorbisplugin = $(package)-vorbis-plugin
win32plugin = $(package)-win32-plugin
mmxnow = libmmxnow
mmxnow-dev = libmmxnow-dev

installprefix = $(PWD)/debian/$(libraries)

allpackages= debian/$(player) debian/$(samples)

build: 
	$(checkdir)
	@echo "---=== Debian package is build with Lame support ===---"
	@echo "           library is being linked at runtime"
	@echo "for the full usage user has to install mp3lame library"
	./autogen.sh
#        rm config.cache
#	./configure --disable-tsc --datadir /usr/share --prefix $(prefix)
# we want to stay pentium/k6 compatible
#	CFLAGS="-g -O2 -march=i586" CXXFLAGS="-g -O2 -march=i586"
	./configure --enable-quiet --enable-release --enable-lame-bin --disable-ffmpeg-ac3 --prefix /usr
	nice -n 20 $(MAKE) -j 4
	touch build

cleanB:
	$(checkdir)
	dh_clean
#	rm -rf core `find debian/* -type d ! -name CVS`

clean: cleanB
	$(checkdir)
	@if [ -f Makefile ]; then $(MAKE) maintainer-clean ; fi
# remove various files which should not be in tar.gz
	@rm -rf core `find . -name "*~" -o -name Makefile.in -o -regex '.*\.bbg?' -o -name '*.gcov' -o -name '.#*' -o -name '*.out' `
	@rm -rf debian/docs .snprj avifile-0.6.0.*.proj
	@rm -f log build

binary-indep:
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch: cleanB checkscripts build
	$(checkdir)
	@echo ":: Preparing packages"
	@find doc -type f | egrep -v "CVS| |Makefile|cvsignore"  >debian/docs 
	
#	@$(INST) -d debian
	@dh_testdir
	@dh_testroot
	@dh_installdirs
	
# this has been all tricky way to fix some problems with makefile - no longer needed
#	@make DESTDIR=$(prefix) datadir=debian/$(player)/usr/share prefix=$(prefix) install
	@make DESTDIR=$(installprefix) install

	@pngtopnm $(installprefix)/usr/share/$(aviversion)/test.png | ppmtoxpm >$(installprefix)/usr/share/$(aviversion)/test.xpm

	@$(INST) -s $(installprefix)/usr/bin/aviplay debian/$(player)/usr/bin/

	@$(INST) -s -m 4755 $(installprefix)/usr/bin/kv4lsetup debian/$(samples)/usr/bin/
	@for i in avibench avirecompress avicap avicat avimake avitype ; do \
	   $(INST) -s $(installprefix)/usr/bin/$${i} debian/$(samples)/usr/bin/ ; done

#####
# !!LAME ENCODER REMOVEMENT FOR DEBIAN PACKAGE!!
#
# comment following 'rm' line to get full debian package
#
# note - we use wrapping plugin for mp3lame library instead - so better
# way is to install latest version of lame library from CVS instead
#####
	rm -f core $(installprefix)/usr/lib/$(aviversion)/libmp3lame_audioenc.*

	@$(INST) $(installprefix)/usr/bin/avifile-config debian/$(devel)/usr/bin/
	@mv $(installprefix)/usr/include debian/$(devel)/usr
	@mv $(installprefix)/usr/lib/*.so debian/$(devel)/usr/lib
	@mv $(installprefix)/usr/lib/$(aviversion)/libmad* debian/$(madplugin)/usr/lib/$(aviversion)/
	@mv $(installprefix)/usr/lib/$(aviversion)/libmjpeg* debian/$(mjpegplugin)/usr/lib/$(aviversion)/
	@mv $(installprefix)/usr/lib/$(aviversion)/libvorbis* debian/$(vorbisplugin)/usr/lib/$(aviversion)/
	@mv $(installprefix)/usr/lib/$(aviversion)/libwin32* debian/$(win32plugin)/usr/lib/$(aviversion)/

	@mv $(installprefix)/usr/share/$(aviversion) debian/$(player)/usr/share/

# remove for now 
	@rm $(installprefix)/usr/lib/libavqt*
	@rm $(installprefix)/usr/lib/libmmxnow*

# clean some files from library package
	@rm -rf debian/$(libraries)/usr/bin
	@rm -f debian/$(libraries)/usr/lib/*.so
	@rm -rf core `find debian/$(libraries) -name "*.a"`
	@mkdir -p debian/$(devel)/usr/share/lintian/overrides/
	@mkdir -p debian/$(samples)/usr/share/lintian/overrides/
	@cp -a debian/libavifile0.6-dev.lintian debian/$(devel)/usr/share/lintian/overrides/libavifile0.6-dev
	@cp -a debian/avifile-samples.lintian debian/$(samples)/usr/share/lintian/overrides/avifile-samples
	@$(INST) -m 0644 debian/aviplay.1 debian/$(player)/usr/share/man/man1

# disabled - lintian...
	dh_undocumented -p $(devel) avifile-config.1
	dh_undocumented -p $(samples) avicat.1
	dh_undocumented -p $(samples) avimake.1
	dh_undocumented -p $(samples) avitype.1
	dh_undocumented -p $(samples) avirecompress.1
	dh_undocumented -p $(samples) avicap.1
	dh_undocumented -p $(samples) kv4lsetup.1
	dh_undocumented -p $(samples) avibench.1
##	@ln -s ../man7/undocumented.7.gz debian/$(samples)/usr/share/man/man1/benchmark.1.gz

#	@chmod -R go=rX $(allpackages)

	@dh_installexamples
	dh_installdocs   
	dh_installchangelogs -k ChangeLog

# this way we are saving few inodes and making it more user friendly
# as doc is concentrated in one directory
# I don't like the idea to use .link file - this is much simplier
	@for i in $(player) $(samples) $(devel) $(madplugin) $(mjpegplugin) \
		$(vorbisplugin) $(win32plugin);\
	 do \
		rm -rf debian/$${i}/usr/share/doc/$${i} ;\
		ln -s libavifile0.6 debian/$${i}/usr/share/doc/$${i} ;\
        done
	dh_installmime
	dh_installmenu
	@dh_installcron
#	dh_installmanpages -ptmp
	@dh_movefiles
	dh_compress
	dh_fixperms -Xkv4lsetup
	dh_strip
	dh_shlibdeps
	dh_makeshlibs -V

#	@echo "libmpeg_audiodec-$(version) 0 lib$(aviversion)" >> debian/$(libraries)/DEBIAN/shlibs

# add svgalib dependency - not needed - SDL maintainer solved this problem
#	if ! grep svgalib-dummyg1 debian/substvars >/dev/null ; then \
#		mv debian/substvars debian/substvars-unhacked ; \
#		sed -e 's/svgalibg1\( *([^)]*)\)\{0,1\}/& | svgalib-dummyg1\1/'
#		rm debian/substvars-unhacked; \
#	fi

	dh_gencontrol
	dh_installdeb
	dh_md5sums
	dh_builddeb -v

define checkdir
	@test -f debian/rules
endef

checkscripts:
	@echo ":: Checking scripts"

# Below here is fairly generic really

binary: binary-indep binary-arch

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

.PHONY: binary binary-indep binary-arch clean checkroot
