#!/usr/bin/make -f
# rules file for jade
#
# The principle of these rules files are simplicity, robustness,
# ease of maintenance, and readability through abstraction
#
# Some bytes in this file may have been touched by Ian Jackson,
# so I can never remove his name from this file.  Oh boy.

#export DH_VERBOSE=1

pkg-sp		:= sp
pkg-jade	:= jade
pkg-libsp1	:= libsp1c2
pkg-libsp1-dev	:= libsp1-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

# directory abstraction
prefix-jade	:= debian/$(pkg-jade)
bindir-jade	:= $(prefix-jade)/usr/bin
sgmldir-jade	:= $(prefix-jade)$(sgmldir)/jade
docdir-jade	:= $(prefix-jade)/usr/share/doc/$(pkg-jade)
infodir-jade	:= $(prefix-jade)/usr/share/info
sharedir-jade	:= $(prefix-jade)/usr/share

prefix-sp	:= debian/$(pkg-sp)
bindir-sp	:= $(prefix-sp)/usr/bin
confdir-sp	:= $(prefix-sp)/etc
sgmldir-sp	:= $(prefix-sp)$(sgmldir)/sp
docdir-sp	:= $(prefix-sp)/usr/share/doc/$(pkg-sp)
infodir-sp	:= $(prefix-sp)/usr/share/info
sharedir-sp	:= $(prefix-sp)/usr/share

prefix-libsp1	:= debian/$(pkg-libsp1)
docdir-libsp1	:= $(prefix-libsp1)/usr/share/doc/$(pkg-libsp1)
libdir-libsp1	:= $(prefix-libsp1)/usr/lib

prefix-libsp1-dev	:= debian/$(pkg-libsp1-dev)
incdir-libsp1-dev	:= $(prefix-libsp1-dev)/usr/include/$(pkg-sp)
docdir-libsp1-dev	:= $(prefix-libsp1-dev)/usr/share/doc/$(pkg-libsp1-dev)
libdir-libsp1-dev	:= $(prefix-libsp1-dev)/usr/lib

# build tool abstraction
install_file	:= install -o root -g root -m 644 -p
install_script	:= install -o root -g root -m 755 -p
install_program	:= install -o root -g root -m 755 -p
make_directory	:= install -d -o root -g root -m 755
compress	:= gzip -9f

# debhelper verbose
#export DH_VERBOSE=1

# first version of package providing the right shlibs
SHLIBS_PKGVER	:= 1.3.4-1.2.1-44

# determine our version numbers
SRC_VERSION	:= $(shell LC_ALL=C dpkg-parsechangelog | sed -n 's/^Version: *//p')
SP_VERSION	:= $(shell cat VERSION)

# determine our top level
SRCDIR		:= $(shell pwd)

CFLAGS		:= -g -pipe
CXXFLAGS	:= -g -pipe -D_REENTRANT
CC		:= gcc
CXX		:= g++


# work around broken C++ compiler 4.0 on hppa
DEB_BUILD_ARCH  := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
# ifeq ($(DEB_BUILD_ARCH),hppa)
#   CC		:= gcc-3.4
#   CXX		:= g++-3.4
# endif
# ifeq ($(DEB_BUILD_ARCH),m68k)
#   CC		:= gcc-3.3
#   CXX		:= g++-3.3
# endif

# policy recommendations
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O0
  CXXFLAGS += -O0
else
  CFLAGS += -O2
  CXXFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  INSTALL_PROGRAM += -s
endif


version-test:
	@echo Debian version is $(SRC_VERSION)
	@echo upstream version is $(UPSTREAM_VERSION)

config.status:
	dh_buildinfo generate cat
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" CC="$(CXX)" CXX="$(CXX)" \
	   ./configure --prefix=/usr --enable-html			\
	   --enable-http --enable-static --enable-shared --enable-mif	\
	   --enable-default-catalog=$(default-catalogs)			\
	   --enable-default-search-path=$(default-sgml-path)

build:	config.status
	dh_testdir
	$(MAKE)
	touch build

clean:
	dh_testdir
	rm -f debian/buildinfo
#	 update config.sub and config.guess, from autotools-dev
	test -r /usr/share/misc/config.sub && \
	  cp -f /usr/share/misc/config.sub config/
	test -r /usr/share/misc/config.guess && \
	  cp -f /usr/share/misc/config.guess config/
	rm -f build binary-arch
	$(MAKE) clean
	test ! -f Makefile.dist || $(MAKE) mrproper
	rm -f `find . -name "*~"`
	rm -rf $(prefix-sp) $(prefix-libsp1) $(prefix-libsp1-dev)
	rm -rf $(prefix-jade)
	rm -f debian/copyright debian/files* core
#        remove some autoconf spam
	rm -f conftest*
#	 force the M4 files to rebuild, timestamps can't be trusted
	find . -name '*.m4' | xargs chmod +w
	find . -name '*.m4' | xargs touch
#	 these are created in the binary-arch rule
	rm -f debian/README.Debian debian/substvars debian/shlibs* \
	      debian/sp.conffiles debian/*.substvars
# 	 some stuff that should be cleaned in upstream Makefile
	find . -name 'Makefile.dep' | xargs rm -f
	rm -f libtool
	dh_clean

binary-indep:	build
# There are no architecture-independent files to be uploaded
# generated by this package.

binary-arch:	build
	dh_testdir
	dh_testroot
	dh_clean -k

#	 lets create our debian-std copyright file; I like to ensure
#	 I have a verbatim copy of the upstream copyright by cat'ing
#	 the Debian-specific stuff to the end
	cat COPYING debian/copyright.Debian > debian/copyright

#	 substvars used to indicate our version of sp and libsp1
	: > debian/substvars
	echo "$(pkg-sp):Version=$(pkg-sp) (= $(SP_VERSION)-$(SRC_VERSION))" \
	    >> debian/substvars
	echo "$(pkg-libsp1):Version=$(pkg-libsp1) (= $(SP_VERSION)-$(SRC_VERSION))" \
	    >> debian/substvars
	echo "$(pkg-libsp1):ShlibVersion=$(pkg-libsp1) (>= $(SHLIBS_PKGVER))" \
	    >> debian/substvars
	: > debian/${pkg-libsp1-dev}.substvars
	echo "$(pkg-libsp1):Version=$(pkg-libsp1) (= $(SP_VERSION)-$(SRC_VERSION))" \
	    >> debian/${pkg-libsp1-dev}.substvars

#	 substitution in README.Debian
	sed -e 's|%{default-catalogs}|$(default-catalogs)|' \
	-e 's|%{default-sgml-path}|$(default-sgml-path)|' \
	-e 's|%{sgmldir}|$(sgmldir)|' \
	debian/README.Debian.in > debian/README.Debian

##
##	 install sp suite
	$(make_directory) $(bindir-sp) $(sgmldir-sp) $(docdir-sp) $(confdir-sp)
	$(MAKE) install PROGDIRS="nsgmls spam sgmlnorm spent sx" LIBDIRS="" \
	    prefix=$(SRCDIR)/$(prefix-sp)/usr INSTALL="$(install_script)"
	mv $(bindir-sp)/sx $(bindir-sp)/sgml2xml

# The place for sp pubtext, clearly indicated in the LSB (stripped of
# package versioning), is /usr/share/sgml/sp/

#	 install declarations
	$(make_directory) $(sgmldir-sp)
	$(install_file) japan.sgmldecl $(sgmldir-sp)/japan.decl
	$(install_file) debian/sp_implied.decl $(sgmldir-sp)
#	 install xml.soc, this is the expected location
	$(make_directory) $(prefix-sp)$(sgmldir)/declaration
	$(install_file) pubtext/xml.soc $(prefix-sp)$(sgmldir)/declaration
#	install unicode stuff
	$(make_directory) $(sgmldir-sp)/unicode
	$(install_file) unicode/catalog unicode/demo.sgm unicode/unicode.syn \
	    $(sgmldir-sp)/unicode/
	$(install_file) unicode/unicode.sd $(sgmldir-sp)/unicode/unicode.decl
#	 putting this script here is a little perverse
	$(install_script) unicode/gensyntax.pl $(sgmldir-sp)/unicode/

#	 install HTML documentation
	dh_installdocs -p$(pkg-sp) README doc/*.htm debian/README.Maemo
#	 now sx is named sgml2xml, change it in the doco
	perl -pi -e 's/sx/sgml2xml/g; s/SX/SGML2XML/g;'			\
	    $(docdir-sp)/sx.htm
	perl -pi -e 's/>sx/>sgml2xml/g; s/>SX/>SGML2XML/g;'		\
	    $(docdir-sp)/index.htm $(docdir-sp)/new.htm
	dh_installchangelogs -p$(pkg-sp)
	dh_installexamples -p$(pkg-sp) dsssl/FOT.tex dsssl/demo*

#	 install man pages
	dh_installman -p$(pkg-sp) debian/nsgmls.1 debian/spam.1 debian/spent.1 \
	    debian/sgml2xml.1 debian/sgmlnorm.1

##
##	 install libsp1
	$(make_directory) $(prefix-libsp1)/usr/lib $(prefix-libsp1)/DEBIAN

#	 std shlibs file, with the first version that supplied the version
#	  that applications should build with
# should work but doesn't
#	dh_makeshlibs -p$(pkg-libsp1) -V'$(pkg-libsp1) (>=$(SHLIBS_PKGVER))'
# do it by hand
	: > $(prefix-libsp1)/DEBIAN/shlibs
	echo "libsp      1 $(pkg-libsp1) (>= $(SHLIBS_PKGVER))" >> $(prefix-libsp1)/DEBIAN/shlibs
	echo "libgrove   1 $(pkg-libsp1) (>= $(SHLIBS_PKGVER))" >> $(prefix-libsp1)/DEBIAN/shlibs
	echo "libspgrove 1 $(pkg-libsp1) (>= $(SHLIBS_PKGVER))" >> $(prefix-libsp1)/DEBIAN/shlibs
	echo "libstyle   1 $(pkg-libsp1) (>= $(SHLIBS_PKGVER))" >> $(prefix-libsp1)/DEBIAN/shlibs
	chmod 0644 $(prefix-libsp1)/DEBIAN/shlibs
	chown 0.0 $(prefix-libsp1)/DEBIAN/shlibs

	$(MAKE) install PROGDIRS="" LIBDIRS="lib grove spgrove style"	\
	    prefix=$(SRCDIR)/$(prefix-libsp1)/usr INSTALL="$(install_file)"

	rm -f $(libdir-libsp1)/*.so $(libdir-libsp1)/*.a $(libdir-libsp1)/*.la

#	 install Debian-std docs
	dh_installdocs -p$(pkg-libsp1) README debian/README.Maemo
	dh_installchangelogs -p$(pkg-libsp1)
	$(install_file) debian/README.sp-dev $(docdir-libsp1)/README.Debian


##
##	 install libsp1-dev
	$(make_directory) $(incdir-libsp1-dev)
	$(make_directory) $(prefix-libsp1-dev)/DEBIAN			\
	    $(prefix-libsp1-dev)/usr/lib

	$(MAKE) install PROGDIRS="" LIBDIRS="lib grove spgrove style"	\
	    prefix=$(SRCDIR)/$(prefix-libsp1-dev)/usr INSTALL="$(install_script)"

	rm -f $(libdir-libsp1-dev)/*.so.* $(libdir-libsp1-dev)/*.la

#	 includes
	$(install_file) include/*.*            $(incdir-libsp1-dev)
	$(install_file) generic/*.h            $(incdir-libsp1-dev)
	$(install_file) grove/Node.h           $(incdir-libsp1-dev)
	$(install_file) spgrove/GroveApp.h     $(incdir-libsp1-dev)
	$(install_file) spgrove/GroveBuilder.h $(incdir-libsp1-dev)
	$(install_file) style/FOTBuilder.h style/GroveManager.h		\
	  style/DssslApp.h style/dsssl_ns.h    $(incdir-libsp1-dev)

#	 install Debian-std docs
	dh_installdocs -p$(pkg-libsp1-dev) README debian/README.Maemo
	dh_installchangelogs -p$(pkg-libsp1-dev)
	$(install_file) debian/README.sp-dev $(docdir-libsp1-dev)/README.Debian


##
##	 install jade
	$(make_directory) $(bindir-jade) $(sgmldir-jade) $(docdir-jade)

	$(MAKE) install PROGDIRS="jade" LIBDIRS=""			\
	    prefix=$(SRCDIR)/$(prefix-jade)/usr INSTALL="$(install_script)"

#	 check these in dsssl/catalog
	$(make_directory) $(sgmldir-jade)
	$(install_file) dsssl/*.dtd dsssl/catalog $(sgmldir-jade)
	dh_installcatalogs -p jade

#	 make the links
	/usr/share/sgml-data/sgml-catalog-check.pl -l -v 0 -d		\
		$(prefix-jade)/usr/share/sgml $(sgmldir-jade)/catalog

#	 install HTML documentation
	dh_installdocs -p$(pkg-jade) jadedoc/*.htm debian/README.Maemo
	cd $(docdir-jade) && \
	sed -e 's/copying\.txt/copyright/' jade.htm > jade.htm.new && \
	mv jade.htm.new jade.htm && ln -s jade.htm index.html
	dh_installchangelogs -p$(pkg-jade)

#	 install man pages
	dh_installman -p$(pkg-jade) debian/jade.1

#	 install examples
	dh_installexamples -p$(pkg-jade) dsssl/demo.* dsssl/FOT.tex

	dh_strip -a
	dh_compress -a
	dh_fixperms -a

	dh_buildinfo install

	dh_installdeb -a

#	 put it all together
	dh_shlibdeps -l $(libdir-libsp1) -a

#	 control files for sp, libsp1, and libsp1-dev have a different
#	 version
	dh_gencontrol -p$(pkg-sp) -- -v$(SP_VERSION)-$(SRC_VERSION)
	dh_gencontrol -p$(pkg-libsp1) -- -v$(SP_VERSION)-$(SRC_VERSION)
	dh_gencontrol -p$(pkg-libsp1-dev) -- -v$(SP_VERSION)-$(SRC_VERSION)
#	 jade is more normal
	dh_gencontrol -p$(pkg-jade)

	dh_md5sums -a
	dh_builddeb -a
	touch binary-arch

binary:		binary-indep binary-arch

.PHONY: binary binary-indep clean

# Local variables:
# mode: makefile
# compile-command: "cd .. && fakeroot dpkg-buildpackage -uc -us"
# End:
