#!/usr/bin/make -f

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

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

GTK_VERSIONS = 2.0 3.0

CFLAGS = -g -Wall

ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -O2
else
        CFLAGS += -O0
endif

ifeq ($(DEB_BUILD_ARCH),alpha)
        CFLAGS += -Wl,--no-relax
endif

ifeq ($(DEB_BUILD_ARCH),s390)
        CFLAGS += -gstabs
endif

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

clean:
	dh_testdir
	dh_testroot

	rm -f build-stamp install-stamp
	-for version in $(GTK_VERSIONS); do \
		test -d build-$${version}/Source/WebKit/gtk/docs && \
			cd build-${version}/Source/WebKit/gtk/docs && \
			make maintainer-clean; \
	done

	-rm -rf Source/WebKit/gtk/docs/xml* Source/WebKit/gtk/docs/html* Source/WebKit/gtk/docs/sgml.stamp Source/WebKit/gtk/docs/tmpl*
	-for file in Source/WebKit/gtk/docs/webkitgtk.prerequisites Source/WebKit/gtk/docs/tmpl.stamp \
		Source/WebKit/gtk/docs/webkitgtk-overrides.txt Source/WebKit/gtk/docs/webkitgtk.args \
		Source/WebKit/gtk/docs/webkitgtk.interfaces Source/WebKit/gtk/docs/webkitgtk.signals \
		Source/WebKit/gtk/docs/webkitgtk.hierarchy Source/WebKit/gtk/docs/tmpl/webkitgtk-unused.sgml \
		Source/WebKit/gtk/docs/webkitgtk-decl-list.txt Source/WebKit/gtk/docs/webkitgtk-decl.txt \
		Source/WebKit/gtk/docs/webkitgtk-undeclared.txt Source/WebKit/gtk/docs/webkitgtk-undocumented.txt \
		Source/WebKit/gtk/docs/webkitgtk-unused.txt; do \
		rm -f ${file}; \
	done
	rm -rf build-*
	rm -rf debian/tmp-*
	rm -f Source/WebKit/gtk/docs/version.xml

	for file in config.guess config.sub; do \
		sed -i '2!b;/^exec "/ d' Source/autotools/$$file ; \
	done

	dh_clean

build: build-stamp

build-stamp:
	dh_testdir

	for file in config.guess config.sub; do \
		sed -i '2!b;/^#/ i\exec "/usr/share/misc/'$$file'" "$$@"' Source/autotools/$$file ; \
	done

	for version in $(GTK_VERSIONS); do \
		if [ ! -d build-$${version} ]; then \
			mkdir build-$${version}; \
		fi; \
		cd build-$${version}; \
		env CFLAGS="$(CFLAGS)" \
		CXXFLAGS="$(CFLAGS)" \
		CC="gcc -Wl,--as-needed" \
		CXX="g++ -Wl,--as-needed" \
		../configure --prefix=/usr \
			--host=$(DEB_HOST_GNU_TYPE) \
			--build=$(DEB_BUILD_GNU_TYPE) \
			--with-gtk=$${version} \
			--enable-gtk-doc \
			--enable-introspection \
			--disable-geolocation; \
		cd ..; \
	done

	for version in $(GTK_VERSIONS); do \
		$(MAKE) $(MAKEARGUMENTS) -C build-$${version}; \
	done

	# Awesome hack to get the docs built! For some reason, the
	# first call to make docs fails mid-work, but the second
	# succeeds.
	for version in $(GTK_VERSIONS); do \
		ln -fs ../../../../build-$${version}/Source/WebKit/gtk/docs/version.xml \
			Source/WebKit/gtk/docs/; \
		cd build-$${version}/Source/WebKit/gtk/docs; \
		make docs || make docs; \
		cd ../../../../..; \
	done

	touch $@

install: install-stamp

install-stamp: build-stamp
	dh_testdir
	dh_testroot

	dh_clean -k

ifeq (2.0,$(filter 2.0,$(GTK_VERSIONS)))
	$(MAKE) -C build-2.0 install DESTDIR="$(CURDIR)"/debian/tmp-2.0
	[ ! -d debian/tmp-2.0/usr/lib/webkitgtk-1.0-0/libexec ] && install -d -m 755 debian/tmp-2.0/usr/lib/webkitgtk-1.0-0/libexec || true
	build-2.0/libtool --mode=install install -m 755 build-2.0/Programs/GtkLauncher "$(CURDIR)"/debian/tmp-2.0/usr/lib/webkitgtk-1.0-0/libexec/GtkLauncher
	chrpath -d debian/tmp-2.0/usr/lib/webkitgtk-1.0-0/libexec/GtkLauncher

	install -d -m 755 debian/tmp-2.0/usr/share/doc/libwebkitgtk-dev/html
	install -m 644 build-2.0/Source/WebKit/gtk/docs/html/* \
		debian/tmp-2.0/usr/share/doc/libwebkitgtk-dev/html/

	mv debian/tmp-2.0/usr/share/webkit-1.0 \
		debian/tmp-2.0/usr/share/webkitgtk-1.0
endif

ifeq (3.0,$(filter 3.0,$(GTK_VERSIONS)))
	$(MAKE) -C build-3.0 install DESTDIR="$(CURDIR)"/debian/tmp-3.0
	[ ! -d debian/tmp-3.0/usr/lib/webkitgtk-3.0-0/libexec ] && install -d -m 755 debian/tmp-3.0/usr/lib/webkitgtk-3.0-0/libexec || true
	build-3.0/libtool --mode=install install -m 755 build-3.0/Programs/GtkLauncher "$(CURDIR)"/debian/tmp-3.0/usr/lib/webkitgtk-3.0-0/libexec/GtkLauncher
	chrpath -d debian/tmp-3.0/usr/lib/webkitgtk-3.0-0/libexec/GtkLauncher

	install -d -m 755 debian/tmp-3.0/usr/share/doc/libwebkitgtk-3.0-dev/html
	install -m 644 build-3.0/Source/WebKit/gtk/docs/html/* \
		debian/tmp-3.0/usr/share/doc/libwebkitgtk-3.0-dev/html/

	mv debian/tmp-3.0/usr/share/webkit-3.0 \
		debian/tmp-3.0/usr/share/webkitgtk-3.0
endif

	touch $@

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
ifeq (2.0,$(filter 2.0,$(GTK_VERSIONS)))
	dh_install -plibwebkitgtk-1.0-common --sourcedir=debian/tmp-2.0
endif
ifeq (3.0,$(filter 3.0,$(GTK_VERSIONS)))
	dh_install -plibwebkitgtk-3.0-common --sourcedir=debian/tmp-3.0
endif
	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_installdocs -a
	dh_installchangelogs -a
ifeq (2.0,$(filter 2.0,$(GTK_VERSIONS)))
	dh_install -plibwebkitgtk-1.0-0 --sourcedir=debian/tmp-2.0
	dh_install -plibwebkitgtk-dev --sourcedir=debian/tmp-2.0
	dh_install -plibwebkitgtk-1.0-0-dbg --sourcedir=debian/tmp-2.0
	dh_install -pgir1.2-webkit-1.0 --sourcedir=debian/tmp-2.0
endif
ifeq (3.0,$(filter 3.0,$(GTK_VERSIONS)))
	dh_install -plibwebkitgtk-3.0-0 --sourcedir=debian/tmp-3.0
	dh_install -plibwebkitgtk-3.0-dev --sourcedir=debian/tmp-3.0
	dh_install -plibwebkitgtk-3.0-0-dbg --sourcedir=debian/tmp-3.0
	dh_install -pgir1.2-webkit-3.0 --sourcedir=debian/tmp-3.0
endif
	dh_link -a
ifeq (2.0,$(filter 2.0,$(GTK_VERSIONS)))
	dh_strip -plibwebkitgtk-1.0-0 --dbg-package=libwebkitgtk-1.0-0-dbg
endif
ifeq (3.0,$(filter 3.0,$(GTK_VERSIONS)))
	dh_strip -plibwebkitgtk-3.0-0 --dbg-package=libwebkitgtk-3.0-0-dbg
endif
	dh_compress -a
	dh_fixperms -a
ifeq (2.0,$(filter 2.0,$(GTK_VERSIONS)))
	dh_makeshlibs -plibwebkitgtk-1.0-0 -V 'libwebkitgtk-1.0-0 (>= 1.3.9)' -- -c4
	dh_girepository --package=gir1.2-webkit-1.0
endif
ifeq (3.0,$(filter 3.0,$(GTK_VERSIONS)))
	dh_makeshlibs -plibwebkitgtk-3.0-0 -V 'libwebkitgtk-3.0-0 (>= 1.3.9)' -- -c4
	dh_girepository --package=gir1.2-webkit-3.0
endif
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

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