#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS
KEYSTOREPASS := $(shell pwgen -s 16 1)
KEYPASS := $(shell pwgen -s 16 1)
DEBVERSION:=$(shell dpkg-parsechangelog | sed -n -e 's/Version: //p')
DEB_NOEPOCH_VERSION:=$(shell echo $(DEBVERSION) | cut -d':' -f 2)
DEB_SRC_VERSION:=$(shell echo $(DEB_NOEPOCH_VERSION) | sed -e 's/-[^-]\+$$//')
LOCALES:=$(shell (cd locale;find . -mindepth 1 -maxdepth 1 -type d)|sed -e 's,^./,,')

build: build-arch build-indep
build-arch:
	# currently none - just to please lintian
build-indep: build-stamp

build-stamp:
	dh_testdir
	javac -sourcepath fix/src/ fix/src/fix/Client.java
	cp debian/fix.manifest fix/manifest.mf
	ln -s src/fix fix
	(cd fix;jar cfmv FIX.jar manifest.mf icons fix/*.class)
	# generate using temp PW thus not showing
	@echo "Signing FIX.jar"
	@(cd fix;keytool -genkey -alias "DebBuild" -dname "cn=Debian F*EX Maintainers, c=de" -storepass "$(KEYSTOREPASS)" -keypass "$(KEYPASS)" -keystore ../debian/fix.keystore)
	@(cd fix;keytool -selfcert -validity 3650 -alias "DebBuild" -dname "cn=Debian F*EX Maintainers, c=de" -storepass "$(KEYSTOREPASS)" -keypass "$(KEYPASS)" -keystore ../debian/fix.keystore)
	@(cd fix;jarsigner -storepass "$(KEYSTOREPASS)" -keypass "$(KEYPASS)" -keystore ../debian/fix.keystore FIX.jar "DebBuild")
	locale/debian_translate . /usr/share/fex locale/export
	touch $@

clean: 
	dh_testdir
	dh_testroot
	rm -f fix/manifest.mf fix/FIX.jar fix/src/fix/*.class fix/fix
	rm -rf locale/export
	rm -f debian/fix.keystore 
	rm -rf debian/fix.tmp 
	rm -f build-stamp
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep -i 
	dh_installdirs -i
	dh_install -i

binary-indep: build-indep install
	dh_testdir
	dh_testroot
	dh_installchangelogs doc/Changes
	dh_installdocs
	dh_install
	if (cd debian/fex/usr/share/fex/;md5sum -c ../../../../htdocs.md5/$(DEB_NOEPOCH_VERSION));then \
		echo "Correct htdocs md5sum found."; \
	else \
		echo "htdocs contents changed! Please run "; \
		echo "debian/rules update-md5 or add the following to your md5 cache:" ; \
		(cd debian/fex/usr/share/fex/;find htdocs/ -maxdepth 1 -type f|grep -v FIX.jar |sort|xargs -n 1 md5sum); \
		exit 1 ; \
	fi
	rm -rf debian/fix.tmp;mkdir debian/fix.tmp
	cd debian/fix.tmp;unzip -q ../fex/usr/share/fex/htdocs/FIX.jar
	if (cd debian/fix.tmp/;md5sum -c ../htdocs.md5/$(DEB_NOEPOCH_VERSION).fix);then \
		echo "Correct FIX.jar md5sum found."; \
	else \
		echo "FIX.jar contents changed! Please run "; \
		echo "debian/rules update-md5 or add the following to your md5 cache:" ; \
		(cd debian/fix.tmp/;find . -type f|grep -v META-INF/DEBBUILD.DSA|sort|xargs -n 1 md5sum); \
		exit 1 ; \
	fi
	(cd debian/htdocs.md5;tar czf ../fex/usr/share/fex/.md5/htdocs.tgz *)
	dh_installcron
	dh_installdebconf
	dh_installexamples
	dh_installlogrotate
	dh_installman -pfex-utils debian/man-fex-utils/*.1
	dh_installman -pfex debian/man-fex/*.1
	dh_link
	dh_lintian
	dh_perl
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

update-md5:
	find htdocs/ -type f -maxdepth 1|sort|xargs -n 1 md5sum >debian/htdocs.md5/latest
	for file in FIX.jar version License;do \
		grep -v "  htdocs/$${file}" debian/htdocs.md5/latest >debian/htdocs.md5/latest.tmp; \
		mv debian/htdocs.md5/latest.tmp debian/htdocs.md5/latest ;\
	done
	mv debian/htdocs.md5/latest debian/htdocs.md5/$(DEB_NOEPOCH_VERSION)
	echo "New debian/htdocs.md5/$(DEB_NOEPOCH_VERSION):"
	cat debian/htdocs.md5/$(DEB_NOEPOCH_VERSION)
	rm -rf debian/fix.tmp;mkdir debian/fix.tmp
	cd debian/fix.tmp/;unzip -q ../../htdocs/FIX.jar;[ -f META-INF/FIX.DSA ]||find . -type f|grep -v META-INF/DEBBUILD.DSA|sort|xargs -n 1 md5sum >../htdocs.md5/$(DEB_NOEPOCH_VERSION).fix
	rm -rf debian/fix.tmp
	if [ ! -f htdocs.md5/$(DEB_NOEPOCH_VERSION).fix ];then \
		rm debian/htdocs.md5/$(DEB_NOEPOCH_VERSION); \
		echo "Failed to update. FIX.jar isn't recompiled but upstream's version!"; \
		exit 1; \
	fi
	echo "New debian/htdocs.md5/$(DEB_NOEPOCH_VERSION).fix:"
	cat debian/htdocs.md5/$(DEB_NOEPOCH_VERSION).fix
	if [ -d .git ];then \
		if type git >/dev/null 2>&1;then \
			git add debian/htdocs.md5; \
			git commit -m 'New md5sums for $(DEB_NOEPOCH_VERSION)' \
				debian/htdocs.md5; \
		fi; \
	fi

binary-arch:

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install
