#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Cristoph Lameter.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

SHELL = /bin/bash
DPKG_EXPORT_BUILDFLAGS = 1
export DEB_BUILD_MAINT_OPTIONS := hardening=+all qa=+bug reproducible=+all

include /usr/share/dpkg/buildflags.mk

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

DESKTOP_VENDOR  = dickey

PKG_SUFFIX	= -dev
PKG_CLASS	= XTermDev

PACKAGE		:= $(shell dpkg-parsechangelog| \
			sed -n 's/^Source: \(.*\)$$/\1/p')

PKG_APPDEFAULTS	:= /etc/X11/app-defaults
PKG_DESKTOP	:= /usr/share/applications

DSTDIR		:= $(CURDIR)/debian/$(PACKAGE)
MY_DESKTOP	:= $(DSTDIR)/usr/share/applications

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
DEBOP=--enable-debug
else
DEBOP=
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -O0
else
        CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
        INSTALL_PROGRAM += -s
endif

configure_flags = \
	--host=$(DEB_HOST_GNU_TYPE) \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--program-suffix=$(PKG_SUFFIX) \
	--prefix=/usr \
	--libexecdir=\$${prefix}/lib \
	--mandir=\$${prefix}/share/man \
	--sysconfdir=/etc/$(PACKAGE) \
	--localstatedir=/var \
	--libdir=/etc/$(PACKAGE) \
	--with-app-class=$(PKG_CLASS) \
	--without-xterm-symlink \
	--disable-imake \
	--enable-dabbrev \
	--enable-dec-locator \
	--enable-exec-xterm \
	--enable-hp-fkeys \
	--enable-load-vt-fonts \
	--enable-logfile-exec \
	--enable-logging \
	--enable-mini-luit \
	--enable-regis-graphics \
	--enable-sco-fkeys \
	--enable-status-line \
	--enable-toolbar \
	--enable-xmc-glitch \
	--with-app-defaults=$(PKG_APPDEFAULTS) \
	--with-icondir=\$${prefix}/share/icons \
	--with-pixmapdir=\$${prefix}/share/pixmaps \
	--with-own-terminfo=\$${prefix}/share/terminfo \
	--with-icon-theme \
	--with-terminal-type=xterm-new \
	--with-utempter \
	--with-icon-name=mini.xterm \
	--with-xpm ${DEBOP}

configure: configure-stamp
configure-stamp:
	dh_testdir

	# Update config.guess, config.sub
	dh_update_autotools_config

	# Configure xterm-dev
	dh_auto_configure -- $(configure_flags)

	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir

	$(MAKE)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	[ ! -f Makefile ] || $(MAKE) distclean

	dh_clean

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) install \
		DESTDIR=$(CURDIR)/debian/$(PACKAGE)

	touch install-stamp

install: install-indep install-arch
install-indep:

install-arch:
	dh_testdir
	dh_testroot
	dh_prep -a
	dh_installdirs -a

	$(MAKE) install-bin \
		DESTDIR=$(DSTDIR)

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot

	# This overwrites the Debian package's copy of app-defaults and icons.
	# But the resources are not the same; they are patched.
	sed -i -f package/debian/color.sed XTerm-col.ad
	sed -i -f package/debian/xterm-xres.sed XTerm.ad

	$(MAKE) install-app \
		install-icon \
		install-man \
		DESTDIR=$(DSTDIR)

	# Follow-up with a check against the installed resource files.
	( cd $(DSTDIR)$(PKG_APPDEFAULTS) \
	  && $(SHELL) -c 'for p in *; do \
	  	test -f $(PKG_APPDEFAULTS)/$$p && \
	  	diff -u $(PKG_APPDEFAULTS)/$$p $$p; \
		done' ; \
	  exit 0 )

	# The Debian package stopped installing xterm's desktop files; a
	# subsequent revision added that back with limitations.  xterm's
	# configure script checks for categories which are used by other
	# terminal emulators.  The Debian package overrides that to a single
	# category.
	# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=486317
	$(MAKE) install-desktop \
		DESKTOP_FLAGS="--dir $(MY_DESKTOP)"

	( cd $(DSTDIR)$(PKG_DESKTOP) \
	  && $(SHELL) -c 'for p in *;do \
	  	test -f $(PKG_DESKTOP)/$$p && \
		diff -u $(PKG_DESKTOP)/$$p $$p; \
	  	test -n "$(PKG_SUFFIX)" && mv $$p `basename $$p .desktop`$(PKG_SUFFIX).desktop; \
		done' ; \
	  exit 0 )

	dh_icons

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_lintian
	dh_installdocs
	dh_installmenu
	dh_installmime
	dh_installexamples tektests vttests
	dh_installchangelogs
	dh_install
	dh_link
	dh_strip
	dh_compress -Xexamples
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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