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

VENDOR ?= $(shell dpkg-vendor --query vendor)

SRCDIR = $(CURDIR)/js/src

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

ifeq ($(VENDOR), Ubuntu)
ifneq (,$(findstring $(DEB_BUILD_ARCH),armel armhf))
        CONFIGURE_FLAGS += --enable-thumb2
endif
endif

%:
	dh $@ --sourcedirectory=$(SRCDIR) --parallel --with pkgkde-symbolshelper

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/js24-config $(CURDIR)/js/src/mozjs-24.pc \
		$(CURDIR)/js/src/symverscript
	rm -f $(CURDIR)/js/src/config.log

# 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 ./configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--libdir=/usr/lib/${DEB_HOST_MULTIARCH}/ \
		--with-system-nspr \
		--disable-tests \
		--disable-strip \
		--enable-ctypes \
		--enable-threadsafe \
		--enable-system-ffi \
		--disable-intl-api \
		$(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 libmozjs-24.so libmozjs-24.so.0.0.0 && \
		ln -s libmozjs-24.so.0.0.0 libmozjs-24.so.0 && \
		ln -s libmozjs-24.so.0 libmozjs-24.so
	dh_install --fail-missing

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

override_dh_strip:
	dh_strip -p libmozjs-24-0 --dbg-package=libmozjs-24-0-dbg
	dh_strip -p libmozjs-24-bin --dbg-package=libmozjs-24-bin-dbg
	dh_strip

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	make -C $(SRCDIR) -k check || true
endif

.PHONY: override_dh_clean override_dh_auto_configure override_dh_install \
	override_dh_strip override_dh_auto_test
