#!/usr/bin/make -f

# Handle the case when the clean rule is triggered before the patches are
# applied and the parent pom created
clean::
	[ -f pom.xml ] || touch pom.xml
	[ -f vision-proxy/pom.xml ] || touch vision-proxy/pom.xml

# CDBS + debian-maven-helper
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/maven.mk

# Honor dpkg-buildflags
export CPPFLAGS=$(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS=$(shell dpkg-buildflags --get CXXFLAGS)
export LDFLAGS=$(shell dpkg-buildflags --get LDFLAGS)

JAVA_HOME := /usr/lib/jvm/default-java
DEB_MAVEN_DOC_TARGET :=

# As I understand it, maven-debian-helper expect the jar / pom files to
# belong to only one binary package. Here we have two java binary packages.
# So we tell maven-debian-helper the java package name is tmp to install
# into debian/tmp.
DEB_JAR_PACKAGE := tmp

export VP_CONFIGURE_FLAGS=--build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)

configure/libsikuli-script-jni::
	[ -z "$(DEB_FAIL)" ]
	sed "s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g;s/@DEB_HOST_GNU_TYPE@/$(DEB_HOST_GNU_TYPE)/g" debian/libsikuli-script-jni.install.in > debian/libsikuli-script-jni.install

clean::
	-cd vision-proxy && chmod a+x compile  config.guess  config.sub  configure  depcomp  install-sh  ltmain.sh  missing runConfigure
	rm -f debian/libsikuli-script-jni.install
	cp debian/maven.ignoreRules.real debian/maven.ignoreRules
	[ -s pom.xml ] || rm pom.xml
	[ -s vision-proxy/pom.xml ] || rm vision-proxy/pom.xml

# ${maven:Depends} for each package
# Unsing specific maven.ignoreRules to avoid a failure at the first unresolved dependency
install/sikuli-ide::
	cp debian/maven.ignoreRules.resolv debian/maven.ignoreRules
	mh_resolve_dependencies --non-interactive --offline -psikuli-ide  --base-directory=$(CURDIR) --non-explore
	cp debian/maven.ignoreRules.real debian/maven.ignoreRules

install/libsikuli-script-java::
	cp debian/maven.ignoreRules.resolv debian/maven.ignoreRules
	mh_resolve_dependencies --non-interactive --offline -plibsikuli-script-java --javadoc --base-directory=$(CURDIR) --non-explore
	cp debian/maven.ignoreRules.real debian/maven.ignoreRules

# Fix perms after dh_install
binary-install/libsikuli-script-java::
	chmod a-x debian/libsikuli-script-java/usr/share/sikuli/Lib/sikuli/*.py \
		  debian/libsikuli-script-java/usr/share/sikuli/scripts/clean-dot-sikuli.py

# Generate doc the good old way
install/libsikuli-script-doc::
	javadoc -windowtitle "Javadoc of Sikuli Script" -sourcepath basics/src/main/java:jython/src/main/java:api/src/main/java -d debian/tmp/doc/ -classpath lib:/usr/share/java/jython.jar \
		-link /usr/share/doc/default-jdk-doc/api \
		-link /usr/share/doc/jython-doc/api-html \
		-link /usr/share/doc/junit-doc/doc/api \
		-link /usr/share/doc/libswing-layout-java-doc/api \
		-link /usr/share/doc/libjxgrabkey-java/api \
		org.sikuli.script
