#!/usr/bin/make -f

export JAVA_HOME=/usr/lib/jvm/default-java

%:
	dh --with xulrunner $@

# The renaming of org.apache.ant_* directory is done so that eclipse is built
# using the latest ant version in Debian.
override_dh_auto_build:
	test ! -d eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145 || \
		mv eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145 \
		eclipse/plugins/org.apache.ant_1.8.2.v20101227-1145
	ant build

override_dh_auto_clean:
	ant distclean
	mh_clean
	test ! -d eclipse/plugins/org.apache.ant_1.8.2.v20101227-1145 || \
		mv eclipse/plugins/org.apache.ant_1.8.2.v20101227-1145 \
		eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145

override_dh_auto_install:
	ant install -Dprefix=/usr -DdestDir=$(CURDIR)/debian/tmp

DEB_HOST_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
JNI_SO_PATH = debian/eclipse-rcp/usr/lib/eclipse/configuration/org.eclipse.osgi/bundles/*/1/.cp
SWT_SO_PATH := $(CURDIR)/debian/eclipse-rcp/usr/lib/eclipse/debian-swt
LAUNCHER_LIB = debian/eclipse-rcp/usr/lib/eclipse/plugins/org.eclipse.equinox.launcher.*/eclipse_*.so

override_dh_shlibdeps:
	dh_shlibdeps -peclipse-rcp -l$(JAVA_HOME)/jre/lib/$(DEB_HOST_ARCH_CPU) \
		-- -dDepends \
			$(SWT_SO_PATH)/libswt-gtk-*.so \
			$(SWT_SO_PATH)/libswt-atk-gtk-*.so \
			$(SWT_SO_PATH)/libswt-awt-gtk-*.so \
			$(SWT_SO_PATH)/libswt-cairo-gtk-*.so \
			$(SWT_SO_PATH)/libswt-pi-gtk-*.so \
			$(SWT_SO_PATH)/libswt-xpcominit-gtk-*.so \
			$(SWT_SO_PATH)/libswt-xulrunner-gtk-*.so \
			$(shell find $(JNI_SO_PATH) -name '*.so' | grep -v libswt-) \
			$(LAUNCHER_LIB) \
		-dRecommends \
			$(SWT_SO_PATH)/libswt-gnome-gtk-*.so \
			$(SWT_SO_PATH)/libswt-glx-gtk-*.so
	dh_shlibdeps --remaining-packages

# This has to go in an arch package, as some of the version numbers
# depend on which arch it was build one.
COPY_PLATFORM:=/usr/lib/eclipse/buildscripts/copy-platform
COPY_PLATFORM_INSTALL:=debian/tmp$(COPY_PLATFORM)
PDEBUILD_INSTALL:=debian/tmp/usr/lib/eclipse/buildscripts/pde-build
PDEBUILDVERSION = $(shell ls debian/tmp/usr/lib/eclipse/plugins | grep org.eclipse.pde.build_ | sed 's/org.eclipse.pde.build_//')

XULVERSION := $(shell pkg-config --modversion libxul)
XULVERSION_STRIP1 := $(shell echo $(XULVERSION) | sed "s/\.[^\.]*$$//")
XULVERSION_STRIP2 := $(shell echo $(XULVERSION_STRIP1) | sed "s/\.[^\.]*$$//")
XULRUNNER := $(shell if which xulrunner-$(XULVERSION) > /dev/null 2>&1; then which xulrunner-$(XULVERSION); \
	elif which xulrunner-$(XULVERSION_STRIP1) > /dev/null 2>&1; then which xulrunner-$(XULVERSION_STRIP1); \
	elif which xulrunner-$(XULVERSION_STRIP2) > /dev/null 2>&1; then which xulrunner-$(XULVERSION_STRIP2); fi)
XULRUNNER_PATH := $(shell if test -d /usr/lib/xulrunner-$(shell $(XULRUNNER) --gre-version 2> /dev/null); \
	then echo '/usr/lib/xulrunner-$$($(XULRUNNER) --gre-version)'; \
	else pkg-config --variable=sdkdir libxul | sed "s/-devel//"; fi)

