#!/usr/bin/make -f

export JAVA_HOME=/usr/lib/jvm/default-java
export CLASSPATH=/usr/share/java/gettext-commons.jar:/usr/share/java/jsr305.jar:/usr/share/java/signpost-core.jar:/usr/share/java/jmapviewer.jar

SVNREPO := https://josm.openstreetmap.de/svn/trunk
SVNI18NREPO := https://josm.openstreetmap.de/osmsvn/applications/editors/josm/i18n

DATE := $(shell date +%Y%m%d)
ifdef SVNREV
VERSION := 0.0.svn${SVNREV}
else
VERSION := 0.0.0.${DATE}
endif
DESTDIR := ../tarballs/josm-${VERSION}
DESTTGZ := ../tarballs/josm_${VERSION}+dfsg.orig.tar.gz

include /usr/share/dpkg/pkg-info.mk

DEB_UPSTREAM_VERSION := $(shell echo $(DEB_VERSION) | cut -d- -f1)

%:
	dh $@

override_dh_auto_build:
	# Move translation causing build failure out of the way
	mv i18n/po/ca@valencia.po .

	dh_auto_build -- -v -d -Ddebian.version=${DEB_VERSION} dist

	mv ca@valencia.po i18n/po/

execute_after_dh_auto_install:
	install -m 644 $(CURDIR)/dist/josm-custom.jar $(CURDIR)/debian/josm/usr/share/josm/josm-${DEB_UPSTREAM_VERSION}.jar
	dh_link usr/share/josm/josm-${DEB_UPSTREAM_VERSION}.jar usr/share/josm/josm.jar

	install -m 755 $(CURDIR)/native/linux/tested/usr/bin/josm $(CURDIR)/debian/josm/usr/bin/josm

	install -d $(CURDIR)/debian/josm/etc/default
	install -m 644 $(CURDIR)/native/linux/tested/etc/default/josm $(CURDIR)/debian/josm/etc/default/josm

	for size in 512x512 256x256 192x192 128x128 96x96 80x80 72x72 64x64 48x48 42x42 40x40 36x36 32x32 24x24 22x22 16x16 8x8; do \
		install -d $(CURDIR)/debian/josm/usr/share/icons/hicolor/$${size}/apps ; \
		install -m 644 $(CURDIR)/native/linux/tested/usr/share/icons/hicolor/$${size}/apps/org.openstreetmap.josm.png $(CURDIR)/debian/josm/usr/share/icons/hicolor/$${size}/apps/org.openstreetmap.josm.png ; \
	done
	install -d $(CURDIR)/debian/josm/usr/share/icons/hicolor/scalable/apps
	install -m 644 $(CURDIR)/native/linux/tested/usr/share/icons/hicolor/scalable/apps/org.openstreetmap.josm.svg $(CURDIR)/debian/josm/usr/share/icons/hicolor/scalable/apps/org.openstreetmap.josm.svg

	install -m 644 $(CURDIR)/native/linux/tested/usr/share/pixmaps/org.openstreetmap.josm.png $(CURDIR)/debian/josm/usr/share/pixmaps/org.openstreetmap.josm.png
	install -m 644 $(CURDIR)/native/linux/tested/usr/share/applications/org.openstreetmap.josm.desktop $(CURDIR)/debian/josm/usr/share/applications/org.openstreetmap.josm.desktop
	install -m 644 $(CURDIR)/native/linux/tested/usr/share/metainfo/org.openstreetmap.josm.appdata.xml $(CURDIR)/debian/josm/usr/share/metainfo/org.openstreetmap.josm.appdata.xml

	install -d $(CURDIR)/debian/josm/usr/share/mime/packages
	install -m 644 $(CURDIR)/native/linux/tested/usr/share/mime/packages/josm.xml $(CURDIR)/debian/josm/usr/share/mime/packages

execute_after_dh_install:
	# remove *.lang files, since we ship them in josm-l10n
	rm -rf $(CURDIR)/debian/josm/usr/share/josm/data/*.lang

	# remove executable bit
	chmod a-x $(CURDIR)/debian/josm/usr/share/josm/data/defaultpresets.xml

	# Remove empty directories
	find $(CURDIR)/debian/josm/usr/share/josm/data -type d -empty -delete

	# check font in SVG files
	sh $(CURDIR)/debian/images-check.sh

get-orig-source:
	dh_testdir
	test -d ../tarballs/. || mkdir -p ../tarballs
	@echo Downloading josm from ${SVNREPO}
	svn export -r ${SVNREV} ${SVNREPO} ${DESTDIR}
	@echo Removing duplicated code
	rm -rf ${DESTDIR}/src/org/apache/commons/compress ${DESTDIR}/src/org/apache/commons/logging
	@echo Downloading source JAR
	wget https://josm.openstreetmap.de/download/josm-snapshot-${SVNREV}-sources.jar -O /tmp/josm-sources.jar
	@echo Extracting sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/josm-sources.jar ch/poole com/drew com/google com/kitfox jakarta/annotation jakarta/json org/apache/commons org/eclipse org/tukaani && cd $$OLDPWD
	for i in `grep "Generated By:JavaCC:" ${DESTDIR}/src/ch/poole -r | cut -d: -f1 | sort -u`; do \
		rm -v "$$i"; \
	done
	cd ${DESTDIR}/resources && jar -xf /tmp/josm-sources.jar META-INF/resources/webjars/tag2link && cd $$OLDPWD
	find ${DESTDIR}/resources/META-INF/resources/webjars/tag2link -type f -not -name index.json -delete
	@echo Downloading xmpcore source JAR
	wget https://repo1.maven.org/maven2/com/adobe/xmp/xmpcore/6.1.10/xmpcore-6.1.10-sources.jar -O /tmp/xmpcore-sources.jar
	@echo Extracting xmpcore sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/xmpcore-sources.jar com/adobe && cd $$OLDPWD
	@echo Downloading annotations source JAR
	wget https://repo1.maven.org/maven2/org/jetbrains/annotations/24.0.1/annotations-24.0.1-sources.jar -O /tmp/annotations-sources.jar
	@echo Extracting annotations sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/annotations-sources.jar org/jetbrains && cd $$OLDPWD
	@echo Fetching revision metadata
	@echo Downloading commons-io source JAR
	wget https://repo1.maven.org/maven2/commons-io/commons-io/2.16.1/commons-io-2.16.1-sources.jar -O /tmp/commons-io-sources.jar
	@echo Extracting commons-io sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/commons-io-sources.jar org/apache && cd $$OLDPWD
	@echo Downloading commons-lang3 source JAR
	wget https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.16.0/commons-lang3-3.16.0-sources.jar -O /tmp/commons-lang3-sources.jar
	@echo Extracting commons-lang3 sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/commons-lang3-sources.jar org/apache && cd $$OLDPWD
	svn info --xml -r ${SVNREV} ${SVNREPO} > ${DESTDIR}/REVISION
	@echo Fetching internalization data
	svn export ${SVNI18NREPO} ${DESTDIR}/i18n
	@echo Downloading translations from Launchpad
	bzr export -v ${DESTDIR}/i18n/po/ lp:~openstreetmap/josm/josm_trans/
	@echo Stripping comments from translations to save some space in the tarball
	for i in ${DESTDIR}/i18n/po/josm/*.po; do \
		grep -E -v "^#[:,\.]" $$i > ${DESTDIR}/i18n/po/`basename $$i`; \
	done
	mv ${DESTDIR}/i18n/po/josm/*.pot ${DESTDIR}/i18n/po/
	rm -rf ${DESTDIR}/i18n/po/josm/
	@echo Removing jar files
	find ${DESTDIR} -name "*.jar" -exec rm -v {} \;
	@echo Removing nodist/images
	rm -fr ${DESTDIR}/nodist/images
	@echo Removing other useless data
	rm -fr ${DESTDIR}/nodist/data/*.osm ${DESTDIR}/nodist/data/*.gpx ${DESTDIR}/nodist/data/*.jpg
	@echo Removing non-free or autogenerated data
	rm -fr ${DESTDIR}/resources/data/*.lang
	rm -fr ${DESTDIR}/src/org/openstreetmap/josm/data/imagery/types/*
	@echo Removing embedded code copy of jmapviewer
	rm -fr ${DESTDIR}/src/org/openstreetmap/gui/jmapviewer/
	@echo Removing embedded fonts
	rm -fr ${DESTDIR}/resources/data/fonts/
	@echo Removing OSX support
	rm -fr ${DESTDIR}/native/macosx/
	@echo Removing Windows installer
	rm -fr ${DESTDIR}/native/windows/
	@echo Building snapshot tarball
	tar czf ${DESTTGZ} -C `dirname ${DESTDIR}` `basename ${DESTDIR}`
	@echo Cleaning up
	rm -rf ${DESTDIR}
	@echo Moving tarball to parent directory
	mv -v ${DESTTGZ} ../
	@echo Removing source JAR
	rm -f /tmp/josm-sources.jar /tmp/xmpcore-sources.jar /tmp/annotations-sources.jar /tmp/commons-io-sources.jar /tmp/commons-lang3-sources.jar
	@echo Removing tarballs directory
	rmdir ../tarballs
	@echo .
	@echo To update debian/changelog type
	@echo dch -v ${VERSION}+dfsg-1
	@echo .
