#! /usr/bin/make -f

# The name of the package
package = man-db-n900
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)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CONFARGS = --build=$(DEB_HOST_GNU_TYPE)
else
CONFARGS = --build=$(DEB_HOST_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

configure-stamp:
	./configure --prefix=/usr --libexecdir=\$${libdir} \
	            --enable-setuid --with-db=gdbm \
	            --enable-undoc='man 7 undocumented' \
	            --with-device=latin1 --enable-mb-groff \
	            --with-config-file=/etc/manpath.config \
	            --with-browser=www-browser --with-pager=pager \
	            --with-col=col --with-vgrind=vgrind --with-grap=grap \
	            --with-compress=compress --with-bzip2=bzip2 \
		    --with-sections='1 n l 8 3 2 3posix 3pm 3perl 5 4 9 6 7' \
	            $(CONFARGS) am_cv_func_iconv=yes am_cv_func_iconv_works=yes
	touch $@

build: build-stamp
build-stamp: configure-stamp
# Builds the binary package.
	dh_testdir
	$(MAKE) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
	touch $@

clean:
	dh_testdir
	[ ! -f Makefile ] || $(MAKE) -i distclean
	-rm -f configure-stamp build-stamp install-stamp man/index.*
	dh_clean

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/man-db
	rm $(CURDIR)/debian/$(package)/etc/manpath.config
	cp -a $(CURDIR)/debian/manpath.config $(CURDIR)/debian/$(package)/etc/manpath.config
	touch $@

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/man-db/examples/manpath.config
	install -m644 debian/bug.presubj \
		$(dtmp)/usr/share/bug/man-db/presubj
	dh_installchangelogs docs/ChangeLog
	install -D -m644 debian/man-db.lintian \
		$(dtmp)/usr/share/lintian/overrides/man-db
	rm -rf $(CURDIR)/debian/$(package)/opt/man
	rmdir $(CURDIR)/debian/$(package)/opt
	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
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

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