#!/usr/bin/make -f

export DH_VERBOSE=1

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

# build tool abstraction
install_	:= install -o root -g root -p
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		:= $(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

ifneq (,$(findstring hppa,$(DEB_BUILD_ARCH)))
  CFLAGS += -ffunction-sections
  CXXFLAGS += -ffunction-sections
endif
CXXFLAGS += -fno-lifetime-dse  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69534

# 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.4devel1-11

clean:
	dh_testdir
	[ ! -f Makefile ] || $(MAKE) distclean || true
	find . -type l | xargs rm -f
	rm -f Makefile config.log config.status libtool \
	    po/Makefile po/Makefile.in po/POTFILES stamp-h1 \
	    contrib/Makefile contrib/Makefile.in unicode/Makefile unicode/Makefile.in
	# The build process doesn't handle this properly, force a rebuild
	-rm -f style/InterpreterMessages.cxx style/InterpreterMessages.h \
	    style/DssslAppMessages.h jade/JadeMessages.h \
	    jade/RtfMessages.h jade/HtmlMessages.h jade/TeXMessages.h \
	    jade/MifMessages.h
	dh_autoreconf_clean
	dh_clean configure-stamp build-stamp install-stamp debian/copyright

configure: configure-stamp
configure-stamp:
	dh_autoreconf
	dh_auto_configure -- --enable-http --enable-shared --enable-static
	touch $@


build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir jade
	$(MAKE) SUBDIRS="grove spgrove style"
	$(MAKE)
	touch $@


# Upstream copyright notice only appears in README; for Debian, it must be in
# copyright file.  COPYING is the license, which appears to be BSD derived
# (similar wording, with "Regents" changed to "James Clark"). Create
# debian/copyright with all the right information.
debian/copyright: README debian/license.Debian debian/copyright.Debian
	cat $^ > $@

# Install into DESTDIR, then move everything later. CURDIR is set by make.
DESTDIR = $(CURDIR)/debian/tmp
export DESTDIR
install: build install-stamp
install-stamp:
	dh_testdir
	dh_testroot
	dh_prep
	$(MAKE) install $(inst_flags)
	touch $@

clean-install:
	rm -fr debian/tmp
	rm install-stamp

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

binary-arch: install debian/copyright debian/README.Debian 
	dh_testdir
	dh_testroot
	rm -f debian/*.debhelper
	dh_prep -X tmp

	dh_installdirs
	dh_installdocs
	dh_installchangelogs -A ChangeLog

	mkdir -p $(DESTDIR)/usr/share/man/man1 && \
	cp -p debian/openjade.1 \
		$(DESTDIR)/usr/share/man/man1/openjade-1.4devel.1
	dh_installman -p openjade

	cp -p $(DESTDIR)/usr/bin/openjade $(DESTDIR)/usr/bin/openjade-1.4devel
	dh_install

	dh_installcatalogs

	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs
	dh_shlibdeps -L $(pkg-libostyle) -l debian/$(pkg-libostyle)/usr/lib

	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

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

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

