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

# 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)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

SRCDIR = $(CURDIR)/js/src

ifneq (,$(findstring $(DEB_BUILD_ARCH),armel))
        CONFIGURE_FLAGS += --disable-methodjit
endif

ifneq (,$(findstring $(DEB_BUILD_ARCH),armhf))
        export DEB_CFLAGS_MAINT_APPEND = -fno-schedule-insns
        export DEB_CXXFLAGS_MAINT_APPEND = -fno-schedule-insns
endif

%:
	dh $@ --sourcedirectory=$(SRCDIR) --with gnome,pkgkde-symbolshelper --without autoreconf

override_dh_clean:
	dh_clean
	find $(CURDIR)/js/src/ \( -type l  -o -name \*.pyc \) -exec rm {} \;
	rm -f $(CURDIR)/js/src/config/nsinstall
	rm -f $(CURDIR)/js/src/dist/bin/.purgecaches
	rm -f  $(CURDIR)/js/src/js52-config $(CURDIR)/js/src/mozjs-52.pc \
		$(CURDIR)/js/src/symverscript
	rm -f $(CURDIR)/js/src/config.log
	# rm -f $(CURDIR)/js/src/configure

# dh_auto_configure does not properly handle autoconf2.13 generated configure
# scripts, so we call configure ourselves.
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=570375
override_dh_auto_configure:
	cd $(SRCDIR) && SHELL=/bin/sh && PYTHON=python && $(SHELL) configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--libdir=/usr/lib/${DEB_HOST_MULTIARCH}/ \
		--without-system-icu \
		--enable-posix-nspr-emulation \
		--with-system-zlib \
		--enable-tests \
		--disable-strip \
		--enable-threadsafe \
		--with-intl-api \
		--enable-readline \
		--enable-xterm-updates \
		--enable-shared-js \
		--enable-gcgenerational \
		--disable-optimize \
		--enable-pie \
		$(CONFIGURE_FLAGS)

override_dh_install:
	# move library and create additional symlinks to standardize the file layout
	cd $(CURDIR)/debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}/ && \
		mv libjs_static.ajs libmozjs-52.a && \
		mv libmozjs-52.so libmozjs-52.so.0.0.0 && \
		ln -s libmozjs-52.so.0.0.0 libmozjs-52.so.0 && \
		ln -s libmozjs-52.so.0 libmozjs-52.so
	rm debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}/libmozjs-52.a
	dh_install --fail-missing

override_dh_fixperms:
	dh_fixperms
	chmod a-x $(CURDIR)/debian/libmozjs-52-dev/usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig/mozjs-52.pc

override_dh_gnome:
	dh_gnome --no-gnome-versions

# Don't fail the build on arches where the tests are known to fail
override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
ifeq (,$(filter $(DEB_HOST_ARCH),kfreebsd-i386 kfreebsd-amd64 ppc64el))
	make -C $(SRCDIR) -k check-jstests
else
	make -C $(SRCDIR) -k check-jstests || true
endif
endif
