#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.
export DH_VERBOSE = 1

# See FEATURE AREAS in dpkg-buildflags(1).
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

export JAVA_HOME=/usr/lib/jvm/default-java
export CLASSPATH=/usr/share/java/libreoffice.jar:/usr/share/java/junit4.jar:$(CURDIR)/build

EXTPATH := usr/lib/libreoffice/share/extensions/h2orestart
TMPBUILDDIR := $(CURDIR)/debian/build/

%:
	dh $@ --with javahelper

override_jh_build:
	mkdir -p $(TMPBUILDDIR)
	jh_build --javacopts="-source 1.8 -target 1.8" --no-javadoc
	jar -uf $(TMPBUILDDIR)/H2Orestart.jar -C $(CURDIR)/build ebandal/libreoffice/H2Orestart.class
	jar -uf $(TMPBUILDDIR)/H2Orestart.jar -C $(CURDIR)/build ebandal/libreoffice/XH2Orestart.class
	mkdir -p $(TMPBUILDDIR)/ebandal/libreoffice/comp/
	echo "ebandal.libreoffice.comp.H2OrestartImpl" > $(TMPBUILDDIR)/ebandal/libreoffice/comp/RegistrationHandler.classes
	jar -uf $(TMPBUILDDIR)/H2Orestart.jar -C $(TMPBUILDDIR) ebandal/libreoffice/comp/RegistrationHandler.classes

        # Use manifest.xml from the upstream built .oxt file
	cd $(TMPBUILDDIR) && unzip -DD $(CURDIR)/dist/H2Orestart.oxt META-INF/manifest.xml


override_jh_manifest:
	env CLASSPATH="" jh_manifest

override_dh_auto_install:
	dh_auto_install
        # copy contents files written in ./package.properties
	for F in $(shell grep contents= $(CURDIR)/package.properties | sed -e 's/^contents=//' -e 's/, / /g'); do \
	  test $$F = "types.rdb" && continue; \
	  (test -d $$F || test -f $$F) || (echo "ERROR: Cannot find file or directory: $$F"; false) || exit 1; \
          test -d $$F && mkdir -p -m755 $(CURDIR)/debian/libreoffice-h2orestart/$(EXTPATH)/$$F && echo "Installed at: $(EXTPATH)/$$F/"; \
          test -f $$F && install -m644 $$F $(CURDIR)/debian/libreoffice-h2orestart/$(EXTPATH)/$$F && echo "Installed at: $(EXTPATH)/$$F"; \
	done
