#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# This file is public domain software, originally written by Joey Hess.
#
# This version is for a multibinary package. It also allows you to build any
# of the binary packages independantly, via binary-<package> targets.

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

# This has to be exported to make some magic below work.
export DH_OPTIONS


export DEB_HOST_GNU_TYPE  := $(subst i386,i486, $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE))
export DEB_BUILD_GNU_TYPE := $(subst i386,i486, $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE))

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

ifeq (amd64,$(DEB_HOST_ARCH))
  confflags += --disable-fat
else
  confflags += --enable-fat
endif


export CFLAGS = -g
export LDFLAGS = -Wl,-z,defs

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
    ifeq (m68k,$(DEB_HOST_ARCH))
	CFLAGS += -O0
    else
	CFLAGS += -O2
    endif
endif

build: build-stamp
build-stamp:
	dh_testdir

	chmod +x configure
	./configure $(confflags) --prefix=/usr \
		--enable-cxx --enable-mpbsd
	$(MAKE)
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	$(MAKE) check
endif

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	-$(MAKE) clean
	-$(MAKE) distclean

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

# Hack around libtool madness.  Thanks a heap to Kevin Ryde for this.
	sed "/^relink_command/s:libgmp.la:-L`pwd`/debian/tmp/usr/lib libgmp.la:" <libgmpxx.la >libgmpxx.la.tmp
	mv libgmpxx.la.tmp libgmpxx.la

	$(MAKE) DESTDIR=`pwd`/debian/tmp install

	# Library packages
	dh_install -plibgmp3c2 --autodest debian/tmp/usr/lib/libgmp.so.*
	dh_install -plibgmp3c2 --autodest debian/tmp/usr/lib/libmp.so.*
	dh_install -plibgmpxx4 --autodest debian/tmp/usr/lib/libgmpxx.so.*

	# Developer packages
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libgmp.so
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libgmp.a
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libgmp.la
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/include/gmp.h

	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libmp.so
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libmp.a
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libmp.la
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/include/mp.h

#	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libmpfr.a
#	dh_install -plibgmp3-dev --autodest debian/tmp/usr/include/mpfr.h
#	dh_install -plibgmp3-dev --autodest debian/tmp/usr/include/mpf2mpfr.h

	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libgmpxx.so
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libgmpxx.a
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/lib/libgmpxx.la
	dh_install -plibgmp3-dev --autodest debian/tmp/usr/include/gmpxx.h
#	dh_install -plibgmp3-dev --autodest debian/tmp/usr/include/mpfrxx.h

	# Documentation package
	#dh_installinfo -plibgmp3-doc debian/tmp/usr/info/*
	dh_installexamples -plibgmp3-doc --exclude=Makefile.in demos
	find debian/libgmp3-doc -name Makefile | xargs rm -f

	# Miscellaneous install.
	dh_install

	dh_makeshlibs -v -plibgmp3c2 -V 'libgmp3c2'
	dh_makeshlibs -v -plibgmpxx4 -V 'libgmpxx4'

# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything you want to only affect one package
# should be put in another target, such as the install target.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs NEWS README debian/FAQ
	dh_installexamples
#	dh_installman
#	dh_installinfo
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -L libgmp3c2 -l debian/libgmp3c2/usr/lib:debian/libgmpxx4/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
	 $(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Any other binary targets build just one binary package at a time.
binary-%: build install
	make -f debian/rules binary-common DH_OPTIONS=-p$*

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