#!/usr/bin/make -f
INSTALL = install
INSTALL_PROGRAM = $(INSTALL) -p -o root -g root -m 755

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk
H_LDFLAGS = $(LDFLAGS)

CFLAGS += -Wall -g

CONFIGFLAGS =  --prefix=/usr
CONFIGFLAGS += --mandir=/usr/share/man
CONFIGFLAGS += --bindir=/bin
CONFIGFLAGS += LDFLAGS="-Wl,--as-needed -g $(H_LDFLAGS)"

CONFIGFLAGS += --infodir=/usr/share/info
CONFIGFLAGS += --enable-maildir-support
CONFIGFLAGS += --enable-max-jobtable-size=256
CONFIGFLAGS += --enable-etcdir=/etc/zsh
CONFIGFLAGS += --enable-function-subdirs
CONFIGFLAGS += --enable-site-fndir=/usr/local/share/zsh/site-functions
CONFIGFLAGS += --enable-fndir=/usr/share/zsh/functions
CONFIGFLAGS += --with-tcsetpgrp
CONFIGFLAGS += --with-term-lib="ncursesw tinfo"
CONFIGFLAGS += --enable-cap --enable-pcre
CONFIGFLAGS += --enable-readnullcmd=pager
CONFIGFLAGS += --enable-custom-patchlevel=Debian
CONFIGFLAGS += --enable-additional-fpath=/usr/share/zsh/vendor-functions,/usr/share/zsh/vendor-completions

STATICFLAGS =  --disable-dynamic
STATICFLAGS += --enable-ldflags=-static
STATICFLAGS += --disable-dynamic-nss

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
CONFIGFLAGS += --enable-zsh-debug
CONFIGFLAGS += --enable-zsh-mem-debug
CONFIGFLAGS += --enable-zsh-mem-warning
CONFIGFLAGS += --enable-zsh-secure-free
CONFIGFLAGS += --enable-zsh-hash-debug
endif

BUILT_USING=$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W libcap-dev libncursesw5-dev libpcre3-dev eglibc)

build: build-arch build-indep

build-arch: configure-stamp
	dh_auto_build -B obj --parallel
	HOME="$(CURDIR)/obj/testhome" dh_auto_test -B obj --parallel || true
	touch $@

build-indep: configure-stamp
	dh_auto_build -B obj --parallel -- pdf
	touch $@

build-static: configure-static-stamp
	dh_testdir
	dh_auto_build -B obj-static --parallel
	touch $@

build-debug: DEB_BUILD_OPTIONS+=debug
build-debug: build

configure:
	touch stamp-h.in
	dh_autoreconf
	dh_autotools-dev_updateconfig

configure-stamp: configure
	dh_testdir
	chmod 755 configure
	mkdir -p obj/testhome
	dh_auto_configure -B obj -- $(CONFIGFLAGS)
	touch $@

configure-static-stamp: configure
	dh_testdir
	mkdir obj-static
	dh_auto_configure -B obj-static -- $(CONFIGFLAGS) $(STATICFLAGS)
#	cp debian/static.conf obj-static/Src/mymods.conf
	sed -i -e 's/files.mdd link=no/files.mdd link=static/;s/stat.mdd link=no/stat.mdd link=static/' \
	    obj-static/config.modules
	touch $@

clean:
	dh_testdir
	rm -rf Doc
	cp -ra doc/Doc .
	dh_autotools-dev_restoreconfig
	dh_autoreconf_clean
	rm -f build build-static
	dh_auto_clean -B obj        --parallel
	dh_auto_clean -B obj-static --parallel
	dh_auto_clean               --parallel
	dh_clean
	rm -rf config.cache obj obj-static autom4te.cache

binary-indep: build-indep
	dh_testdir
	dh_testroot
	dh_install -pzsh-doc
	dh_installdocs -pzsh-doc -pzsh-beta -pzsh-beta-doc --link-doc=zsh-common
	dh_installdirs -pzsh-doc

# Work around texi2html unfriendliness
	sed -i -e 's/<BODY.*>/<BODY LANG="EN">/' debian/zsh-doc/usr/share/doc/zsh-doc/html/*.html

	dh_installchangelogs -pzsh-doc
	dh_link                        -pzsh-beta -pzsh-beta-doc
	dh_compress          -pzsh-doc -pzsh-beta -pzsh-beta-doc -Xpdf
	dh_fixperms          -pzsh-doc -pzsh-beta -pzsh-beta-doc
	dh_installdeb        -pzsh-doc -pzsh-beta -pzsh-beta-doc
	dh_md5sums           -pzsh-doc -pzsh-beta -pzsh-beta-doc
	dh_gencontrol        -pzsh-doc -pzsh-beta -pzsh-beta-doc
	dh_builddeb                    -pzsh-beta -pzsh-beta-doc
	dh_builddeb          -pzsh-doc -- -Zxz

binary-arch: binary-arch-dynamic binary-arch-static binary-arch-dev
binary-arch-dynamic: build-arch
	dh_testdir
	dh_testroot

	dh_installdirs -pzsh -pzsh-common -pzsh-dbg

	cd obj && $(MAKE) install.man DESTDIR=$(CURDIR)/debian/zsh-common
	nroff -mandoc -Tascii Doc/zshbuiltins.1 | colcrt - | \
	sed -e 's//{+|-}/' | ( cd debian/zsh-common/usr/share/zsh/help && \
	perl $(CURDIR)/Util/helpfiles )

# functions
	dh_installexamples -pzsh-common -X.distfiles
	cd debian/zsh-common/usr/share/doc/zsh-common/examples/; mv Example Functions
	sed -i -e '1!b;s:^#!.*[ /]zsh:#!/bin/zsh:;s#/usr/local/bin#/usr/bin#' \
	   debian/zsh-common/usr/share/doc/zsh-common/examples/Misc/*

	cd obj && $(MAKE) install.bin     DESTDIR=$(CURDIR)/debian/zsh INSTALL_PROGRAM='$(INSTALL_PROGRAM)'
	cd obj && $(MAKE) install.modules DESTDIR=$(CURDIR)/debian/zsh INSTALL_PROGRAM='$(INSTALL_PROGRAM)'
	cd obj && $(MAKE) install.fns     DESTDIR=$(CURDIR)/debian/zsh-common

	rm -r debian/zsh-common/usr/local

# move this to a non-root section; also drop it for cross-compiles
ifeq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	awk '/^#define FPATH_DIR/     { head=$$3;       gsub(/"/,"",head); };        \
             /^#define FPATH_SUBDIRS/ { $$1=""; $$2=""; gsub(/[" ]/,""); tail=$$0; } \
             END                      { printf "%s/%s\n", head, tail; };'            \
	    obj/Src/zshpaths.h >obj/Src/zshpaths.temp
	debian/zsh/bin/zsh -fc \
            'setopt extendedglob; \
	     for i in debian/zsh-common/'`cat obj/Src/zshpaths.temp`'; do \
		 zcompile -U -M $$i.zwc $$i/*~*.zwc(^/);                  \
		 chmod 644 $$i.zwc;                                       \
	     done'
endif

	mv debian/zsh/bin/zsh debian/zsh/bin/zsh5
	rm debian/zsh/bin/zsh-5.[0-9]*

	dh_strip   -pzsh              --dbg-package=zsh-dbg
	dh_link    -pzsh -pzsh-common             -pzsh-dbg
	dh_install -pzsh -pzsh-common             -pzsh-dbg

	sed -i -e 's,^local HELPDIR=.*,local HELPDIR=$${HELPDIR:-/usr/share/zsh/help},; s,:-more,:-/usr/bin/pager,;' \
		debian/zsh-common/usr/share/zsh/functions/Misc/run-help
	sed -i -e '1!b;s:^#!.*[ /]zsh:#!/bin/zsh:;s#/usr/local/bin#/usr/bin#;' \
		`find debian/zsh-common/usr/share/zsh/functions -type f`
	chmod 755 debian/zsh-common/usr/share/zsh/functions/Misc/checkmail \
	          debian/zsh-common/usr/share/zsh/functions/Misc/harden    \
	          debian/zsh-common/usr/share/zsh/functions/Misc/run-help  \
	          debian/zsh-common/usr/share/zsh/functions/Misc/zkbd      \
	          debian/zsh-common/usr/share/zsh/functions/Misc/zcalc

	dh_shlibdeps         -pzsh              -pzsh-dbg --   \
		-dDepends    debian/zsh/bin/*                  \
		-dRecommends debian/zsh/usr/lib/zsh/*/zsh/*.so

	dh_installmenu             -pzsh-common
	dh_installdocs       -pzsh -pzsh-common -pzsh-dbg --link-doc=zsh-common
	dh_installchangelogs       -pzsh-common -pzsh-dbg ChangeLog
	dh_lintian           -pzsh -pzsh-common -pzsh-dbg
	dh_compress          -pzsh -pzsh-common -pzsh-dbg
	dh_fixperms          -pzsh -pzsh-common -pzsh-dbg
	dh_installdeb        -pzsh -pzsh-common -pzsh-dbg
	dh_md5sums           -pzsh -pzsh-common -pzsh-dbg
	dh_gencontrol        -pzsh -pzsh-common -pzsh-dbg
	dh_builddeb          -pzsh -pzsh-common           -- -Zxz
	dh_builddeb                             -pzsh-dbg

binary-arch-static: build-static
	dh_testdir
	dh_testroot

	dh_installdirs -pzsh-static
	dh_lintian     -pzsh-static

	# Do not symlink /usr/share/doc/zsh-static to
	# /usr/share/doc/zsh-common because zsh-static has a uniqe,
	# generated README.Debian
	dh_installdocs -pzsh-static
	awk 'BEGIN { print "The following modules are statically-compiled into the static zsh binary:\n"; } /link=static/ { printf "%s (%s %s)\n", substr($$1,6), $$4, $$5; }' obj-static/config.modules >debian/zsh-static/usr/share/doc/zsh-static/README.Debian

	dh_install     -pzsh-static
	dh_strip       -pzsh-static
	mv debian/zsh-static/bin/zsh debian/zsh-static/bin/zsh5-static

	dh_installchangelogs -pzsh-static
	dh_link              -pzsh-static
	dh_compress          -pzsh-static
	dh_fixperms          -pzsh-static
	dh_installdeb        -pzsh-static
	dh_md5sums           -pzsh-static
	dh_gencontrol        -pzsh-static -- -VBuilt-Using="$(BUILT_USING)"
	dh_builddeb          -pzsh-static -- -Zxz

binary-arch-dev: build-arch
	dh_testdir
	dh_testroot

	dh_installdirs       -pzsh-dev
	dh_install           -pzsh-dev
	cd debian/zsh-dev/usr/share/aclocal; mv aczshoot.m4 zshoot.m4
	dh_installdocs       -pzsh-dev --link-doc=zsh-common
	dh_installchangelogs -pzsh-dev
	dh_compress          -pzsh-dev
	dh_fixperms          -pzsh-dev
	dh_installdeb        -pzsh-dev
	dh_md5sums           -pzsh-dev
	dh_gencontrol        -pzsh-dev
	dh_builddeb          -pzsh-dev

binary: binary-indep binary-arch binary-arch-static binary-arch-dev

prebuild:
	Util/preconfig
	./configure
	make -C Doc
	make distclean
	rm -rf autom4te.cache

.PHONY: binary binary-arch binary-indep clean binary-arch-dynamic binary-arch-static prebuild binary-arch-dev
