#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# The debhelper compatibility version to use is now set in debian/compat.
#export DH_COMPAT=3

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

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

confflags =	--disable-maintainer-mode \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		-v

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# Ensure at build time that the library has no dependencies on undefined
# symbols.
LDFLAGS += -Wl,-z,defs

# Speed up loading.
LDFLAGS += -Wl,-O1

export LDFLAGS

# shared library versions, option 1
#version=2.0.5
#major=2
# option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so
version=`ls src/.libs/lib*.so.* | \
 awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
major=`ls src/.libs/lib*.so.* | \
 awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`

upstreamversion=$(shell dpkg-parsechangelog | grep Version | head -1 | sed -e 's/Version: //g' -e 's/-[A-Za-z0-9\.]*$$//g')


config.status:
	dh_testdir
	autoreconf -if
	env CXX="c++ -Wl,--as-needed -Wl,-O1" ./configure $(confflags)

build: build-stamp
build-stamp:  config.status
	dh_testdir

	cd doc && latex2man -M -t pstoedit.trans pstoedit.tex pstoedit.1
	cd doc && latex2man -H -t pstoedit.trans pstoedit.tex pstoedit.htm
	$(MAKE)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	[ ! -f Makefile ] || $(MAKE) distclean

	rm -f debian/buildinfo
	rm -rf autom4te.cache
	rm -f aclocal.m4 ltmain.sh configure config.guess config.sub depcomp
	rm -f mkinstalldirs install-sh missing src/pstoedit_config.h.in
	-find -type f -name 'Makefile.in' | xargs rm -f
	-find -type f -name 'Makefile.in' | sed -e 's/\.in$$//' | xargs rm -f
	rm -f doc/pstoedit.1 doc/pstoedit.htm

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) install prefix=$(CURDIR)/debian/tmp/usr

	@for plugin in libp2edrvstd libp2edrvmagick++ ; do \
		if ! test -r $(CURDIR)/debian/tmp/usr/lib/pstoedit/$$plugin.la ; then \
			echo "" ; \
			echo "pstoedit plugins \"$$plugin\" didn't get installed properly" ; \
			echo "Please investigate" ; \
			exit 1; \
		fi ; \
	done



binary-indep: build install
# We have nothing to do by default.

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installcron
	dh_installman
	dh_installinfo
	dh_installchangelogs doc/changelog.htm
	dh_link
	dh_strip
	dh_compress
	dh_buildinfo
	dh_fixperms
	dh_makeshlibs -plibpstoedit0c2a -V 'libpstoedit0c2a (>= $(upstreamversion))'
	dh_installdeb
	dh_shlibdeps -Llibpstoedit0c2a -ldebian/libpstoedit0c2a/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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