#!/usr/bin/make -f

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

pkg_lib := $(shell dh_listpackages | head -n1)
pkg_lib_udeb := $(pkg_lib)-udeb
pkg_bin_udeb := libdirectfb-bin-udeb

pkgs_deb := $(shell dh_listpackages | sed -e '/$(pkg_lib)/d; /udeb/d; /dbg/d')
pkgs_dbg := $(shell dh_listpackages | grep dbg | sed -e 's|-dbg||')

DESTDIR_DIST := $(CURDIR)/debian/tmp
DESTDIR_UDEB := $(CURDIR)/debian/tmp-udeb

BUILD_DIST := directfb-build
BUILD_UDEB := directfb-udeb

conf_script_env := LDFLAGS="-Wl,--as-needed"

DEB_HOST_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  conf_gnu_type += --build $(DEB_HOST_GNU_TYPE)
else
  conf_gnu_type += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
  CROSS=LD=$(DEB_HOST_GNU_TYPE)-ld
endif

ifeq ($(DEB_HOST_ARCH_OS), linux)
  conf_options := \
    --with-gfxdrivers=all \
    --enable-video4linux2
  conf_options_udeb := \
    --with-gfxdrivers=matrox \
    --with-inputdrivers=keyboard,linuxinput,ps2mouse,serialmouse
else
  conf_options := \
    --with-gfxdrivers=none \
    --with-inputdrivers=ps2mouse
  conf_options_udeb := $(conf_options)
endif

%:
	dh $@ --parallel

override_dh_auto_configure:
	autoreconf -f -i

	mkdir -p $(BUILD_DIST)

	cd $(BUILD_DIST) && \
	$(conf_script_env) ../configure \
		$(conf_gnu_type) \
		--prefix=/usr \
		--includedir=\$${prefix}/include \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--sysconfdir=/etc \
		--localstatedir=/var \
		$(conf_options) \
		--enable-static \
		--enable-unique \
		--disable-sdl \
		--disable-vnc \
		--enable-x11

	mkdir -p $(BUILD_UDEB)

	cd $(BUILD_UDEB) && \
	$(conf_script_env) ../configure \
		$(conf_gnu_type) \
		--prefix=/usr \
		--includedir=\$${prefix}/include \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--sysconfdir=/etc \
		--localstatedir=/var \
		$(conf_options_udeb) \
		--disable-sdl \
		--disable-x11 \
		--disable-vnc \
		--disable-gif \
		--disable-jpeg \
		--disable-mpeg2 \
		--disable-unique \
		--disable-video4linux \
		--disable-mmx \
		--disable-sse

override_dh_auto_build:
	$(MAKE) $(CROSS) -C $(BUILD_DIST)
	$(MAKE) $(CROSS) -C $(BUILD_UDEB)

override_dh_clean:
	rm -rf $(BUILD_DIST)
	rm -rf $(BUILD_UDEB)

	find . -name 'Makefile.in' -delete
	rm -f configure config.* aclocal.m4 ltmain.sh INSTALL \
		missing mkinstalldirs depcomp install-sh compile

	rm -rf $(DESTDIR_UDEB)
	
	rm -f m4/libtool.m4
	rm -f m4/ltoptions.m4
	rm -f m4/ltsugar.m4
	rm -f m4/ltversion.m4
	rm -f m4/lt~obsolete.m4

	dh_clean

override_dh_auto_install:
	$(MAKE) $(CROSS) -C $(BUILD_DIST) install DESTDIR=$(DESTDIR_DIST)
	$(MAKE) $(CROSS) -C $(BUILD_UDEB) install DESTDIR=$(DESTDIR_UDEB)

override_dh_install:
	dh_install -a -N$(pkg_lib_udeb) -N$(pkg_bin_udeb) --sourcedir=$(DESTDIR_DIST)
	dh_install --remaining-packages --sourcedir=$(DESTDIR_UDEB)

override_dh_link:
	for pkg in $(pkgs_deb); do \
		rm -rf debian/$$pkg/usr/share/doc/$$pkg; \
		dh_link -p$$pkg usr/share/doc/$(pkg_lib) usr/share/doc/$$pkg; \
	done

override_dh_makeshlibs:
	dh_makeshlibs -a --add-udeb=$(pkg_lib_udeb)

override_dh_strip:
	for pkg in $(pkgs_dbg); do \
		dh_strip -p$$pkg --dbg-package=$${pkg}-dbg; \
	done
	dh_strip --remaining-packages

.PHONY: override_dh_auto_test
