#!/usr/bin/make -f
# rules file for openjade
#
# 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.
# 
# This file was modified from the jade rules file.

pkg			:= openjade
pkg-openjade		:= $(pkg)
pkg-libostyle		:= libostyle0
pkg-libostyle-dev	:= libostyle-dev

# directory abstraction
prefix-openjade		:= debian/tmp
bindir-openjade		:= $(prefix-openjade)/usr/bin
confdir-openjade	:= $(prefix-openjade)/etc
libdir-openjade		:= $(prefix-openjade)/usr/share/OpenJade
sgmldir-openjade	:= $(prefix-openjade)/usr/lib/sgml
docdir-openjade		:= $(prefix-openjade)/usr/share/doc/$(pkg-openjade)
infodir-openjade	:= $(prefix-openjade)/usr/share/info
sharedir-openjade	:= $(prefix-openjade)/usr/share
mandir-openjade		:= $(prefix-openjade)/usr/share/man/man1

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

prefix-libostyle-dev	:= debian/$(pkg-libostyle-dev)
incdir-libostyle-dev	:= $(prefix-libostyle-dev)/usr/include/OpenJade
docdir-libostyle-dev	:= $(prefix-libostyle-dev)/usr/share/doc/$(pkg-libostyle-dev)
libdir-libostyle-dev	:= $(prefix-libostyle-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
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  install_program += --strip
endif
make_directory	:= install -d -o root -g root -m 755
compress	:= gzip -9f

CFLAGS		:= -O3 --pipe
CXXFLAGS	:= -O3 --pipe
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -g
  CXXFLAGS += -g
endif
export CFLAGS
export CXXFLAGS

# flags to get automake to use the above
am_inst_flags	:= INSTALL='install -o root -g root -p' \
		   INSTALL_PROGRAM='$(install_program)' \
		   INSTALL_SCRIPT='$(install_script)' \
		   INSTALL_DATA='$(install_file)'

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

# extra docs we don't want
DOCS_TOO_MUCH	:= ABOUT-NLS COPYING copying.txt ChangeLog

# determine our version numbers
SRC_VERSION	:= $(shell LC_ALL=C dpkg-parsechangelog | grep ^Version: | sed 's/^Version: *//')
UPSTREAM_VERSION:= $(shell echo $(SRC_VERSION) | sed -e 's/^\(.*\)-[^-]*$$/\1/')

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

# stuff for snapshot/cvs building
root_cmd		:= sudo
deb_build		:= dpkg-buildpackage -ppgp -r$(root_cmd) -b
cvs_build_dir		:= $(HOME)/debian/$(pkg)
cvs_ver_build_dir	:= $(cvs_build_dir)/$(pkg)-$(UPSTREAM_VERSION)
cvs_pure_src_dir	:= ..
cvs_pure_src		:= $(pkg)


ChangeLog:
	[ ! -e OpenJade-$(UPSTREAM_VERSION) ]
	[ -f $(cvs_pure_src_dir)/$(pkg)_$(UPSTREAM_VERSION).orig.tar.gz ]
	tar -xzvf $(cvs_pure_src_dir)/$(pkg)_$(UPSTREAM_VERSION).orig.tar.gz \
		 OpenJade-1.4devel/ChangeLog
	mv OpenJade-1.4devel/ChangeLog .
	rm -r OpenJade-1.4devel

config.status:	ChangeLog
	aclocal
	autoheader
	libtoolize --force
	automake --add-missing
	autoreconf
	./configure --prefix=/usr --sysconfdir=/etc			\
	   --mandir='$${prefix}/share/man'				\
	   --enable-http --enable-shared				\
	   --enable-static


build:	config.status
	$(checkdir)
	$(MAKE) SUBDIRS="grove spgrove style" CXXFLAGS="$(CXXFLAGS) -D_REENTRANT"
	$(MAKE)
	touch build

clean:
	$(checkdir)
	[ ! -f Makefile ] || $(MAKE) maintainer-clean || true
	find . -type l | xargs rm -f
	dh_clean build binary-arch debian/substvars debian/copyright	\
	    debian/$(pkg-libostyle).shlibs

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

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

debian/substvars:
#	 indicate our providing version of shlibs; this must be
#	 sync'd with debian/control
	: > debian/substvars
	echo "openjade:Version=$(pkg-openjade) (= $(SRC_VERSION))" \
	    >> debian/substvars
	echo "libostyle:Version=$(pkg-libostyle) (= $(SRC_VERSION))" \
	    >> debian/substvars
	echo "libostyle:ShlibVersion=$(pkg-libostyle) (>= $(SHLIBS_PKGVER))" \
	    >> debian/substvars

debian/$(pkg-libostyle).shlibs:
#	 std shlibs file, with the first version that supplied the version
#	  that applications should build with
	: > $@
	echo "libostyle      0 $(pkg-libostyle) (>= $(SHLIBS_PKGVER))" >> $@
	echo "libogrove      0 $(pkg-libostyle) (>= $(SHLIBS_PKGVER))" >> $@
	echo "libospgrove    0 $(pkg-libostyle) (>= $(SHLIBS_PKGVER))" >> $@


binary-arch:	build debian/copyright debian/substvars 		\
		debian/$(pkg-libostyle).shlibs debian/README.Debian
	$(checkdir)
	$(checkroot)

##
##	 openjade
##
	rm -rf $(prefix-openjade)
	$(make_directory) $(bindir-openjade) $(sgmldir-openjade)	\
	    $(mandir-openjade)

#	 we can't use SUBDIRS here since that trickles down; 
#	 install it all into openjade dir
	$(MAKE) install							\
	    prefix=$(SRCDIR)/$(prefix-openjade)/usr			\
	    pkgdocdir=$(SRCDIR)/$(docdir-openjade)			\
	    $(am_inst_flags)

#	 remove the extra docs installed from top-level Makefile
	cd $(docdir-openjade) && rm -f $(DOCS_TOO_MUCH)

#	 install unicode stuff -- this should probably be handled in
#	 upstream Makefiles
	$(make_directory) $(libdir-openjade)/unicode
	$(install_file) unicode/catalog unicode/demo.sgm unicode/unicode.syn \
	    $(libdir-openjade)/unicode
	$(install_file) unicode/unicode.sd $(libdir-openjade)/unicode/
	$(install_script) unicode/gensyntax.pl $(libdir-openjade)/unicode

#	 install man pages
	dh_undocumented -p$(pkg) openjade.1


##
##	 libostyle
##
	rm -rf $(prefix-libostyle)
	$(make_directory) $(libdir-libostyle)

	mv $(prefix-openjade)/usr/lib/lib*.so.*.* $(libdir-libostyle)/
	mv $(prefix-openjade)/usr/lib/lib*.so.* $(libdir-libostyle)/

#	 make sure maintainer scripts are good
	sh -n debian/$(pkg-libostyle).postinst


##
##	 libostyle-dev
##
	rm -rf $(prefix-libostyle-dev)
	$(make_directory) $(incdir-libostyle-dev) $(libdir-libostyle-dev)\
	    $(docdir-libostyle-dev)

	mv $(prefix-openjade)/usr/lib/lib*.* $(libdir-libostyle-dev)/


##
##	 common to all
##

#	 install Debian-std docs
	dh_installdocs -a
#	 optionally, install upstream ChangeLog
	[ ! -f ChangeLog ] || dh_installchangelogs -a ChangeLog
	[ -f ChangeLog ] || dh_installchangelogs -a

	dh_installdeb -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a

	dh_shlibdeps -l $(pkg-libostyle) -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

	touch binary-arch

# cvs snapshot building
cvs-build:	clean
	[ -d $(cvs_pure_src_dir)/$(cvs_pure_src)/CVS ] || exit 1
	$(root_cmd) rm -rf $(cvs_ver_build_dir)
#	rm -f $(cvs_build_dir)/$(pkg)_$(UPSTREAM_VERSION).orig.tar.gz
#	 don't do cvs up or make the changelog
	install -d -m 755 $(cvs_ver_build_dir)
#	GZIP=-9 tar czf $(cvs_build_dir)/$(pkg)_$(UPSTREAM_VERSION).orig.tar.gz \
#		-C $(cvs_pure_src_dir) --exclude CVS $(cvs_pure_src)
	tar cf - --exclude CVS . | tar xf - -C $(cvs_ver_build_dir)
	[ -f ChangeLog ] || cvs2cl -r -t -b
	cp ChangeLog $(cvs_ver_build_dir)/
	cd $(cvs_ver_build_dir) && $(deb_build)

define checkdir
	test -f debian/rules && test -d jade
endef

# Below here is fairly generic really

define checkroot
	test `id -u` = 0
endef

binary:		binary-indep binary-arch

.PHONY: binary binary-indep clean

#Local variables:
#mode: makefile
#End:
