#!/usr/bin/make -f

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

include /usr/share/dpkg/pkg-info.mk

export CLASSPATH=/usr/share/java/jackson-core.jar:/usr/share/java/jackson-annotations.jar:/usr/share/java/jackson-databind.jar:/usr/share/java/jna.jar:/usr/share/java/juh.jar:/usr/share/java/ridl.jar:/usr/share/java/unoil.jar
export JAVA_HOME=/usr/lib/jvm/default-java

%:
	dh $@ --with xul-ext,javahelper

override_dh_auto_build:
	./build.sh -p l -v ${DEB_VERSION} -d

	# Create the XPI file, filtering out the test subdirectory
	cp -a modules/zotero modules/zotero.new
	rm -rf modules/zotero.new/test
	xpi-pack modules/zotero.new xul-ext-zotero.xpi
	rm -rf modules/zotero.new

override_jh_build:
	jh_build -N staging/zotero-libreoffice-integration/build/oxt/Zotero.jar staging/zotero-libreoffice-integration/build/source
	# Add the RegistrationHandler.classes file
	jar uf staging/zotero-libreoffice-integration/build/oxt/Zotero.jar -C staging/zotero-libreoffice-integration/build/source org/zotero/integration/ooo/comp/RegistrationHandler.classes

override_dh_auto_install:
	rm -f staging/zotero-standalone/COPYING
	rm -f staging/zotero-standalone/xul-ext/zotero-standalone/COPYING
	rm -f staging/zotero-standalone/xul-ext/zotero-standalone/resource/tinymce/license.txt
	rm -f staging/zotero-libreoffice-integration/COPYING
	install-xpi -p xul-ext-zotero -r xul-ext-zotero.xpi

# We don't want to act on the jar in zotero-standalone (which is codeless)
override_jh_manifest:
	jh_manifest -plibreoffice-zotero-integration

# We don't want to act on the jar in zotero-standalone (which is codeless)
override_jh_depends:
	jh_depends -plibreoffice-zotero-integration

override_dh_auto_clean:
	rm -rf staging
	rm -f xul-ext-zotero.xpi

get-orig-source:
	rm -rf zotero-standalone-build-$(DEB_VERSION_UPSTREAM)
	git clone https://github.com/zotero/zotero-standalone-build.git zotero-standalone-build-$(DEB_VERSION_UPSTREAM)
	# Checkout right commit and submodules, and remove non-DFSG compliant files.
	cd zotero-standalone-build-$(DEB_VERSION_UPSTREAM) && \
		git checkout `echo $(DEB_VERSION_UPSTREAM) | sed 's/\+dfsg\d*//'` && \
		git submodule update --init --recursive && \
		git submodule deinit modules/zotero-word-for-mac-integration && \
		git submodule deinit modules/zotero-word-for-windows-integration && \
		rm -rf win/ mac/* linux/xulrunner-stub-* modules/zotero-libreoffice-integration/build/lib/*.jar modules/zotero-libreoffice-integration/install/Zotero_OpenOffice_Integration.oxt
	# Pack reproducible orig tarball
	tar -caf zotero-standalone-build_$(DEB_VERSION_UPSTREAM).orig.tar.xz \
		--format=pax --owner=root --group=root --mode='a=rX,u+w' --sort=name --exclude-vcs \
		--mtime=@`cd zotero-standalone-build-$(DEB_VERSION_UPSTREAM) && git log -n1 --format=%ct` \
		zotero-standalone-build-$(DEB_VERSION_UPSTREAM)
	rm -rf zotero-standalone-build-$(DEB_VERSION_UPSTREAM)
