#! /usr/bin/make -f

# The name of the package
package = man-db
dtmp    = debian/$(package)

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS = -O2 -g -Wall
LDFLAGS = -g
else
CFLAGS = -g -Wall
LDFLAGS = -g
endif

DEB_BUILD_GNU_TYPE = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CONFARGS = --host=$(DEB_HOST_GNU_TYPE)
endif

ifeq (,$(wildcard /usr/bin/po2debconf))
PO2DEBCONF := no
MINDEBCONFVER := 0.5
else
PO2DEBCONF := yes
MINDEBCONFVER := 1.2.0
endif

GNUmakefile:
# create the makefile needed both from build and clean rules
	touch configure stamp-h.in	# avoid autoconf build-dependency
	test -x configure || chmod +x configure 
	./configure --prefix=/usr --enable-setuid --with-db=gdbm \
	            --enable-undoc='man 7 undocumented' \
	            --with-device=latin1 --enable-mb-groff \
	            --with-config-file=/etc/manpath.config $(CONFARGS)

build: build-stamp
build-stamp: GNUmakefile
# Builds the binary package.
	dh_testdir
	$(MAKE) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
	$(MAKE) -C manual SOELIM=../zsoelim/zsoelim
	touch build-stamp

clean:
	dh_testdir
	touch configure stamp-h.in	# avoid autoconf build-dependency
	-$(MAKE) -i distclean
	-$(MAKE) -C manual -i distclean
	-rm -f build-stamp install-stamp man/index.*
	-rm -f */*.i
ifeq ($(PO2DEBCONF),yes)
	# Compatibility with woody.
	echo '1 popular' > debian/po/output
	po2debconf debian/templates.master > debian/templates
	rm -f debian/po/output
endif
	dh_clean
	-cd po && rm -f stamp-cat-id cat-id-tbl.c

install: install-stamp
install-stamp: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) install DESTDIR=$(CURDIR)/$(dtmp)
	# Debian man-db used to put potentially setuid executables in a
	# private directory in order to do wrapper magic. The wrapper isn't
	# used any more, but migration is complicated enough that these stay
	# in /usr/lib/man-db (for now).
	mv $(dtmp)/usr/bin/man			$(dtmp)/usr/lib/man-db/man
	mv $(dtmp)/usr/bin/mandb		$(dtmp)/usr/lib/man-db/mandb
	install -m755 tools/chconfig		$(dtmp)/usr/share/$(package)
	# now do manual changes needed by debian
	# change locale names
	mv $(dtmp)/usr/share/man/de_DE.88591	$(dtmp)/usr/share/man/de
	mv $(dtmp)/usr/share/man/es_ES.88591	$(dtmp)/usr/share/man/es
	mv $(dtmp)/usr/share/man/fr_FR.88591	$(dtmp)/usr/share/man/fr
	mv $(dtmp)/usr/share/man/it_IT.88591	$(dtmp)/usr/share/man/it
	mv $(dtmp)/usr/share/man/ja_JP.eucJP	$(dtmp)/usr/share/man/ja
	rm -rf $(dtmp)/usr/share/man/de_DE.ascii
	touch install-stamp

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	install -m644 manual/man_db.cat \
		$(dtmp)/usr/share/doc/man-db/man-db-manual.txt
	install -m644 manual/man_db.dvi \
		$(dtmp)/usr/share/doc/man-db/man-db-manual.dvi
	install -m644 manual/man_db.ps \
		$(dtmp)/usr/share/doc/man-db/man-db-manual.ps
	dh_installexamples docs/manpage.example*
	install -m644 $(dtmp)/etc/manpath.config \
		$(dtmp)/usr/share/doc/$(package)/examples/manpath.config
	install -m644 debian/bug.presubj \
		$(dtmp)/usr/share/bug/man-db/presubj
	dh_installchangelogs docs/ChangeLog
	dh_installcron
ifeq ($(PO2DEBCONF),yes)
	po2debconf -e utf8 debian/templates.master > debian/templates
endif
	dh_installdebconf
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	chown man:root $(dtmp)/var/cache/man
	chmod 2755     $(dtmp)/var/cache/man
	dh_shlibdeps
	dh_gencontrol -- -V'debconf-depends=debconf (>= $(MINDEBCONFVER)) | debconf-2.0'
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

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