#!/usr/bin/make -f
# $Id$

# Debian rules file for xterm source package

# Copyright 2005 David Martínez Moreno
#
# Licensed under the GNU General Public License, version 2.  See the file
# /usr/share/common-licenses/GPL or <http://www.gnu.org/copyleft/gpl.txt>.

# debhelper
export DH_OPTIONS

DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CXXFLAGS += -O0
else
	CXXFLAGS += -O2
endif

# Figure out who's building this package.
BUILDER:=$(shell echo $${DEBEMAIL:-$${EMAIL:-$$(echo $$LOGNAME@$$(cat /etc/mailname 2> /dev/null))}})

# Set some Imake variables for the regular and server-debugging builds.
TIMESTAMP:=$(shell env TZ=UTC date +%Y%m%d%H%M%S)

HTML2TEXT:=lynx -dump -nolist

STAMP_DIR:=stampdir

DEBINSTALLTREE:=$(CURDIR)/debian/tmp

# Set up the $(STAMP_DIR) directory.
.PHONY: stampdir
stampdir: $(STAMP_DIR)/stampdir
$(STAMP_DIR)/stampdir:
	mkdir $(STAMP_DIR)
	>$@

# Set up the package build directory as quilt expects to find it.
.PHONY: prepare
prepare: $(STAMP_DIR)/prepare
$(STAMP_DIR)/prepare: $(STAMP_DIR)/stampdir
	mkdir $(STAMP_DIR)/patches $(STAMP_DIR)/log
	echo 2 >$(STAMP_DIR)/patches/.version
	ln -s debian/patches patches
	ln -s $(STAMP_DIR)/patches .pc
	>$@

# Apply all patches to the upstream source.
.PHONY: patch
patch: $(STAMP_DIR)/patch
$(STAMP_DIR)/patch: $(STAMP_DIR)/prepare
	@echo -n "Applying patches... "
	@if quilt push -a -v > $(STAMP_DIR)/log/patch 2>&1; then \
	  echo "successful."; \
	else \
	  echo "failed! (Check $(STAMP_DIR)/log/patch for details)"; \
	  exit 1; \
	fi; \

	touch $@

# Revert all patches to the upstream source.
.PHONY: unpatch
unpatch: 
	@if [ -e $(STAMP_DIR)/patches/applied-patches ]; then \
	  echo -n "Unapplying patches... "; \
	  if quilt pop -a > $(STAMP_DIR)/log/unpatch 2>&1; then \
	    echo " successful."; \
	  else \
	    echo " failed! (Check $(STAMP_DIR)/log/unpatch for details)"; \
	    exit 1; \
	  fi; \
	fi

configure: $(STAMP_DIR)/patch $(STAMP_DIR)/configure
$(STAMP_DIR)/configure: $(STAMP_DIR)/patch
	dh_testdir
	./configure --enable-logging --enable-wide-chars --enable-luit \
		--enable-256-color \
		--prefix=/usr --exec-prefix=/usr --mandir=/usr/share/man \
		--with-app-defaults=/etc/X11/app-defaults \
		--disable-imake --enable-narrowproto --enable-exec-xterm \
		$(confflags)
	touch $@


.PHONY: build
build: $(STAMP_DIR)/build
$(STAMP_DIR)/build: $(STAMP_DIR)/configure debian/local/xterm.faq.gz
	dh_testdir
	$(MAKE)
	touch $@

.PHONY: clean
clean: unpatch
	dh_testdir
	dh_testroot
	rm -f .pc patches
	rm -rf $(STAMP_DIR)
	-$(MAKE) distclean
	dh_clean debian/local/xterm.faq.gz

.PHONY: install
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	# General installation
	$(MAKE) install mandir=/usr/share/man/man1 libdir=/etc DESTDIR=$(DEBINSTALLTREE)
	
	## Install lxterm command and manual page.
	install -m 755 debian/local/lxterm $(DEBINSTALLTREE)/usr/bin
	install -m 755 debian/local/lxterm.1 $(DEBINSTALLTREE)/usr/share/man/man1
	## Install koi8rxterm command, manual page, and app-defaults file.
	install -m 755 debian/local/koi8rxterm $(DEBINSTALLTREE)/usr/bin
	install -m 755 debian/local/koi8rxterm.1 $(DEBINSTALLTREE)/usr/share/man/man1
	install -d $(DEBINSTALLTREE)/etc/X11/app-defaults
	install -m 644 debian/local/KOI8RXTerm $(DEBINSTALLTREE)/etc/X11/app-defaults
	## Install manual page for uxterm.
	install -m 644 debian/local/uxterm.1 $(DEBINSTALLTREE)/usr/share/man/man1
	## Install terminfo and termcap precompiled definitions.
	install -d $(DEBINSTALLTREE)/usr/lib
	install -m 644 termcap  $(DEBINSTALLTREE)/usr/lib/xterm.termcap
	install -m 644 terminfo $(DEBINSTALLTREE)/usr/lib/xterm.terminfo
	dh_installdocs
	dh_installchangelogs
	dh_installmenu
	dh_install --sourcedir=debian/tmp --list-missing
	# Lintian overrides.
	cp debian/xterm.overrides $(CURDIR)/debian/xterm/usr/share/lintian/overrides/xterm

# Generate plain text XTerm FAQ from HTML.
debian/local/xterm.faq.gz: debian/local/xterm.faq.html
	$(HTML2TEXT) $< >debian/local/xterm.faq
	# dh_compress doesn't recognize this file extension, so we must
	# compress it manually.
	gzip -9f debian/local/xterm.faq

.PHONY: binary
binary: binary-arch

.PHONY: binary-arch
binary-arch: install
	dh_testdir
	dh_testroot
	dh_strip
	dh_compress
	dh_fixperms
	chown :utmp debian/xterm/usr/bin/xterm
	chmod g+s debian/xterm/usr/bin/xterm
	dh_installdeb
	dh_makeshlibs
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# this target is required by policy
.PHONY: binary-indep
binary-indep:

# Note: This rule is for Debian package maintainers' convenience, and is not
# needed for conventional build scenarios.
.PHONY: patch-audit
patch-audit: prepare unpatch
	touch stampdir/log/patch; \
	FUZZY=no; \
	while [ -n "`quilt next`" ]; do \
	  RESULT=`quilt push -v | tee -a stampdir/log/patch | grep ^Hunk | sed 's/^Hunk.*\(succeeded\|FAILED\).*/\1/'`;\
	  case "$$RESULT" in \
	    succeeded) \
	      echo "The patch is fuzzy: `quilt top`"; \
		  echo "The patch is fuzzy: `quilt top`" > stampdir/log/`quilt top`; \
	      FUZZY=yes; \
	    ;; \
	    FAILED) \
	      echo "The patch is broken: `quilt next`"; \
		  echo "The patch is fuzzy: `quilt next`" > stampdir/log/`quilt next`; \
	      exit 1; \
	    ;; \
	  esac; \
	done; \
	if [ $$FUZZY = "yes" ]; then \
	  echo "There were fuzzy patches. Please Fix."; \
	  exit 1; \
	fi

# vim:set ai noet sw=8 ts=8 tw=0:
