#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

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

#
# ok looks like this script is becoming a little bit overcomplicated
# however if anyone know how to simplify this let me know: kabi@debian.org
#

package = avifile
version = -0.7
aviversion = $(package)$(version)
libraries = lib$(aviversion)c2

# 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)

CFLAGS   := -Wall -g
CXXFLAGS := $(CFLAGS)
LDFLAGS  := -Wl,-z,defs -Wl,--as-needed

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
CXXFLAGS += -O0
else
CFLAGS += -O2 -ffast-math -fomit-frame-pointer
CXXFLAGS += -O2 -ffast-math -fomit-frame-pointer
endif

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

%:
	dh $@

override_dh_auto_clean:
	@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 build-stamp configure-stamp
	dh_clean acinclude.m4

override_dh_auto_configure:
	dh_testdir
	@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 --clean
	dh_auto_configure -- --prefix=/usr --without-lame \
	  --enable-lamebin --without-mad --enable-quiet --enable-release \
	  --without-qt \
	  --disable-dependency-tracking $(CROSS) CFLAGS="$(CFLAGS)" \
	  CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)"

override_dh_auto_test:

override_dh_auto_install:
	@echo ":: Preparing packages"
	dh_auto_install -- pkgdocdir=/usr/share/doc/lib$(aviversion)-common
	@rm -f debian/tmp/usr/share/doc/lib$(aviversion)-common/COPYING
	@rm -f debian/tmp/usr/share/doc/lib$(aviversion)-common/ChangeLog
	@chmod a+x debian/tmp/usr/share/$(aviversion)/scripts/avicap-*

override_dh_installdocs:
	dh_installdocs --link-doc=libavifile-0.7-common
