#!/usr/bin/make -f
# 							-*- makefile -*-
# debian/rules file for the Debian/GNU Linux dieharder package
# Copyright 2007 - 2019  Dirk Eddelbuettel <edd@debian.org>

# 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  ?= $(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 18 Nov 2006:  add -fexceptions, but don't use -O3 as
#                   'make check' as gcc hangs in cblas/ after test_ger.c
# edd 27 Dec 2015:  add dpkg-buildflags as well
CFLAGS	   :=-Wall -pipe -fexceptions -D_REENTRANT $(DEBUGFLAGS) $(shell dpkg-buildflags --get CFLAGS)
# edd 29 Sep 2005   alpha needs -mieee with gcc 4.0
ifeq ($(arch),alpha)
CFLAGS	+= -mieee
endif

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
        #./autogen.sh
	autoreconf -ifv

	./configure 	CFLAGS="$(CFLAGS)" 		\
			--prefix=/usr 			\
			$(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-arch build-indep
build-arch: configure-stamp build-stamp
build-indep: configure-stamp build-stamp
build-stamp:
	dh_testdir
	$(MAKE)

        # manually call for manual build
#	(cd manual && make && cd -)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

        # Robert uses a non-standard way of building things; make clean does call
        # configure and fails in the chroot/build unless we run autogen.sh first. Sigh
        # so we fixed this by deleting Makefile from the upstream tarball. Sigh.
	#[ ! -f Makefile ] || $(MAKE) clean distclean

	rm -rf buildroot
	rm -f dieharder.pdf
	rm -f config.guess config.sub config.status

        # extra cleanup in manual/
	rm -vf manual/dieharder.dvi manual/dieharder.pdf

	dh_clean -d

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

#	# Add here commands to install the package into debian/tmp.
#	#$(MAKE) prefix=`pwd`/debian/tmp/usr install
	$(MAKE) prefix=`pwd`/debian/dieharder/usr install

#	(cd debian/dieharder/usr/lib && \
#		ls -l )
#		rm -vf libdieharder.so )
#		ln -s libdieharder.so.2.*.* libdieharder.so.2 && \
#		ln -s libdieharder.so.2 libdieharder.so )

	dh_movefiles --sourcedir=debian/dieharder \
				-plibdieharder3t64 -plibdieharder-dev

#	install -m 0644 manual/dieharder.pdf \
#		`pwd`/debian/libdieharder-dev/usr/share/doc/libdieharder-dev/

        # cleanups to make lintian happy
	rmdir -v `pwd`/debian/dieharder/usr/share/man/man3 	\
		 `pwd`/debian/dieharder/usr/include/dieharder	\
		 `pwd`/debian/dieharder/usr/include/

        # edd 03 Jun 2011
        # empty dependency_lib in .la files (cf #621199)
	find debian/dieharder/ -name \*.la | \
		xargs perl -p -i -e "s/dependency_libs='.*'/dependency_libs=''/"


# 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
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_lintian
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installcatalogs
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installman
#	dh_installcron
#	dh_installinfo
#	dh_undocumented
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
# (Uncomment this next line if you have such packages.)
#	 $(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
