#!/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/
XCU_LIST := $(sort $(foreach xcu,$(wildcard $(CURDIR)/registry/*.xcu),registry/$(notdir $(xcu))))

%:
	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

        # Build META-INF/manifest.xml
	[ -d $(TMPBUILDDIR)/META-INF ] || mkdir -p $(TMPBUILDDIR)/META-INF
	rm -f $(TMPBUILDDIR)/META-INF/manifest.xml
	echo '<?xml version="1.0" encoding="UTF-8"?>' >$(TMPBUILDDIR)/META-INF/manifest.xml
	echo '<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">' >>$(TMPBUILDDIR)/META-INF/manifest.xml
	echo '<manifest:file-entry manifest:full-path="description.xml" manifest:media-type="application/vnd.sun.star.package-bundle-description;locale=ko-KR"/>' >>$(TMPBUILDDIR)/META-INF/manifest.xml
	echo '<manifest:file-entry manifest:full-path="H2Orestart.jar" manifest:media-type="application/vnd.sun.star.uno-component;type=Java"/>' >>$(TMPBUILDDIR)/META-INF/manifest.xml
	for XCU in $(XCU_LIST); do \
	    echo '<manifest:file-entry manifest:full-path="'$$XCU'" manifest:media-type="application/vnd.sun.star.configuration-data"/>' >>$(TMPBUILDDIR)/META-INF/manifest.xml; \
	done
	echo '</manifest:manifest>' >>$(TMPBUILDDIR)/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
