#!/usr/bin/make -f

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
	if [ -e "i18n/po/ca@valencia.po" ]; then mv i18n/po/ca@valencia.po .; fi

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

	if [ -e "ca@valencia.po" ]; then mv ca@valencia.po i18n/po/; fi

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/github/weisj/jsvg com/google 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 Extract translations from JAR
	mkdir ${DESTDIR}/trans
	cd ${DESTDIR}/trans && jar -xf /tmp/josm-sources.jar data && cd $$OLDPWD
	find ${DESTDIR}/trans/data -type f -not -name "*.lang" -delete
	find ${DESTDIR}/trans/data -mindepth 1 -type d -delete
	mv ${DESTDIR}/trans/data/*.lang ${DESTDIR}/resources/data/
	rmdir ${DESTDIR}/trans/data ${DESTDIR}/trans

	@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 jetbrains annotations source JAR
	wget https://repo1.maven.org/maven2/org/jetbrains/annotations/24.0.1/annotations-24.0.1-sources.jar -O /tmp/jetbrains-annotations-sources.jar
	@echo Extracting jetbrains annotations sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/jetbrains-annotations-sources.jar org/jetbrains && cd $$OLDPWD

	@echo Downloading commons-io source JAR
	wget https://repo1.maven.org/maven2/commons-io/commons-io/2.20.0/commons-io-2.20.0-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

	@echo Downloading jsvg annotations source tarball
	wget https://github.com/weisJ/jsvg/archive/refs/tags/v2.1.0.tar.gz -O /tmp/jsvg.tar.gz
	@echo Extracting jsvg annotations sources from tarball
	mkdir ${DESTDIR}/jsvg-annotations
	tar -xaf /tmp/jsvg.tar.gz --strip-components=9 -C ${DESTDIR}/jsvg-annotations --wildcards jsvg-*/annotations/src/main/java/com/github/weisj/jsvg/annotations
	mv ${DESTDIR}/jsvg-annotations/annotations ${DESTDIR}/src/com/github/weisj/jsvg/
	rm -rf ${DESTDIR}/jsvg-annotations

	@echo Downloading errorprone annotations source JAR
	wget https://repo1.maven.org/maven2/com/google/errorprone/error_prone_annotations/2.25.0/error_prone_annotations-2.25.0-sources.jar -O /tmp/errorprone-annotations-sources.jar
	@echo Extracting errorprone annotations sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/errorprone-annotations-sources.jar com/google && cd $$OLDPWD

	@echo Downloading intellij annotations source JAR
	wget https://repo1.maven.org/maven2/com/intellij/annotations/12.0/annotations-12.0-sources.jar -O /tmp/intellij-annotations-sources.jar
	@echo Extracting intellij annotations sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/intellij-annotations-sources.jar org/intellij && cd $$OLDPWD

	@echo Downloading bnd annotations source JAR
	wget https://repo1.maven.org/maven2/biz/aQute/bnd/biz.aQute.bnd.annotation/7.1.0/biz.aQute.bnd.annotation-7.1.0-sources.jar -O /tmp/bnd-annotations-sources.jar
	@echo Extracting bnd annotations sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/bnd-annotations-sources.jar aQute/bnd && cd $$OLDPWD

	@echo Downloading osgi annotations source JAR
	wget https://repo1.maven.org/maven2/org/osgi/osgi.annotation/8.1.0/osgi.annotation-8.1.0-sources.jar -O /tmp/osgi-annotations-sources.jar
	@echo Extracting osgi annotations sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/osgi-annotations-sources.jar org/osgi && cd $$OLDPWD

	@echo Downloading osgi namespace extender source JAR
	wget https://repo1.maven.org/maven2/org/osgi/org.osgi.namespace.extender/1.0.1/org.osgi.namespace.extender-1.0.1-sources.jar -O /tmp/osgi-namespace-extender-sources.jar
	@echo Extracting osgi namespace extender sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/osgi-namespace-extender-sources.jar org/osgi && cd $$OLDPWD

	@echo Downloading osgi namespace service source JAR
	wget https://repo1.maven.org/maven2/org/osgi/org.osgi.namespace.service/1.0.0/org.osgi.namespace.service-1.0.0-sources.jar -O /tmp/osgi-namespace-service-sources.jar
	@echo Extracting osgi namespace service sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/osgi-namespace-service-sources.jar org/osgi && cd $$OLDPWD

	@echo Downloading osgi resource source JAR
	wget https://repo1.maven.org/maven2/org/osgi/org.osgi.resource/1.0.0/org.osgi.resource-1.0.0-sources.jar -O /tmp/osgi-resource-sources.jar
	@echo Extracting osgi resource sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/osgi-resource-sources.jar org/osgi && cd $$OLDPWD

	@echo Downloading osgi dto source JAR
	wget https://repo1.maven.org/maven2/org/osgi/org.osgi.dto/1.0.0/org.osgi.dto-1.0.0-sources.jar -O /tmp/osgi-dto-sources.jar
	@echo Extracting osgi dto sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/osgi-dto-sources.jar org/osgi && cd $$OLDPWD

	@echo Downloading osgi service serviceloader source JAR
	wget https://repo1.maven.org/maven2/org/osgi/org.osgi.service.serviceloader/1.0.0/org.osgi.service.serviceloader-1.0.0-sources.jar -O /tmp/osgi-service-serviceloader-sources.jar
	@echo Extracting osgi service serviceloader sources from JAR
	cd ${DESTDIR}/src && jar -xf /tmp/osgi-service-serviceloader-sources.jar org/osgi && cd $$OLDPWD

	@echo Fetching revision metadata
	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/jetbrains-annotations-sources.jar \
	      /tmp/commons-io-sources.jar \
	      /tmp/commons-lang3-sources.jar \
	      /tmp/jsvg.tar.gz \
	      /tmp/errorprone-annotations-sources.jar \
	      /tmp/intellij-annotations-sources.jar \
	      /tmp/bnd-annotations-sources.jar \
	      /tmp/osgi-annotations-sources.jar \
	      /tmp/osgi-namespace-extender-sources.jar \
	      /tmp/osgi-namespace-service-sources.jar \
	      /tmp/osgi-resource-sources.jar \
	      /tmp/osgi-dto-sources.jar \
	      /tmp/osgi-service-serviceloader-sources.jar

	@echo Removing tarballs directory
	rmdir ../tarballs

	@echo .
	@echo To update debian/changelog type
	@echo dch -v ${VERSION}+dfsg-1
	@echo .
