#!/usr/bin/make -f
# -*- makefile -*-

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

DEB_HOST_ARCH		?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS	?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifneq (,$(filter $(DEB_HOST_ARCH), ppc64))
  build32 = yes
endif
ifeq ($(DEB_HOST_ARCH),ppc64el)
  cpu=--with-cpu=power8
endif
# --with-cpu=z10 fails to build from source
# https://github.com/libdfp/libdfp/issues/43
ifeq ($(DEB_HOST_ARCH),s390x)
  cpu=
endif

configure: configure-stamp $(if $(filter yes, $(build32)),configure32-stamp)
configure-stamp:
	rm -rf build
	mkdir -p build
	cd build && ../configure --prefix=/usr --enable-decimal-float=yes $(cpu)
	touch $@
configure32-stamp:
	rm -rf build32
	mkdir -p build32
	cd build32 && CC="gcc -m32" ../configure --prefix=/usr --enable-decimal-float=yes
	touch $@

build: build-stamp $(if $(filter yes, $(build32)),build32-stamp)
build-stamp: configure-stamp
	$(MAKE) -C build
	-$(MAKE) -k -C build check
	touch $@
build32-stamp: configure32-stamp
	$(MAKE) -C build32
	-$(MAKE) -k -C build32 check
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	rm -rf build build32
	dh_clean

install: install-stamp
install-stamp:
	dh_testdir
	dh_testroot
	dh_clean -k
	rm -rf $(d)
	dh_installdirs -plibdfp-dev \
		usr/lib
	$(MAKE) -C build install install_root=$(CURDIR)/debian/libdfp1
	mv debian/libdfp1/usr/lib/libdfp.so debian/libdfp1/usr/lib/libdfp.a \
		debian/libdfp-dev/usr/lib/
	mv debian/libdfp1/usr/include debian/libdfp-dev/usr/

ifneq ($(build32),)
	dh_installdirs -plib32dfp-dev \
		usr/lib32
	$(MAKE) -C build32 install install_root=$(CURDIR)/debian/lib32dfp1
	mv debian/lib32dfp1/usr/lib debian/lib32dfp1/usr/lib32
	rm -rf debian/lib32dfp1/usr/include
	mv debian/lib32dfp1/usr/lib32/libdfp.so debian/lib32dfp1/usr/lib32/libdfp.a \
		debian/lib32dfp-dev/usr/lib32/
endif

binary-indep: build install
# nothing to do

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -plibdfp1 \
		README.user
ifneq ($(build32),)
	dh_installdocs -plib32dfp1
endif
	mkdir -p debian/libdfp-dev/usr/share/doc/libdfp1
	cp -p README.developer debian/libdfp-dev/usr/share/doc/libdfp1/
	ln -sf libdfp1 debian/libdfp-dev/usr/share/doc/libdfp-dev
	mkdir -p debian/libdfp1-dbg/usr/share/doc
	ln -sf libdfp1 debian/libdfp1-dbg/usr/share/doc/libdfp1-dbg
ifneq ($(build32),)
	mkdir -p debian/lib32dfp-dev/usr/share/doc/lib32dfp1
	cp -p README.developer debian/lib32dfp-dev/usr/share/doc/lib32dfp1/
	ln -sf lib32dfp1 debian/lib32dfp-dev/usr/share/doc/lib32dfp-dev
	mkdir -p debian/lib32dfp1-dbg/usr/share/doc
	ln -sf lib32dfp1 debian/lib32dfp1-dbg/usr/share/doc/lib32dfp1-dbg
	dh_installchangelogs -plibdfp1 -plib32dfp1
endif
	dh_installexamples -s
	dh_fixperms -s
	dh_strip -plibdfp1 -plibdfp-dev --dbg-package=libdfp1-dbg
ifneq ($(build32),)
	dh_strip -plib32dfp1 -plib32dfp-dev --dbg-package=lib32dfp1-dbg
endif
	dh_compress -s
	dh_makeshlibs -s
	dh_shlibdeps -s

ifneq ($(build32),)
	-ls -l $(d_rld)/usr/share/doc/$(p_rl)
	dh_installdocs -p$(p_rl32) \
		USAGE debian/inputrc.arrows
	dh_installchangelogs -p$(p_rl32) CHANGES
	ln -sf $(p_rl32) $(d_rld32)/usr/share/doc/$(p_rld32)
	dh_compress -p$(p_rl32) -p$(p_rld32)
	dh_fixperms -p$(p_rl32) -p$(p_rld32)
	dh_strip --dbg-package=$(p_rlg) -p$(p_rl32) -p$(p_rld32)
	dh_makeshlibs -p$(p_rl32)
	-dh_shlibdeps -p$(p_rl32) -p$(p_rld32) \
		 -L $(p_rl32) -l $(d_rl32)/lib
	-ls -l $(d_rld)/usr/share/doc/$(p_rl)
endif
	dh_installdeb -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary:	binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean
