#!/usr/bin/make -f

package=cln

version=$(shell expr `pwd` : '.*-\([0-9.]*\)')
version_major=$(shell expr `pwd` : '.*-\([0-9]*\).[0-9.]*')

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

# ARM: CLN's assembler support is not working properly (it was only
# 'theoretically' ported by copying from code that had once worked in
# CLISP under BSD). Same for armel.
ifneq (,$(findstring arm,$(DEB_HOST_ARCH)))
  CPPFLAGS += -DNO_ASM
endif

# HPPA: Assembler support is not working properly.  Somebody needs to
# investigate but currently I don't have the inspiration for fixing
# things on exotic architectures.
ifneq (,$(findstring hppa,$(DEB_HOST_ARCH)))
  CPPFLAGS += -DNO_ASM
endif

# SPARC: With some versions of GCC, there are apparently problems in
# passing return values in %g1.
ifneq (,$(findstring sparc,$(DEB_HOST_ARCH)))
  CPPFLAGS += -DNO_ASM
endif

debian/autoreconf-stamp:
	dh_autoreconf
	touch $@

build-indep:
# There are no architecture-independent files to be uploaded
# generated by this package. If there were any they would be made here.

build-arch: debian/autoreconf-stamp
	dh_testdir
	./configure --prefix=/usr --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) CPPFLAGS="$(CPPFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)"
	${MAKE}
	(cd doc; ${MAKE} cln.html)
	touch build

build: build-indep build-arch

clean:
	dh_testdir
	dh_autoreconf_clean
	-rm -f build
	[ ! -f Makefile ] || ${MAKE} distclean
	dh_clean

binary-indep: build-indep
	dh_testdir
	dh_testroot

binary-arch: build-arch
	dh_testdir
	dh_testroot
	dh_installdirs
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	# I want to see what the buildd logs of the different architectures say:
	-${MAKE} check
endif
	${MAKE} install prefix=${CURDIR}/debian/tmp/usr bindir=${CURDIR}/debian/pi/usr/bin mandir=${CURDIR}/debian/pi/usr/share/man datadir=${CURDIR}/debian/libcln-dev/usr/share
	${MAKE} install-html prefix=${CURDIR}/debian/libcln-dev/usr htmldir=${CURDIR}/debian/libcln-dev/usr/share/doc/libcln-dev/html
	# This installs into libdir, but we must not set libdir because it affects the .la file:
	mv ${CURDIR}/debian/tmp/usr/lib/pkgconfig/* ${CURDIR}/debian/libcln-dev/usr/lib/pkgconfig/
	dh_installdocs ChangeLog NEWS README TODO
	dh_installexamples -plibcln-dev ${CURDIR}/examples/contfrac.cc ${CURDIR}/examples/e.cc ${CURDIR}/examples/fibonacci.cc ${CURDIR}/examples/legendre.cc ${CURDIR}/examples/lucaslehmer.cc ${CURDIR}/examples/nextprime.cc ${CURDIR}/examples/perfnum.cc
	/usr/bin/install -m 644 debian/libcln-dev.doc-base ${CURDIR}/debian/libcln-dev/usr/share/doc-base/libcln-dev
	dh_installchangelogs
	dh_install --sourcedir=debian/tmp
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean
