#! /usr/bin/make -f
build clean install binary-arch binary-indep binary:
	dh --builddirectory=debian/build $@

export DH_OPTIONS

CC  = gcc
CXX = g++
ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS = -O2 -g -Wall
else
CFLAGS = -g -Wall
endif

# From Makefile.in
version  = $(shell cat VERSION)
revision = $(shell sed -e 's/^0$$//' -e 's/^[1-9].*$$/.&/' REVISION)
upstream = $(version)$(revision)
datadir  = usr/share/groff/$(upstream)

gbtmp = debian/groff-base
gtmp  = debian/groff

ifeq (,$(filter no-groff-x11,$(DEB_BUILD_OPTIONS)))
GROFF_X11 := yes
else
GROFF_X11 := no
endif

ifeq ($(GROFF_X11),no)
  confflags += --without-x
endif

override_dh_auto_configure:
	-rm -f config.log config.cache
	CC="$(CC)" CXX="$(CXX)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" \
		YACC='bison -y' PSPRINT=lpr PAGE=A4 \
		dh_auto_configure -- $(confflags) \
			--with-appresdir=/etc/X11/app-defaults

override_dh_auto_build:
	touch configure		# avoid autoconf build-dependency
	$(MAKE) -C debian/build \
		extratmacdirs=/usr/share/groff/tmac \
		docdir=\$${prefix}/share/doc/groff-base
	GROFF_NO_SGR=1 $(MAKE) -C debian/build/doc \
		meintro.txt meref.txt pic.txt

override_dh_auto_install:
	dh_auto_install -- \
		docdir=\$${prefix}/share/doc/groff-base \
		INSTALL_INFO=:

%: %.in
	sed -e 's/@VERSION@/$(upstream)/g' $< > $@

override_dh_install:
	$(MAKE) -f debian/rules \
		debian/groff-base.dirs \
		debian/groff-base.install \
		debian/groff.install
	dh_install --fail-missing
	# Debian additions to *.local
	cat debian/mandoc.local >> $(gbtmp)/etc/groff/man.local
	cat debian/mandoc.local >> $(gbtmp)/etc/groff/mdoc.local
	# Scripts should be executable
	chmod +x $(gbtmp)/$(datadir)/font/devps/generate/afmname \
		 $(gbtmp)/$(datadir)/font/devps/generate/symbol.sed
	chmod +x $(gtmp)/$(datadir)/font/devdvi/generate/CompileFonts \
		 $(gtmp)/$(datadir)/font/devlj4/generate/special.awk

override_dh_installdocs:
	dh_installdocs -pgroff-base
	dh_installdocs -pgroff -Xcopyright --link-doc=groff-base

override_dh_installchangelogs:
	dh_installchangelogs -pgroff-base

override_dh_builddeb:
	dh_builddeb
ifneq ($(GROFF_X11),yes)
	@echo
	@echo "=========================================================="
	@echo "WARNING! groff has been built without X support."
	@echo "Please do not upload these packages to the Debian archive!"
	@echo "=========================================================="
	@echo
endif

# You only need to run this immediately after checking out the package from
# revision control.
quilt-setup:
	[ ! -d .pc ]
	set -e; for patch in $$(quilt series | tac); do \
		patch -p1 -R --no-backup-if-mismatch <"debian/patches/$$patch"; \
	done
	quilt push -a

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