override_dh_install:
	# move images from /usr/lib to /usr/share
	cd debian/tmp/usr && find lib/ -type f \( -name '*.bmp' -o -name '*.gif' -o -name '*.jpg' -o -name '*.png' \) | perl ../../../debian/extra/imagemv.pl
	# repack some jars
	debian/extra/repack_jars.sh debian/tmp/usr/lib/eclipse/plugins
	# remove extra licence file
	rm -f debian/tmp/usr/lib/eclipse/plugins/org.apache.ant_*/about_files/LICENSE.sax.txt \
		debian/tmp/usr/lib/eclipse/epl-v10.html \
		debian/tmp/usr/lib/eclipse/about_files/lgpl-v21.txt \
		debian/tmp/usr/lib/eclipse/about_files/mpl-v11.txt \
		debian/tmp/usr/lib/eclipse/about_files/webkit-bsd.txt \
		debian/tmp/usr/lib/eclipse/about_files/pixman-licenses.txt \
		debian/tmp/usr/lib/eclipse/about_files/about_cairo.html
	# wrapper script for setting MOZILLA_FIVE_HOME
	rm -f debian/tmp/usr/bin/eclipse
	sed 's|@XULRUNNER_PATH@|$(XULRUNNER_PATH)|' debian/extra/eclipse > debian/tmp/usr/bin/eclipse
	chmod 755 debian/tmp/usr/bin/eclipse
	# Set a saner default file limit than "unlimited".
	echo 'osgi.bundlefile.limit=100' >> debian/tmp/usr/lib/eclipse/configuration/config.ini
	# Remove the unnecessary configuration data (taken from Fedora spec)
	find debian/tmp/usr/lib/eclipse -depth -type d -empty -delete
	# fix permissions of scripts
	chmod +x debian/tmp/usr/lib/eclipse/plugins/org.eclipse.pde.build_*/templates/package-build/*.sh
	@echo "*** generate-pdebuild-scripts ***"
	mkdir -p `dirname $(COPY_PLATFORM_INSTALL)` `dirname $(PDEBUILD_INSTALL)`
	install -m 755 pdebuild/eclipse-copy-platform.sh $(COPY_PLATFORM_INSTALL)
	( \
	  cd debian/tmp/usr/lib/eclipse; \
	  ls -d * | egrep -v '^(plugins|features|about_files|dropins|buildscripts)$$' ; \
	  ls -d * plugins/* features/* ; \
	) | sed -e's,^\(.*\),[ ! -e \1 ] \&\& ln -s $$eclipse/\1 \1,' >> $(COPY_PLATFORM_INSTALL)
	sed -e "s/@PDEBUILDVERSION@/$(PDEBUILDVERSION)/g" -e "s:@COPY_PLATFORM@:$(COPY_PLATFORM):g" < pdebuild/eclipse-pdebuild.sh > $(PDEBUILD_INSTALL)
	chmod a+x $(PDEBUILD_INSTALL)
	dh_install --list-missing
	$(if $(wildcard debian/tmp/usr/lib/eclipse/plugins/org.eclipse.core.net.linux.*.jar),dh_install -peclipse-platform debian/tmp/usr/lib/eclipse/plugins/org.eclipse.core.net.linux.*.jar /usr/lib/eclipse/plugins)
	$(if $(wildcard debian/tmp/usr/lib/eclipse/plugins/org.eclipse.update.core.linux*.jar),dh_install -peclipse-platform debian/tmp/usr/lib/eclipse/plugins/org.eclipse.update.core.linux*.jar /usr/lib/eclipse/plugins)
	mkdir "$(SWT_SO_PATH)"
	find $(JNI_SO_PATH) -name 'libswt*.so' -exec mv {} "$(SWT_SO_PATH)" \;
	find $(JNI_SO_PATH) -depth -type d -empty -delete
	# Check if we are doing a -B build
	[ ! -d debian/eclipse-platform-data ] || debian/rules override_dh_install_indep

# Operations on arch: all packages - not invoked if building with -B
override_dh_install_indep:
	# mv arch-indep plugins from lib to share
	cd debian/eclipse-platform-data/usr && find lib/ -type f -path '*lib/eclipse/plugins/*' | perl ../../../debian/extra/imagemv.pl
	cd debian/eclipse-plugin-cvs/usr && find lib/ -type f -path '*lib/eclipse/*' | perl ../../../debian/extra/imagemv.pl
	# Expose osgi jars
	mkdir -p debian/libequinox-osgi-java/usr/share/java
	cd debian/libequinox-osgi-java/usr/share/java && \
	  for FILE in ../../lib/eclipse/plugins/* ; do \
		ln -s $$FILE ; \
		ln -s $$FILE `basename $$FILE | sed 's/_.*$$//g'`.jar ; \
	  done
	# Install poms and jars into Maven repo
	 for pom in $(wildcard debian/poms/*.xml); do \
		mh_installpom -v -plibequinox-osgi-java $$pom; \
		SUFFIX=`basename $$pom .xml | sed 's/pom-equinox-//'`; \
		mh_installjar -v -plibequinox-osgi-java  $$pom debian/libequinox-osgi-java/usr/share/java/org.eclipse.$$SUFFIX.jar; \
	 done

