#! /usr/bin/make -f
#
# debian/rules file - for troffcvt.
#
# Copyright 1996, 1997 by Ben Pfaff.
# Copyright 2001-2022 by Colin Watson.
# Original Makefile by Ian Jackson.
#
# This Makefile is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This Makefile is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

export package=troffcvt

# It's really not a good idea to hard-code this, but we're guaranteed
# a FSSTND filesystem under Debian GNU/Linux so we might as well take
# advantage of it.  NOTE: Our utilities must come before /usr/bin,
# otherwise if the package is already installed there may be
# interference.
export PATH=$(CURDIR)/debian/build:/bin:/usr/bin:/usr/bin/X11

export TROFFCVT_LIB_DIR=$(CURDIR)/debian/troffcvt/usr/share/troffcvt

docdir=usr/share/doc/$(package)

# Most packages depend on these tools.
tools = debian/build/_x_ imboot/_b_ msub/_b_ WRPRC/_b_

include /usr/share/dpkg/buildflags.mk

#
# Main targets
#

build-indep:

build-arch:
	dh_testdir
	$(MAKE) -f debian/rules _b_
	touch $@

build: build-indep build-arch

clean:
	dh_testdir
	# Exclude several files that are part of upstream tarballs.
	dh_clean \
		-XWRPRC/config/Imake.rules~ \
		-XWRPRC/config/hp.cf.orig \
		-Xpost/rtf-fonts.orig \
		-Xtblcvt/tblcvt.pl~

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Install into debian/troffcvt.
	make install install.man \
		LIBDIR=$(CURDIR)/debian/troffcvt/usr/share/troffcvt \
		USRLIBDIR=$(CURDIR)/debian/troffcvt/usr/share/troffcvt \
		LOCALROOTDIR=$(CURDIR)/debian/troffcvt/usr \
		MANROOT=$(CURDIR)/debian/troffcvt/usr/share/man \

	# Convert documentation to HTML.
	(cd doc; \
	for d in *.ms; do \
	    soelim $$d | tblcvt | troff2html -ms tmac.wrprc -a tc.wrprc-html - \
		> ../debian/troffcvt/usr/share/doc/$(package)/`echo $$d | sed -e 's/\.ms/\.html/'`; \
	done)

binary-indep:	build
# There are no architecture-independent files.

binary-arch:	build install debian/docs
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress $(docdir)/Changes-1.01 $(docdir)/Changes-1.02 \
		    $(docdir)/Changes-1.03 $(docdir)/Changes-1.04
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

#
# Miscellaneous
#

binary:		binary-indep binary-arch


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

#
# Build targets.
#
define build-link-farm
set -e; \
file_list=`find subdir -name \*.[ah] -o -perm /111 -a -type f`; \
for d in $$file_list; do \
	ln -sf ../../$$d debian/build/; \
done
endef

debian/build/_x_:
	mkdir -p debian/build
	touch $@

imboot/_b_: debian/build/_x_
	sed imboot/imboot.pl > debian/build/imboot \
		-e "s#\$$(PERLPATH)#`which perl`#" \
		-e "s#\$$(CONFIGROOTDIR)#`pwd`/config#"
	chmod a+x debian/build/imboot
	touch imboot/_b_

msub/_b_: debian/build/_x_ imboot/_b_
	gcc $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) msub/msub.c -o debian/build/msub
	touch msub/_b_

WRPRC/_b_: debian/build/_x_ msub/_b_
	sed WRPRC/config/site.def > WRPRC/config/site.def.temp \
		-e "s%^#define ConfigRootDir .*\$$%#define ConfigRootDir \"`pwd`/config\"%"
	mv WRPRC/config/site.def.temp WRPRC/config/site.def
	(set -e; cd WRPRC; make World; make install)
	touch WRPRC/_b_

portlib/_b_: $(tools)
	make -C portlib World \
		INCLUDES="-I $(CURDIR)/debian/build" \
		CCOPTIONS="$(CPPFLAGS) $(CFLAGS)" \
		LDFLAGS="$(LDFLAGS) -L $(CURDIR)/debian/build"
	$(build-link-farm:subdir=portlib)
	touch $@

etm/_b_: $(tools)
	make -C etm World \
		INCLUDES="-I $(CURDIR)/debian/build" \
		CCOPTIONS="$(CPPFLAGS) $(CFLAGS)" \
		LDFLAGS="$(LDFLAGS) -L $(CURDIR)/debian/build"
	$(build-link-farm:subdir=etm)
	touch $@

tokenscan/_b_: $(tools)
	make -C tokenscan World \
		INCLUDES="-I $(CURDIR)/debian/build" \
		CCOPTIONS="$(CPPFLAGS) $(CFLAGS)" \
		LDFLAGS="$(LDFLAGS) -L $(CURDIR)/debian/build"
	$(build-link-farm:subdir=tokenscan)
	touch $@

memmgr/_b_: $(tools)
	make -C memmgr World \
		INCLUDES="-I $(CURDIR)/debian/build" \
		CCOPTIONS="$(CPPFLAGS) $(CFLAGS)" \
		LDFLAGS="$(LDFLAGS) -L $(CURDIR)/debian/build"
	$(build-link-farm:subdir=memmgr)
	touch $@

_b_: portlib/_b_ etm/_b_ tokenscan/_b_ memmgr/_b_
	make World \
		INCLUDES="-I $(CURDIR)/debian/build" \
		CCOPTIONS="$(CPPFLAGS) $(CFLAGS)" \
		LDFLAGS="$(LDFLAGS) -L $(CURDIR)/debian/build"
	$(build-link-farm:subdir=troffcvt)
	$(build-link-farm:subdir=tblcvt)
	$(build-link-farm:subdir=post)
	touch $@

# Local Variables:
# mode:makefile
# End:
