#!/usr/bin/make -f

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

# We need this for overriding uname, upstream expects a slightly
# different behaviour than Debian's uname.
export PATH:=$(CURDIR)/debian/extra/bin:$(PATH)

%:
	dh --with xulrunner $@

override_dh_auto_clean:
	ant distclean
	mh_clean

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

JNI_SO_PATH = debian/eclipse-rcp/usr/lib/eclipse/configuration/org.eclipse.osgi/bundles/*/1/.cp
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 \
			$(JNI_SO_PATH)/libswt-gtk-*.so \
			$(JNI_SO_PATH)/libswt-atk-gtk-*.so \
			$(JNI_SO_PATH)/libswt-awt-gtk-*.so \
			$(JNI_SO_PATH)/libswt-cairo-gtk-*.so \
			$(JNI_SO_PATH)/libswt-pi-gtk-*.so \
			$(JNI_SO_PATH)/libswt-xpcominit-gtk-*.so \
			$(JNI_SO_PATH)/libswt-xulrunner-gtk-*.so \
			$(shell find $(JNI_SO_PATH) -name '*.so' | grep -v libswt-) \
			$(LAUNCHER_LIB) \
		-dRecommends \
			$(JNI_SO_PATH)/libswt-gnome-gtk-*.so \
			$(JNI_SO_PATH)/libswt-glx-gtk-*.so
	dh_shlibdeps --remaining-packages

unapply-patches:
	for P in `tac debian/patches/series` ; do \
		patch -p1 -R < debian/patches/$$P || exit 1;\
	done

apply-patches:
	for P in `cat debian/patches/series` ; do \
		patch -p1 < debian/patches/$$P || exit 1;\
	done

refresh-patches: unapply-patches
	for P in `cat debian/patches/series` ; do \
		(QUILT_PATCHES=debian/patches quilt --quiltrc /dev/null push || test $$? = 2) || exit 1 ;\
		QUILT_PATCHES=debian/patches quilt --quiltrc /dev/null refresh -p ab --no-index --no-timestamps || exit 1 ;\
	done
	rm -fr .pc

# TODO: Turn all commands below this comment into patches and send them to eclipse-build.

# 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

DEB_VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DEB_NOEPOCH_VERSION := $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')
BUILD_TAG := $(shell grep ^buildTag= build.properties | sed "s/^buildTag=//")
SOURCE_DIR := $(CURDIR)/build/eclipse-$(UPSTREAM_VERSION)-src
RESULT_DIR := $(SOURCE_DIR)/installation/
DEBIAN_PACK_LIBDIR := $(CURDIR)/debian/tmp/usr/lib/eclipse
PROFILE_ID := PlatformProfile

LAUNCHERVERSION = $(shell ls $(DEBIAN_PACK_LIBDIR)/plugins | grep equinox.launcher_ | sed 's/org.eclipse.equinox.launcher_//')
PDEBUILDVERSION = $(shell ls $(DEBIAN_PACK_LIBDIR)/plugins | grep org.eclipse.pde.build_ | sed 's/org.eclipse.pde.build_//')
UPSTREAM_VERSION := $(shell echo $(DEB_UPSTREAM_VERSION) | sed "s/+repack.*$$//")

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)

DEB_HOST_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
ifneq (,$(filter $(DEB_HOST_ARCH_CPU),i386 i486 i586 i686))
	ECLIPSE_BUILD_ARCH=x86
endif
ifneq (,$(findstring $(DEB_HOST_ARCH_CPU),arm))
	ECLIPSE_BUILD_ARCH=arm
endif
ifeq ($(DEB_HOST_ARCH_CPU),amd64)
	ECLIPSE_BUILD_ARCH=x86_64
endif
ifneq (,$(findstring $(DEB_HOST_ARCH_CPU),mips))
	ECLIPSE_BUILD_ARCH=$(DEB_HOST_ARCH)
endif
ifneq (,$(filter $(DEB_HOST_ARCH_CPU),parisc parisc64))
	ECLIPSE_BUILD_ARCH=PA_RISC
endif
ifeq ($(DEB_HOST_ARCH_CPU),powerpc)
	ECLIPSE_BUILD_ARCH=ppc
endif
ifeq ($(ECLIPSE_BUILD_ARCH),)
	ECLIPSE_BUILD_ARCH=$(DEB_HOST_ARCH_CPU)
endif

POMS := debian/poms/pom-equinox-osgi.xml debian/poms/pom-equinox-osgi.services.xml debian/poms/pom-equinox-osgi.util.xml

override_dh_install:
	# Turn all following commands into patches and send them to eclipse-build
	# Remove some bad arguments that causes eclipse to fail
	perl -i -ne 'print $$_ unless(m/^-XX/);' debian/tmp/etc/eclipse.ini
	# Replace symlink with a copy - The absolute symlink causes eclipse to fail
	# and some of the commands attempt to modify eclipse.ini.
	rm -f debian/tmp/usr/lib/eclipse/eclipse.ini
	cp -f debian/tmp/etc/eclipse.ini debian/tmp/usr/lib/eclipse/
	# 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
	rm -fr $(DEBIAN_PACK_LIBDIR)/p2/
	# Generate metadata and extract the swt libraries.
	cd debian/tmp && java \
			-cp $(DEBIAN_PACK_LIBDIR)/plugins/org.eclipse.equinox.launcher_$(LAUNCHERVERSION) \
			org.eclipse.core.launcher.Main \
			-application \
			org.eclipse.equinox.p2.metadata.generator.EclipseGenerator \
			-debug -consolelog \
			-metadataRepository file:$(DEBIAN_PACK_LIBDIR)/metadata/ \
			-artifactRepository file:$(DEBIAN_PACK_LIBDIR)/metadata/ \
			-source $(DEBIAN_PACK_LIBDIR) \
			-root "Eclipse Platform" \
			-rootVersion $(UPSTREAM_VERSION) \
			-flavor tooling \
			-publishArtifacts \
			-data $(CURDIR)/workspace \
			-append \
			-artifactRepositoryName "Eclipse Platform" \
			-metadataRepositoryName "Eclipse Platform"
	cd debian/tmp && java \
			-Declipse.p2.data.area=file:$(DEBIAN_PACK_LIBDIR)/p2 \
			-Declipse.p2.MD5Check=false \
			-cp $(DEBIAN_PACK_LIBDIR)/plugins/org.eclipse.equinox.launcher_$(LAUNCHERVERSION) \
			org.eclipse.core.launcher.Main \
			-application \
			org.eclipse.equinox.p2.director \
			-debug -consolelog \
			-flavor tooling \
			-installIU "Eclipse Platform" \
			-p2.os linux \
			-p2.ws gtk \
			-p2.arch $(ECLIPSE_BUILD_ARCH) \
			-roaming \
			-data $(CURDIR)/workspace \
			-profile $(PROFILE_ID) \
			-profileProperties org.eclipse.update.install.features=true \
			-metadataRepository file:$(DEBIAN_PACK_LIBDIR)/metadata/ \
			-artifactRepository file:$(DEBIAN_PACK_LIBDIR)/metadata/ \
			-destination $(DEBIAN_PACK_LIBDIR) \
			-bundlepool $(DEBIAN_PACK_LIBDIR)
	cd debian/tmp && java -Dosgi.sharedConfiguration.area=$(DEBIAN_PACK_LIBDIR)/configuration \
			-cp $(DEBIAN_PACK_LIBDIR)/plugins/org.eclipse.equinox.launcher_$(LAUNCHERVERSION) \
			org.eclipse.core.launcher.Main \
			-debug -consolelog \
			-data $(CURDIR)/workspace \
			-metadataRepository file:$(DEBIAN_PACK_LIBDIR)/metadata \
			-artifactRepository file:$(DEBIAN_PACK_LIBDIR)/metadata \
			-application org.eclipse.equinox.initializer.configInitializer \
			-fileInitializer $(CURDIR)/debian/extract_patterns.txt
	# Normalize the paths.
	grep --exclude-from=debian/exclude_patterns.txt -l -I -r debian/tmp debian/tmp/ | xargs sed -i s@$(CURDIR)/debian/tmp@@g
	TO_CORRECT=`echo $(CURDIR)/debian/tmp | sed s@/@_@g` ; grep --exclude-from=debian/exclude_patterns.txt \
	        -l -I -r debian_tmp debian/tmp/ | xargs sed -i s@$${TO_CORRECT}@@g
	# remove extra licence file
	rm -f debian/tmp/usr/lib/eclipse/plugins/org.apache.ant_*/about_files/LICENSE.sax.txt
	# 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
	# Correct the profile in config.ini - else eclipse will refuse to open the update/install menus.
	sed -i 's/SDKProfile/PlatformProfile/' debian/tmp/usr/lib/eclipse/configuration/config.ini
	# 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)
	rm -r $(DEBIAN_PACK_LIBDIR)/configuration/org.eclipse.core.runtime
	rm -r $(DEBIAN_PACK_LIBDIR)/configuration/org.eclipse.equinox.app
	rm -rf $(DEBIAN_PACK_LIBDIR)/configuration/*.log
	find $(DEBIAN_PACK_LIBDIR)/configuration -depth -type d -name data -exec rm -rf {} \;
	find $(DEBIAN_PACK_LIBDIR) -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
	# Regenerate the eclipse.ini symlink.
	ln -sf /etc/eclipse.ini debian/tmp/usr/lib/eclipse/eclipse.ini
	@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_PACK_LIBDIR) ; \
	  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
	# Fix some weird exec permission
	chmod a-x debian/eclipse-rcp/usr/lib/eclipse/about_files/IJG_README
	$(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)
	# 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
	# 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 $(POMS); 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

