#!/usr/bin/make -f
# debian/rules for alpine

# Used in get-orig-source, upstreams tarball.
MD5SUM=02dad85c1be80ce020206f222ecf5ac8

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

override_dh_auto_configure:
	dh_auto_configure -- --with-system-pinerc=/etc/pine.conf \
			     --with-system-fixed-pinerc=/etc/pinerc.fixed \
			     --with-passfile=.pine-passfile \
			     --with-smtp-msa=/usr/sbin/sendmail \
			     --with-debug-level=0 \
			     --without-tcl \
			     --with-krb5 \
			     --with-krb5-dir=/usr

override_dh_auto_build:
	# force cross compilers onto make as they are not propagated from configure
	dh_auto_build --buildsystem=makefile

override_dh_install-arch:
	dh_install --arch
	# Rename pico to pico.alpine.
	# alpine-pico.install puts this in the alpine-pico package.
	mv debian/alpine-pico/usr/bin/pico debian/alpine-pico/usr/bin/pico.alpine

override_dh_installdocs-indep:
	dh_installdocs --indep
	# Change /usr/local/ config paths to /etc
	sed -i  -e s@/usr/local/lib/pine@/etc/pine@g \
		-e s@/usr/local/pine@/etc/pine@g \
		debian/alpine-doc/usr/share/doc/alpine-doc/tech-notes/*

override_dh_installman-arch:
	dh_installman --arch
	mv debian/alpine-pico/usr/share/man/man1/pico.1 debian/alpine-pico/usr/share/man/man1/pico.alpine.1

override_dh_missing:
	dh_missing --fail-missing

override_dh_strip:
	dh_strip --dbgsym-migration='alpine-dbg (<< 2.20+dfsg1-5~)'

override_dh_clean:
	dh_clean -Xinclude/config.h.in~ -Xweb/cgi/alpine/2.0/img/cbn/msglist.gif.bak

%:
	dh $@

# get-orig-source to drop dlls and exe files, fix license-problem-non-free-RFC-BCP78.
upstream_version ?= $(shell dpkg-parsechangelog | sed -rne 's/^Version: ([0-9.]+)(\+dfsg\d+)?.*$$/\1/p')
dfsg_version = $(upstream_version)+dfsg1
pkg = alpine

get-orig-source:
	uscan --noconf --force-download --rename --download-current-version --destdir=.
	# Check md5sum against known good hash.
	md5sum $(pkg)_$(upstream_version).orig.tar.xz | grep -q $(MD5SUM)
	tar -xf $(pkg)_$(upstream_version).orig.tar.xz
	mv $(pkg)-$(upstream_version) $(pkg)-$(dfsg_version)
	## No obvious source code, not needed to build.
	cd $(pkg)-$(dfsg_version) ; rm -rf ldap/binaries/
	cd $(pkg)-$(dfsg_version) ; rm -f alpine/ldap32.dll
	## source-contains-unsafe-symlink
	cd $(pkg)-$(dfsg_version) ; rm -f web/detach web/bin/tclsh web/cgi/detach
	## http://wiki.debian.org/NonFreeIETFDocuments
	cd $(pkg)-$(dfsg_version) ; rm -rf imap/docs/draft/ imap/docs/rfc/
	XZ_OPT="-f6" tar -Jcf $(pkg)_$(dfsg_version).orig.tar.xz $(pkg)-$(dfsg_version)
	rm -rf $(pkg)-$(dfsg_version)
