#!/usr/bin/make -f

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

VERSION       := $(shell dpkg-parsechangelog --show-field Version | sed 's/-[^-]*//')
UVERSION      := $(shell echo $(VERSION) | sed 's/~.*//')

GCC_VERSION   := $(shell ls -l /usr/bin/gcj | sed 's/.*-> gcj-\(.*\)/\1/')
GCC_VERSION   := 6

LIBGCJ_EXT    := $(shell ls -l /usr/lib/$(DEB_HOST_MULTIARCH)/libgcj_bc.so.1 | sed 's/.*\.//')
LIBGCJ_SOVER  = $(LIBGCJ_EXT)
LIBGCJ_SOVER  = 17
REQ_VERSION   = 4.4.6

GCJ = gcj-$(GCC_VERSION)
GIJ = gij-$(GCC_VERSION)
GCJDBTOOL = gcj-dbtool-$(GCC_VERSION)
GCJSUBDIR = /usr/lib/$(DEB_HOST_MULTIARCH)/gcj-$(GCC_VERSION)-$(LIBGCJ_SOVER)

# work around ecj1 having fallen out of the gcc search path
PATH := $(CURDIR):$(PATH)
export PATH

with_native := yes
with_rebuild := yes
with_sourcebuild := yes

ifneq (,$(filter $(DEB_HOST_ARCH), m68k powerpcspe ppc64 s390x sh4 sparc64))
  with_sourcebuild :=
endif

ifneq (,$(filter $(DEB_HOST_ARCH), arm armel))
  with_rebuild := no
endif

ifneq (,$(filter $(DEB_HOST_ARCH), arm))
  with_sourcebuild := no
endif

DEB_DH_GENCONTROL_ARGS = -- \
	'-Vpkg:gij=gcj-$(GCC_VERSION)-jre-headless (>= $(REQ_VERSION))' \
	'-Vpkg:libgcjjar=gcj-$(GCC_VERSION)-jre-lib (>= $(REQ_VERSION))'

JAR = eclipse-ecj.jar

eclipse_root = $(CURDIR)/../eclipse

%:
	dh $@

override_dh_auto_build-indep: build/stamp-bytecode

override_dh_auto_build-arch: build/stamp-nativecode

build/stamp-bytecode:
ifneq (,$(findstring $(DEB_HOST_ARCH),arm))
	@echo 'Sorry ecj is no longer built on $(DEB_HOST_ARCH) due to a lack of porters'
	@echo 'supporting gij/gcj on this architecture. Please consider helping if you can.'
	false
endif
	rm -rf build/bin
	mkdir -p build/bin

ifeq ($(with_sourcebuild),yes)
	cp -r src/org.eclipse.jdt.core/org build/bin/
	cat gcc.properties \
	    >> build/bin/org/eclipse/jdt/internal/compiler/batch/messages.properties
	rm -rf build/bin/org/eclipse/jdt/internal/compiler/apt
	rm -rf build/bin/org/eclipse/jdt/internal/compiler/tool

	# Replace the bundle_version parameter with the actual version
	sed -i 's/bundle_qualifier, bundle_version/$(UVERSION)/' \
	    build/bin/org/eclipse/jdt/internal/compiler/batch/messages.properties

	find build/bin -name '*.java' > build/sourcefiles
	split -l 25 build/sourcefiles ecj-sources.
	ls -l
	mv ecj-sources.* build/bin
	ls -l . build/bin

	ln -sf /usr/lib/gcc/ecj1 $(CURDIR)/ecj1
	set -e; \
	for list in $$(find build/bin -name 'ecj-sources.*'); do \
	    echo "building files in $$list ..."; \
	    echo $(GCJ) -d build/bin -C -g \
		-I/usr/share/ant/lib/ant.jar \
		-Ibuild/bin \
		$$(cat $$list); \
	    $(GCJ) -v -d build/bin -C -g \
		-I/usr/share/ant/lib/ant.jar \
		-Ibuild/bin \
	        -Wno-all -Wno-unchecked -Wno-raw -Wno-resource \
		$$(cat $$list); \
	done

	find build/bin -name 'sources.list' -exec rm -f {} \;
	find build/bin -name '*.java' -exec rm -f {} \;
	find build/bin -name '*.html' -exec rm -f {} \;

	mkdir -p build/bootstrap
	fastjar -c -C build/bin . -f build/bootstrap/$(JAR)

  ifeq ($(with_rebuild),yes)
	: # rebuild the compiler
	rm -rf build/bin
	mkdir -p build/bin
	cp -r src/org.eclipse.jdt.core/org build/bin/
	cat gcc.properties \
	    >> build/bin/org/eclipse/jdt/internal/compiler/batch/messages.properties
	rm -rf build/bin/org/eclipse/jdt/internal/compiler/apt
	rm -rf build/bin/org/eclipse/jdt/internal/compiler/tool

	# Replace the bundle_version parameter with the actual version
	sed -i 's/bundle_qualifier, bundle_version/$(UVERSION)/' \
	    build/bin/org/eclipse/jdt/internal/compiler/batch/messages.properties

	time $(GIJ) \
	    -classpath build/bootstrap/$(JAR):/usr/share/ant/lib/ant.jar \
	    org.eclipse.jdt.internal.compiler.batch.Main \
	    -bootclasspath /usr/share/java/libgcj-$(GCC_VERSION).jar \
	    build/bin

	find build/bin -name '*.java' -exec rm -f {} \;
	find build/bin -name '*.html' -exec rm -f {} \;

	mkdir -p build/dist
	fastjar -c -C build/bin . -f build/dist/$(JAR)
  else
	mkdir -p build/dist
	cp -p build/bootstrap/$(JAR) build/dist/$(JAR)
  endif
else
	mkdir -p build/bin
	unzip -d build/bin -q /usr/share/java/eclipse-ecj.jar
	mkdir -p build/dist
	cp -p /usr/share/java/eclipse-ecj.jar build/dist/$(JAR)
endif
	mkdir -p build/exe
	cp build/dist/$(JAR) build/exe/ecj-standalone.jar
	zip -d build/exe/ecj-standalone.jar \
		'org/eclipse/jdt/core/JDTCompilerAdapter*'

	touch build/stamp-bytecode

build/stamp-nativecode: build/stamp-bytecode
	find build/dist -name '*.jar.*.jar' | xargs -r rm -f
ifeq ($(with_native),yes)
	: # $(JAR).so

	cd build/bin && find -name '*.rsc' -o -name '*.properties' -o -name '*.props' \
	    | fastjar -c -@ - -f $(CURDIR)/build/resources.jar
	$(GCJ) \
	    -c -O2 -g -fPIC -fjni -findirect-dispatch \
	    -o build/dist/resources.o build/resources.jar
	cp -p build/dist/resources.o build/exe/

	PYTHONPATH=$(CURDIR)/debian \
	RPATH=-Wl,-rpath,$(GCJSUBDIR) \
	  time python debian/aot-compile \
	    --gcj=$(GCJ) --dbtool=$(GCJDBTOOL) \
	    -L /usr/lib/gcj build/dist build/dist

	PYTHONPATH=$(CURDIR)/debian \
	RPATH=-Wl,-rpath,$(GCJSUBDIR) \
	  time python debian/aot-compile \
	    --gcj=$(GCJ) --dbtool=$(GCJDBTOOL) \
	    -L /usr/lib/gcj build/exe build/exe

  ifeq (0,1)
	: # ecj-gcj
	time $(GCJ) \
	    -O1 -g -Wl,-O1 -Wl,-rpath,$(GCJSUBDIR) -Wl,-z,relro \
	    --main=org.eclipse.jdt.internal.compiler.batch.Main \
	    -o build/dist/ecj-gcj build/exe/ecj-standalone.jar
	: # ecj1
	time $(GCJ) \
	    -O1 -g -Wl,-O1 -Wl,-rpath,$(GCJSUBDIR) -Wl,-z,relro \
	    --main=org.eclipse.jdt.internal.compiler.batch.GCCMain \
	    -o build/dist/ecj1 build/exe/ecj-standalone.jar
  endif
endif
	touch build/stamp-nativecode


override_dh_install-indep:
	dh_install -i

	mh_installpoms -plibecj-java
	mh_installjar -plibecj-java -l debian/poms/ecj.pom build/dist/$(JAR) \
		--usj-name=eclipse-ecj

override_dh_install-arch:
	# ecj
	mkdir -p debian/tmp/usr/bin
	sed 's/@ver@/$(GCC_VERSION)/g' debian/ecj.in \
	  > debian/tmp/usr/bin/ecj
	chmod 755 debian/tmp/usr/bin/ecj

ifeq ($(with_native),yes)
	# libecj-java-gcj
	mkdir -p debian/tmp/usr/lib/gcj
	install -m 644 build/dist/$(JAR).so debian/tmp/usr/lib/gcj

	mkdir -p debian/tmp/usr/share/gcj/classmap.d
	install -m 644 build/dist/*.db \
		debian/tmp/usr/share/gcj/classmap.d/
endif

ifeq ($(with_native),yes)
	# ecj1::
	mkdir -p debian/ecj1/usr/lib/$(DEB_HOST_MULTIARCH)/gcc
	install -m 755 build/exe/ecj1 debian/ecj1/usr/lib/$(DEB_HOST_MULTIARCH)/gcc/
	dh_link -p ecj1 \
	  /usr/lib/$(DEB_HOST_MULTIARCH)/gcc/ecj1 /usr/lib/gcc/ecj1
endif

	# ecj-gcj
ifeq ($(with_native),yes)
	mkdir -p debian/tmp/usr/bin
	install -m 755 build/exe/ecj-gcj debian/tmp/usr/bin/
endif
	mkdir -p debian/ecj-gcj/usr/bin

	mkdir -p debian/ecj-gcj/usr/share/man/man1
	ln -sf ecj.1.gz \
	  debian/ecj-gcj/usr/share/man/man1/ecj-gcj.1.gz

	dh_install -a


override_dh_clean:
	dh_clean
	rm -rf build
	rm -f debian/*.pyc ecj1
	rm -rf debian/.mh

TAG = R4_5_1
DIR = ecj-$(VERSION)

MAVEN_REPO  := http://repo1.maven.org/maven2
PACKAGE := ecj

get-orig-pom:
	mkdir -p debian/poms
	wget  -U NoSuchBrowser/1.0 -O debian/poms/$(PACKAGE).pom \
        $(MAVEN_REPO)/org/eclipse/jdt/core/compiler/$(PACKAGE)/$(VERSION)/$(PACKAGE)-$(VERSION).pom

get-orig-source:
	rm -rf $(DIR) $(TAG).tar.gz $(TAG)
	wget http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/snapshot/$(TAG).tar.gz
	tar xf $(TAG).tar.gz
	mkdir -p $(DIR)/src/org.eclipse.jdt.core
	cp -p $(TAG)/org.eclipse.jdt.core/scripts/about.html \
		$(DIR)/src/org.eclipse.jdt.core/
	cp -p $(TAG)/org.eclipse.jdt.core/scripts/build.xml \
		$(DIR)/src/org.eclipse.jdt.core/

	tar -c -f - -C $(TAG)/org.eclipse.jdt.core/compiler org \
	    | tar -x -f - -C $(DIR)/src/org.eclipse.jdt.core/

	tar -c -f - -C $(TAG)/org.eclipse.jdt.core/antadapter org META-INF \
	    | tar -x -f - -C $(DIR)/src/org.eclipse.jdt.core/

	find $(DIR) -name CheckDebugAttributes.java | xargs -r rm -f
	find $(DIR) -name BuildJarIndex.java | xargs -r rm -f

	tar -c -f - -C $(TAG)/org.eclipse.jdt.core/batch org \
	    | tar -x -f - -C $(DIR)/src/org.eclipse.jdt.core/

	mkdir -p $(DIR)/src/org.eclipse.jdt.core/META-INF/services
#	cp -p $(TAG)/org.eclipse.jdt.core/scripts/META-INF/MANIFEST.MF \
#	    $(DIR)/src/org.eclipse.jdt.core/META-INF/
	printf 'org.eclipse.jdt.internal.compiler.tool.EclipseCompiler #Eclipse compiler' \
	    > $(DIR)/src/org.eclipse.jdt.core/META-INF/services/javax.tools.JavaCompiler

	# Create the upstream tarball
	tar -c -J -f ../ecj_$(VERSION).orig.tar.xz $(DIR)

	# Remove the temporary files
	rm -rf $(DIR) $(TAG).tar.gz $(TAG)
