#!/usr/bin/make -f

PKG=tycho
VER=0.25.0
DTYPE=

UVER=0.25.0
UURL=http://git.eclipse.org/c/tycho/org.eclipse.tycho.git/snapshot/org.eclipse.tycho-tycho-$(UVER).tar.xz

FUURL=https://github.com/rgrunber/fedoraproject-p2

AGGREGATOR_PATH=git://git.eclipse.org/gitroot/platform/eclipse.platform.releng.aggregator.git
TAG=S4_6_0_M6

BINPKG=libtycho-java

%:
	dh $@ --buildsystem=maven

override_dh_auto_configure:
	./debian/eclipse-bootstrap.sh $(CURDIR)
	dh_auto_configure

override_dh_auto_build:
	dh_auto_build -- package -Dmaven.test.skip=true

override_dh_auto_test:

override_dh_auto_install:
	dh_auto_install
	#patch eclipse-application packaging
	cp tycho-bundles/tycho-bundles-external/target/tycho-bundles-external.zip debian/$(BINPKG)/usr/share/maven-repo/org/eclipse/tycho/tycho-bundles-external/$(VER)/tycho-bundles-external-$(VER).zip
	ln -s ../$(VER)/tycho-bundles-external-$(VER).zip debian/$(BINPKG)/usr/share/maven-repo/org/eclipse/tycho/tycho-bundles-external/debian/tycho-bundles-external-debian.zip


