#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

export DH_VERBOSE=1

pkg		:= opensp
pkg-osp		:= $(pkg)
libosp-maj-ver	:= $(shell egrep 'SP_LIBOSP_CUR=[0-9]+' configure.in|cut -d= -f2)
ifeq (,$(libosp-maj-ver))
$(error "Could not get value of SP_LIBOSP_CUR")
endif
pkg-libosp	:= libosp$(libosp-maj-ver)
pkg-libosp-dev	:= libosp-dev

# default SGML dir
sgmldir			:= /usr/share/sgml
local-sgmldir		:= /usr/local/share/sgml

# default SGML pathing - see /usr/share/doc/opensp/sysid.htm
default-sgml-path	:= $(local-sgmldir):$(sgmldir)
default-catalogs	:= /etc/sgml/catalog

# build tool abstraction
install_	:= install -p
install_file	:= install -m 644 -p
install_script	:= install -m 755 -p
install_program	:= install -m 755 -p
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  install_program += --strip
endif
make_directory	:= install -d -m 755
compress	:= gzip -9f

CFLAGS		:= $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS	:= $(shell dpkg-buildflags --get CPPFLAGS)
CXXFLAGS	:= $(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS		:= $(shell dpkg-buildflags --get LDFLAGS)
export CFLAGS
export CPPFLAGS
export CXXFLAGS
export LDFLAGS

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

ifeq (,$(findstring stage1,$(DEB_BUILD_PROFILES)))
	enable_doc_build := --enable-full-doc-build
else
	enable_doc_build := --disable-doc-build
endif

# flags to get automake to use the above
inst_flags	:= INSTALL='$(install_)'
inst_flags	+= INSTALL_PROGRAM='$(install_program)'
inst_flags	+= INSTALL_SCRIPT='$(install_script)'
inst_flags	+= INSTALL_DATA='$(install_file)'

# first version of package providing the right shlibs
SHLIBS_PKGVER	:= 1.5.2-1

# Maintainer target; set AM_MAINTAINER_MODE in configure.in, so this needs to
# be run when autotools input files change.
autoinit:
	./autoinit.sh

clean:
	dh_testdir
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_autoreconf_clean
	dh_clean build-stamp install-stamp debian/copyright \
	    debian/$(pkg-libosp).shlibs debian/$(pkg-libosp-dev).substvars \
	    debian/README.Debian debian/$(pkg-libosp).README.Debian

build-stamp:
	dh_autoreconf
	CC=$(CC) CXX=$(CXX) ./configure --prefix=/usr \
	   --enable-http --enable-shared --enable-static \
	   --enable-default-catalog=$(default-catalogs) \
	   --enable-default-search-path=$(default-sgml-path) \
	   --disable-dtddecl $(enable_doc_build)
	dh_testdir
	dh_update_autotools_config
	$(MAKE) $(MAKEFLAGS) SUBDIRS=lib CXXFLAGS="$(CXXFLAGS) -D_REENTRANT"
	$(MAKE) $(MAKEFLAGS)
	touch $@

build: build-stamp

build-arch: build-stamp

build-indep: build-stamp

debian/copyright:	COPYING debian/copyright.Debian install-stamp
#	 to ensure we have a verbatim copy of the upstream copyright, 
#	 cat the Debian-specific stuff to the end of the upstream file
	cat COPYING debian/copyright.Debian > $@

debian/$(pkg-libosp-dev).substvars:  install-stamp
#	 indicate our providing version of shlibs; this must be
#	 sync'd with debian/control
	: > $@
	echo "opensp:Version=$(pkg-osp) (= $(DEB_VERSION))"            >> $@
	echo "libosp:Version=$(pkg-libosp) (= $(DEB_VERSION))"         >> $@

debian/$(pkg-libosp).shlibs:  install-stamp
#	 std shlibs file, with the first version that supplied the version
#	 that applications should build with
	echo "libosp      $(libosp-maj-ver) $(pkg-libosp) (>= $(SHLIBS_PKGVER))" > $@

debian/README.Debian:	debian/README.Debian.in install-stamp
#	 substitute the catalog paths
	sed -e 's|%{default-catalogs}|$(default-catalogs)|' \
	-e 's|%{default-sgml-path}|$(default-sgml-path)|' $^ > $@

debian/$(pkg-libosp).README.Debian:	debian/$(pkg-libosp).README.Debian.in install-stamp
#	 substitute the catalog paths
	sed -e 's|%{default-catalogs}|$(default-catalogs)|' \
	-e 's|%{default-sgml-path}|$(default-sgml-path)|' $^ > $@

install-common := debian/copyright debian/$(pkg-libosp-dev).substvars \
		  debian/$(pkg-libosp).shlibs \
		  debian/README.Debian debian/$(pkg-libosp).README.Debian

# Install into DESTDIR, then move everything later. CURDIR is set by make.
DESTDIR = $(CURDIR)/debian/tmp
export DESTDIR
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	$(MAKE) install $(inst_flags)
	cd $(DESTDIR)/usr/share/OpenSP && \
	( [ -f unicode.decl ] || mv unicode.sd unicode.decl ) && \
	( sed -e 's/unicode.sd/unicode.decl/' catalog > catalog.tmp && \
	mv -f catalog.tmp catalog ) && \
	chmod +x gensyntax.pl
ifeq (,$(findstring stage1,$(DEB_BUILD_PROFILES)))
	# Ugly fix for man page generation error
	sed 's~\.HP.*\[\\fIsysid\\fR\.\.\.\]~[\\fIsysid\\fR...]~' $(DESTDIR)/usr/share/man/man1/onsgmls.1 > $(DESTDIR)/usr/share/man/man1/onsgmls.1.tmp && mv $(DESTDIR)/usr/share/man/man1/onsgmls.1.tmp $(DESTDIR)/usr/share/man/man1/onsgmls.1
	mkdir -p $(DESTDIR)/usr/share/doc/OpenSP
	for ext in html pdf ps; do \
		touch $(DESTDIR)/usr/share/doc/OpenSP/releasenotes.$$ext; \
	done
else
	mkdir -p $(DESTDIR)/usr/share/man/man1
	for f in onsgmls.1 osgmlnorm.1 ospam.1 ospcat.1 ospent.1 osx.1; do \
		touch $(DESTDIR)/usr/share/man/man1/$$f; \
	done
endif
	touch $@

install: install-stamp

binary-indep: install-stamp
# There are no architecture-independent binary packages generated from this
# source package.

binary-arch: install-stamp $(install-common)
	dh_testdir
	dh_testroot

	dh_installdirs
	dh_installdocs
	dh_installchangelogs -A ChangeLog
	dh_installman -p opensp
	dh_install

	dh_strip -a
	dh_compress -a
	dh_fixperms -a

	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps

	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

TAGS:
	etags $$(find . -path ./debian -prune -o -regex '.*\.\(cxx\|c\|h\)' -print|sed -e 's/^.\///')

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

# vim: set syntax=makefile:
# Local variables:
# compile-command: "cd .. && fakeroot dpkg-buildpackage -uc -us"
# End:
