#! /usr/bin/make -f
#
# rules to debianize groff

export DH_OPTIONS

CC  = gcc
CXX = g++
ifeq (,$(findstring 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)

fontpath := /$(datadir)/font:/usr/local/share/groff/font
fontpath := $(fontpath):/usr/local/lib/font:/usr/lib/font

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

devbase = ascii ascii8 latin1 nippon ps utf8
devx11  = X100 X100-12 X75 X75-12

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

ifeq ($(GROFF_X11),yes)
BUILD_TARGETS  := build-groff build-groff-x11
else
BUILD_TARGETS  := build-groff
endif
BINARY_TARGETS := binary-groff-base binary-groff
DH_EXCLUDE     := -Ngroff-x11

configure: configure-stamp
configure-stamp: patch
	if [ ! -f Makefile.clean ]; then cp -p Makefile Makefile.clean; fi
	-rm -f config.log config.cache
	CC="$(CC)" CXX="$(CXX)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" \
		PSPRINT=lpr PAGE=A4 \
		./configure --prefix=/usr --mandir=\$${prefix}/share/man \
			    --infodir=\$${prefix}/share/info --enable-multibyte
	touch $@

src/xditview/Makefile: configure
	# This strange BuildXaw tweak reminds imake that we're building with
	# libxaw7, not libxaw8 (per the build-dependencies) and that
	# therefore we don't need to link against libxp.
	cd src/xditview && xmkmf -DBuildXaw=NO

build-arch: $(BUILD_TARGETS)
build-indep:
build: build-arch build-indep

build-groff: configure
	dh_testdir
	touch configure		# avoid autoconf build-dependency
	$(MAKE) extratmacdirs=/usr/share/groff/tmac
	GROFF_NO_SGR=1 $(MAKE) -C doc meintro.txt meref.txt pic.txt
	touch $@

ifeq ($(GROFF_X11),yes)
build-groff-x11: src/xditview/Makefile
	dh_testdir
	touch configure		# avoid autoconf build-dependency
	$(MAKE) -C src/xditview depend
	$(MAKE) -C src/xditview FONTPATH=$(fontpath)
	touch $@
endif

clean: unpatch
	dh_testdir
	dh_testroot
	touch configure		# avoid autoconf build-dependency
	rm -f man/index.* 
	find . -name tags | xargs -r rm -f
	find . -name '*.cc.combine' | xargs -r rm -f
	-$(MAKE) -C doc -i extraclean
	-$(MAKE) -C src/xditview extraclean
	-$(MAKE) -i realclean extraclean
	rm -f src/xditview/Imakefile src/xditview/Makefile
	dh_clean configure-stamp build-stamp
	rm -f debian/groff-base.files debian/groff-base.links
	rm -f config.log config.cache config.status
	if [ -f Makefile.clean ]; then mv -f Makefile.clean Makefile; fi

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

patch: patch-stamp
patch-stamp:
	if [ -e unpatch-stamp ] || [ ! -e patch-stamp ] ; then \
		patch -d $(CURDIR) -p1 < debian/gcc-4.2.patch ; \
	fi
	-rm -f unpatch-stamp
	touch $@

unpatch: unpatch-stamp
unpatch-stamp: 
	if [ ! -e unpatch-stamp ] && [ -e patch-stamp ] ; then \
		patch -d $(CURDIR) -p1 -R < debian/gcc-4.2.patch ; \
	fi
	-rm -f patch-stamp
	touch $@

install: DH_OPTIONS=
install: build debian/groff-base.files
	dh_testdir
	dh_testroot
	dh_clean -k -a
	dh_installdirs -a
	$(MAKE) install prefix=$(CURDIR)/$(gtmp)/usr \
			docdir=$(CURDIR)/$(gtmp)/usr/share/doc/groff-base \
			INSTALL_INFO=:
	dh_movefiles -pgroff-base --sourcedir=debian/groff
	dh_installdirs -pgroff-base $(datadir)/font
	set -e; for x in $(devbase); do \
		mv $(gtmp)/$(datadir)/font/dev$$x $(gbtmp)/$(datadir)/font/; \
	done
ifeq ($(GROFF_X11),yes)
	install -m755 src/xditview/gxditview $(gtmp)/usr/bin/gxditview
	install -m644 src/xditview/GXditview.ad \
		$(gtmp)/etc/X11/app-defaults/GXditview
else
	set -e; for x in $(devx11); do \
		rm -rf $(gtmp)/$(datadir)/font/dev$$x; \
	done
endif

binary-indep:

binary-groff-base: DH_OPTIONS=-pgroff-base
binary-groff-base: install debian/groff-base.links
	dh_testdir
	dh_testroot
	# Conffiles should go in /etc
	mv $(gbtmp)/usr/share/groff/site-tmac $(gbtmp)/etc/groff
	# 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
	dh_installdocs
	dh_installmime
	dh_installchangelogs ChangeLog
	dh_link

binary-groff: DH_OPTIONS=-pgroff
binary-groff: install
	dh_testdir
	dh_testroot
	# Scripts should be executable
	chmod +x $(gtmp)/$(datadir)/font/devdvi/generate/CompileFonts
	# Now in groff-base
	rmdir $(gtmp)/usr/lib/groff/site-tmac   || true
	rmdir $(gtmp)/usr/share/groff/site-tmac || true
	# 'make install' creates this directory when installing documentation.
	dh_link usr/share/doc/groff-base usr/share/doc/groff
	dh_installdocs -Xcopyright
ifeq ($(GROFF_X11),yes)
	install -m644 src/xditview/ChangeLog \
		      $(gtmp)/usr/share/doc/groff/ChangeLog.gxditview
	dh_installman src/xditview/gxditview._man
endif
	dh_installinfo doc/groff doc/groff-[0-9]*
	dh_link

binary-arch: DH_OPTIONS=-a $(DH_EXCLUDE)
binary-arch: $(BINARY_TARGETS)
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	maemo-optify
	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

binary: binary-indep binary-arch

.PHONY: configure build build-arch build-indep clean install
.PHONY: binary binary-arch binary-indep binary-groff-base binary-groff
