#!/usr/bin/make -f

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

# Be specific about the version of autotools we use
export AUTOMAKE = automake-1.10
export ACLOCAL = aclocal-1.10

# 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)
configure_flags := --build $(DEB_BUILD_GNU_TYPE)
ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	configure_flags += --host $(DEB_HOST_GNU_TYPE)
endif
configure_flags += --enable-packager-mode --prefix=/usr

# Which bits of Gtk2Hs do we want?
configure_flags += --enable-cairo
configure_flags += --enable-gconf
configure_flags += --enable-gio
configure_flags += --enable-gnomevfs
configure_flags += --enable-gstreamer
configure_flags += --enable-gtk
configure_flags += --enable-svg
configure_flags += --enable-libglade
configure_flags += --enable-opengl
configure_flags += --enable-gtksourceview2
#configure_flags += --enable-mozilla   # not compatible with new xulrunner-dev yet
configure_flags += --enable-docs

# To build with ghc 6.12, --disable-split-objs is needed in all arches.
# http://haskell.org/gtk2hs/archives/2010/01/18/compiling-with-ghc-612/
# This can be fixed by changing the place where gtk2hs searches for the split
# objs, as described in
# http://www.mail-archive.com/gtk2hs-devel@lists.sourceforge.net/msg00511.html
configure_flags += --disable-split-objs

# from hlibrary.mk
GHC6_VERSION = $(shell ghc --numeric-version)
DEB_HADDOCK_DIR ?= /usr/share/ghc6-doc/ghc-$(GHC6_VERSION)/haddock/

MAKEFLAGS = "HADDOCK_FLAGS='--lib=${DEB_HADDOCK_DIR}'"

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS \
          = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

# -g removed to build in IA64
CFLAGS = -Wall
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
	configure_flags += --with-hcflags="-O"
else
	CFLAGS += -O2
	configure_flags += --with-hcflags="-O2"
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

PKGS \
  = glib \
  cairo \
  gconf \
  gio \
  gnomevfs \
  gstreamer \
  gtk \
  svgcairo \
  glade \
  gtkglext \
  gtksourceview2 \
  soegtk
VERSION := $(shell sed -ne '/([0-9.]\+-.*)/ { s/^.*(\([0-9.]\+\)-.*).*$$/\1/p; q }' < debian/changelog)

# configure --libdir: where the libs are temporarily installed (under
# debian/tmp) before moving them to the correct package build hierarchy.
# We need to set this explicitly for path mangling purposes.
TMP_LIBDIR = /usr/lib

# Helper macros
DEB = libghc6-$(1)-dev
PKGLIB_DIR = usr/lib/haskell-packages/ghc6/lib/$(1)-$(VERSION)
PKGLIB_BUILD = debian/$(call DEB,$(1))/$(call PKGLIB_DIR,$(1))
SHORT_HDEP = $(call PKGLIB_BUILD,$(1))/$(1).package.conf
HDEP = $(1):$(call SHORT_HDEP,$(1))

config.status: configure.ac
	dh_testdir
	cp -f /usr/share/misc/config.sub config.sub
	cp -f /usr/share/misc/config.guess config.guess
	autoreconf
	./configure $(configure_flags) --libdir=$(TMP_LIBDIR)

.PHONY: build
build: build-stamp
build-stamp: config.status
	dh_testdir
	$(MAKE) $(MAKEFLAGS)
	touch $@

.PHONY: install
install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k --exclude=build-stamp
	$(MAKE) $(MAKEFLAGS) install DESTDIR=$(CURDIR)/debian/tmp
	rm debian/tmp/usr/share/doc/gtk2hs/html/gtk2hs.haddock
	touch $@

pkgmeta \
  = pkgmeta-glib-stamp \
  pkgmeta-postinst-stamp \
  pkgmeta-prerm-stamp \
  debian/gtk2hs-doc.install

pkgmeta-%-stamp: debian/%.in
	for pkg in $(PKGS) ; do \
		sed -e "s#@PKGLIB_DIR@#$(call PKGLIB_DIR,$$pkg)#g; \
			s#@PKG@#$$pkg#g; \
			s#@VERSION@#$(VERSION)#g; \
			s#@TMP_LIBDIR@#$(TMP_LIBDIR)#g" \
			< $^ > debian/$(call DEB,$$pkg).$* ; \
	done
	touch $@

pkgmeta-glib-stamp: pkgmeta-install-stamp
	echo "debian/tmp$(TMP_LIBDIR)/gtk2hs/include $(call PKGLIB_DIR,glib)" \
	  >> debian/$(call DEB,glib).install
	touch $@

debian/gtk2hs-doc.install: debian/gtk2hs-doc.install.in
	sed -e "s#@DEB_HADDOCK_DIR@#$(DEB_HADDOCK_DIR)#g" < $^ > $@

.PHONY: clean clean-real
clean: clean-real
clean-real:
	dh_testdir
	dh_testroot
	[ ! -e Makefile ] || $(MAKE) $(MAKEFLAGS) clean distclean
	dh_clean aclocal.m4 Makefile.in configure config.guess config.sub \
	  config.log *-stamp debian/*-dev.install debian/*.postinst \
	  debian/*.prerm debian/dh_haskell_shlibdeps

# Build architecture-independent files here.
.PHONY: binary-indep
binary-indep: binary-indep-stamp
binary-indep-stamp: install-stamp $(pkgmeta)
	dh_testdir -i
	dh_testroot -i
	dh_clean -k --exclude=build-stamp --exclude=install-stamp --exclude=debian/tmp
	dh_installdirs -i
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_installexamples -i
	dh_install -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i
	touch $@

# Build architecture-dependent files here.
.PHONY: binary-arch
binary-arch: binary-arch-stamp
binary-arch-stamp: install-stamp $(pkgmeta)
	dh_testdir -a
	dh_testroot -a
	dh_clean -k --exclude=build-stamp --exclude=install-stamp --exclude=debian/tmp
	dh_installdirs -a
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	# move everything into place
	dh_install -a
	# mangle paths in $$pkg.package.conf
	for pkg in $(PKGS) ; do \
		sed --in-place -e \
			"s#$(TMP_LIBDIR)/gtk2hs/imports/$$pkg#/$(call PKGLIB_DIR,$$pkg)/imports#g; \
			 s#$(TMP_LIBDIR)/gtk2hs#/$(call PKGLIB_DIR,$$pkg)#g;" \
			$(call PKGLIB_BUILD,$$pkg)/$$pkg.package.conf ; \
	done
	# Correct gio include dir to use glibs
	sed --in-place -e \
		"s#$(call PKGLIB_DIR,gio)/include#$(call PKGLIB_DIR,glib)#g;" \
		$(call PKGLIB_BUILD,gio)/gio.package.conf
	dh_haskell_shlibdeps $(foreach pkg,$(PKGS),$(call SHORT_HDEP,$(pkg)))
	for pkg in $(PKGS) ; do \
		dh_haskell_depends \
			-p$(call DEB,$$pkg) \
			$(call SHORT_HDEP,$$pkg) ; \
	done
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a
	touch $@

.PHONY: binary
binary: binary-stamp
binary-stamp: binary-indep-stamp binary-arch-stamp
	touch $@