.PHONY: get-orig-source
# upstream is http://git.eclipse.org/c/tycho/org.eclipse.tycho.git/snapshot/org.eclipse.tycho-tycho-0.25.0.tar.xz
# plus
# https://github.com/rgrunber/fedoraproject-p2
# plus
# some of eclipse sources
get-orig-source:
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting.))
	mkdir $(PKG)-$(VER)

	@echo "# Downloading Tycho..."
	wget $(UURL)
	tar x -C $(PKG)-$(VER) -f org.eclipse.tycho-tycho-$(UVER).tar.xz
	find $(PKG)-$(VER)/org.eclipse.tycho-tycho-$(UVER) -name '*.jar' -delete
	find $(PKG)-$(VER)/org.eclipse.tycho-tycho-$(UVER) -name '.git*' -delete
	mv $(PKG)-$(VER)/org.eclipse.tycho-tycho-$(UVER)/* $(PKG)-$(VER)
	rmdir $(PKG)-$(VER)/org.eclipse.tycho-tycho-$(UVER)	#should be empty
	$(RM) org.eclipse.tycho-tycho-$(UVER).tar.xz

	@echo "# Downloading Fedora project P2..."
	cd $(PKG)-$(VER) && git clone $(FUURL)
	cd $(PKG)-$(VER)/fedoraproject-p2 && git checkout && $(RM) -r .git .git*

	@echo "# Downloading Eclipse (this may take a long time)..."
	cd $(PKG)-$(VER) && git clone --depth 1 --branch $(TAG)  $(AGGREGATOR_PATH) eclipse-aggregator
	cd $(PKG)-$(VER)/eclipse-aggregator \
	&& git submodule init eclipse.jdt eclipse.jdt.core eclipse.platform.runtime eclipse.platform.team \
	eclipse.platform.swt eclipse.platform.text eclipse.platform.ui eclipse.platform.resources \
	rt.equinox.p2 rt.equinox.bundles rt.equinox.framework \
	&& git submodule update

	@echo "# We keep only Eclipse bundles required by Tycho"
	$(eval SRC:=$(PKG)-$(VER)/eclipse-aggregator)
	$(eval DEST:=$(PKG)-$(VER)/eclipse-bundles)
	mkdir -p $(DEST)

	rsync -r $(SRC)/eclipse.platform.runtime/bundles/org.eclipse.core.runtime $(DEST)
	rsync -r $(SRC)/eclipse.platform.runtime/bundles/org.eclipse.core.contenttype $(DEST)
	rsync -r $(SRC)/eclipse.platform.runtime/bundles/org.eclipse.core.jobs $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.core $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.metadata $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.publisher $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.publisher.eclipse $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.repository $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.metadata.repository $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.artifact.repository $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.jarprocessor $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.simpleconfigurator.manipulator $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.frameworkadmin $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.frameworkadmin.equinox $(DEST)
	rsync -r $(SRC)/rt.equinox.framework/bundles/org.eclipse.osgi.util $(DEST)
	rsync -r $(SRC)/rt.equinox.bundles/bundles/org.eclipse.equinox.preferences $(DEST)
	rsync -r $(SRC)/rt.equinox.bundles/bundles/org.eclipse.equinox.preferences $(DEST)
	rsync -r $(SRC)/rt.equinox.bundles/bundles/org.eclipse.equinox.common $(DEST)
	rsync -r $(SRC)/rt.equinox.bundles/bundles/org.eclipse.equinox.security $(DEST)
	rsync -r $(SRC)/rt.equinox.bundles/bundles/org.eclipse.equinox.app $(DEST)
	rsync -r $(SRC)/rt.equinox.bundles/bundles/org.eclipse.equinox.registry $(DEST)
	#here can compile
	rsync -r $(SRC)/rt.equinox.framework/bundles/org.eclipse.osgi.compatibility.state $(DEST)
	rsync -r $(SRC)/rt.equinox.framework/bundles/org.eclipse.osgi $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.repository.tools $(DEST)
	rsync -r $(SRC)/rt.equinox.bundles/bundles/org.eclipse.equinox.app $(DEST)
	rsync -r $(SRC)/rt.equinox.framework/bundles/org.eclipse.osgi.services $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.director $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.engine $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.jarprocessor $(DEST)
	#here can compile
	#need SteppedTimeoutLexicoHelper from sat4j 2.3.4
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.touchpoint.eclipse $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.garbagecollector $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.updatesite $(DEST)
	rsync -r $(SRC)/rt.equinox.p2/bundles/org.eclipse.equinox.p2.director.app $(DEST)
	rsync -r $(SRC)/eclipse.platform.team/bundles/org.eclipse.core.net $(DEST)
	#here can compile
	rsync -r $(SRC)/eclipse.platform.swt/bundles/org.eclipse.swt $(DEST)
	#here can compile
	rsync -r $(SRC)/eclipse.jdt.core/org.eclipse.jdt.core $(DEST)
	rsync -r $(SRC)/eclipse.jdt.core/org.eclipse.jdt.compiler.apt $(DEST)
	rsync -r $(SRC)/eclipse.platform.text/org.eclipse.text $(DEST)
	rsync -r $(SRC)/eclipse.platform.text/org.eclipse.jface.text $(DEST)
	rsync -r $(SRC)/eclipse.platform.ui/bundles/org.eclipse.core.commands $(DEST)
	rsync -r $(SRC)/eclipse.platform.ui/bundles/org.eclipse.jface $(DEST)
	rsync -r $(SRC)/eclipse.platform.runtime/bundles/org.eclipse.core.expressions $(DEST)
	rsync -r $(SRC)/rt.equinox.bundles/bundles/org.eclipse.equinox.bidi $(DEST)
	rsync -r $(SRC)/eclipse.platform.resources/bundles/org.eclipse.core.resources $(DEST)
	rsync -r $(SRC)/eclipse.platform.resources/bundles/org.eclipse.core.filesystem $(DEST)

	$(RM) -rf $(SRC)

	find $(DEST) -type d -name ".git" | xargs rm -rf
	find $(DEST) -type f -name *.jar -delete
	find $(DEST) -type f -name *.class -delete
	find $(DEST) -type f -name *.so -delete
	find $(DEST) -type f -name *.dll -delete
	find $(DEST) -type f -name *.jnilib -delete
	find $(DEST) -type f -name *.exe -delete

	@echo "# Packing..."
	$(RM) -rf $(PKG)_$(VER)$(DTYPE).orig.tar.xz
	find -L "$(PKG)-$(VER)" -xdev -type f -print | LC_ALL=C sort \
	| XZ_OPT="-6v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX

	@echo "# Cleaning up..."
	$(RM) -rf $(PKG)-$(VER)


