#!/usr/bin/make -f
#                                                       -*- makefile -*-
# debian/rules file for the Debian/GNU Linux gsl package
# Copyright (C) 1999 - 2007 by Dirk Eddelbuettel <edd@debian.org>

source  := $(shell head -1 debian/changelog | awk '{print $$1}')
sonum   := 0
libpack := libgsl$(sonum)ldbl
#devpack := $(libpack)-dev
devpack := libgsl$(sonum)-dev
dbgpack := libgsl$(sonum)-dbg
binpack := $(source)-bin
docpack := $(source)-doc-pdf
#debtmp	:= $(CURDIR)/debian/$(libpack)
debtmp	:= $(CURDIR)/debian/tmp
debdoc	:= $(CURDIR)/debian/$(docpack)
arch 	:= $(shell dpkg-architecture -qDEB_HOST_ARCH)


#export DH_VERBOSE=1

## needed for texi2pdf / tetex as document has larger-than-normal indices
export pool_size=250000

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# FOR AUTOCONF 2.52 AND NEWER ONLY
CONFFLAGS =
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

DEBUGFLAGS=-g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	DEBUGFLAGS += -O0
else
	DEBUGFLAGS += -O3
ifeq ($(arch),m68k)
	DEBUGFLAGS += -O0
endif
endif
# edd 12 Sep 2007  alpha fails with -O3
ifeq ($(arch),alpha)
	DEBUGFLAGS += -O2
endif

# edd 18 Nov 2006:  add -fexceptions, but don't use -O3 as 
#                   'make check' as gcc hangs in cblas/ after test_ger.c
CFLAGS	   :=-Wall -pipe -fexceptions -D_REENTRANT $(DEBUGFLAGS)
CFLAGSnog  :=-Wall -pipe -fexceptions -D_REENTRANT -O2 
# edd 29 Sep 2005   alpha needs -mieee with gcc 4.0
ifeq ($(arch),alpha)
CFLAGS	+= -mieee
endif

upstream: get-orig-source
get-orig-source: 
        #lynx ftp://sources.redhat.com/pub/gsl
        #lynx http://mirrors.rcn.net/pub/sourceware/gsl
	lynx http://www.network-theory.co.uk/download/gsl

configure: configure-stamp
configure-stamp:
	dh_testdir
	dh_testdir

	ln -sf /usr/share/misc/config.sub .
	ln -sf /usr/share/misc/config.guess .
	rm -f config.cache

	./configure 	CFLAGS="$(CFLAGS)" 		\
			--prefix=/usr 			\
			--enable-shared 		\
			--enable-static 		\
			--with-gnu-ld 			\
			$(CONFFLAGS) 			

        # patch libtool re rpath (see lintian docu)
	sed < libtool > libtool-2 \
			-e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
			-e '/^archive_cmds="/s/"$$/ \\$$deplibs"/'
	mv libtool-2 libtool
	chmod 755 libtool            
        # end libtool rpath patch
	touch configure-stamp

build: configure build-stamp
build-stamp:
	dh_testdir
	$(MAKE)	
	touch build-stamp

build-doc: build-doc-stamp
build-doc-stamp: configure-stamp
	dh_testdir
        # make ps files -- skip as we ship the upstream one in gsl-ref-psdoc
        #cd doc && $(MAKE) ps
        # make pdf
        ## edd 10 Apr 2005  added patch by Guiseppe Bonacci for #303757 
	cd doc && \
	   cp -p randist.texi randist.texi.orig && \
	   sed 's/^.*\\input \(rand.*\)\.tex.*$$/@end tex\n@center @image{\1}\n@tex/' \
	     randist.texi.orig > randist.texi && \
	   for i in rand-*.tex random-walk.tex ; do \
	     echo Converting $$i to eps; \
	     j=`basename $$i .tex`; \
	     tex "\batchmode\nopagenumbers\input $$j\bye"; \
	     dvips $$j; \
	     ps2eps -f $$j.ps; \
	   done && \
	   for i in *.eps; do \
	     echo Converting $$i to pdf... && epstopdf $$i ; \
	   done && $(MAKE) ps && ps2pdf gsl-ref.ps
        # edd 23 Mar 2006  'make pdf' is now inactive upstream
	touch build-doc-stamp

test:	build test-stamp
test-stamp: 
	-$(MAKE) check CFLAGS="$(CFLAGSnog)" | grep -C5 -v '^PASS:'
	touch test-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp test-stamp build-doc-stamp \
		configure-stamp install-doc-stamp
	rm -rf debian/static/
	-rm -f doc/*.pdf doc/*.dvi doc/*.log doc/*.ps
	dh_clean lib/*so* build/*.so*
	[ ! -f Makefile ] || $(MAKE) distclean	
	rm -vf config.sub config.guess

install: test install-stamp 
install-stamp: 
	$(MAKE) -f debian/rules DH_OPTIONS= install-work
install-work:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) prefix=$(debtmp)/usr	 		\
		libdir=$(debtmp)/usr/lib		\
		infodir=$(debtmp)/usr/share/info	\
		includedir=$(debtmp)/usr/include	\
		mandir=$(debtmp)/usr/share/man		install
        # deal with automake / install-info issue #215538
	rm -vf	$(debtmp)/usr/share/info/dir 		\
		$(debtmp)/usr/share/info/dir.old 	
        # deal with libtool leftover .la files (#245556)
	rm -vf $(debtmp)/usr/lib/libgsl*.la
        #
	dh_installdirs		-p$(binpack)	usr/share/man/man1
        #dh_installman		-p$(binpack)	debian/gsl-histogram.1 \
        #					debian/gsl-randist.1
        # move static libs into dev-package  and binaries into bin package
	dh_install --sourcedir=debian/tmp -p$(binpack) -p$(devpack) -p$(libpack)

        # lintian silencer for the 'ldbl' transition package
	dh_installdirs -p$(libpack) usr/share/lintian/overrides
	install -m0644 debian/libgsl0ldbl.overrides \
		$(CURDIR)/debian/$(libpack)/usr/share/lintian/overrides/libgsl0ldbl

	touch install-stamp


install-doc: install-doc-stamp
install-doc-stamp: build-doc
	dh_testdir
	dh_testroot
	mkdir -p -m 0755 $(debdoc)/usr/share/doc/$(docpack)
	install -m 0644 doc/gsl-ref.pdf  $(debdoc)/usr/share/doc/$(docpack)
	touch install-doc-stamp

# 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: build test install 
        #dh_testversion 2.0
	dh_testdir
	dh_testroot
        #dh_installdebconf
	dh_link
	dh_installdocs		-p$(libpack)	AUTHORS NEWS README TODO \
						BUGS THANKS SUPPORT
        #dh_installexamples
        #dh_installmenu
        #dh_installemacsen
        #dh_installpam
        #dh_installinit
        #dh_installcron
	dh_installinfo		-p$(devpack)
        #dh_undocumented	-p$(binpack)	gsl-histogram.1 gsl-randist.1
	dh_installchangelogs			ChangeLog
	dh_compress
	dh_fixperms
#	dh_strip		-N$(devpack)
	dh_strip	--sourcedir=debian/tmp	--dbg-package=libgsl0-dbg
        #dh_suidregister
	dh_makeshlibs
	dh_installdeb
        #dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-doc install-doc
	$(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-arch binary-indep 
.PHONY: build build-doc clean binary-indep binary-arch binary install install-doc 
