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

# 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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

PREFIX = /opt/lscsoft/lal
FRAMEPREFIX = /opt/lscsoft/libframe
METAIOPREFIX = /opt/lscsoft/libmetaio

CFLAGS = -Wall -g

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

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif


config.status : configure
	dh_testdir
#	LDFLAGS="-Wl,-z,defs"
	PKG_CONFIG_PATH=$(FRAMEPREFIX)/lib/pkgconfig:$(METAIOPREFIX)/lib/pkgconfig \
	CFLAGS="$(CFLAGS)" \
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=$(PREFIX) \
		--mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info \
		--enable-nightly=no \
		--disable-debug \
		--disable-gcc-flags


build : build-stamp

build-stamp :  config.status
	dh_testdir

	$(MAKE) V=1
	-yes "" | $(MAKE) dvi
#	-yes "" | $(MAKE) check
#	-yes "" | $(MAKE) distcheck

	touch $@


clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	-$(MAKE) distclean
	dh_clean 


install : build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install

	# split off -dev
#	mkdir -p $(CURDIR)/debian/lal-dev/$(PREFIX)/bin $(CURDIR)/debian/lal-dev/$(PREFIX)/lib $(CURDIR)/debian/lal-dev/$(PREFIX)/include
##	mv $(CURDIR)/debian/lal/$(PREFIX)/bin/lal-config $(CURDIR)/debian/lal-dev/$(PREFIX)/bin/
##	mv $(CURDIR)/debian/lal/$(PREFIX)/lib/*a $(CURDIR)/debian/lal-dev/$(PREFIX)/lib/
##	mv $(CURDIR)/debian/lal/$(PREFIX)/lib/pkgconfig $(CURDIR)/debian/lal-dev/$(PREFIX)/lib/
##	mv $(CURDIR)/debian/lal/$(PREFIX)/include/* $(CURDIR)/debian/lal-dev/$(PREFIX)/include/
##	mv $(CURDIR)/debian/lal/$(PREFIX)/share/lal $(CURDIR)/debian/lal-dev/$(PREFIX)/share/
##	mv $(CURDIR)/debian/lal/$(PREFIX)/share/man* $(CURDIR)/debian/lal-dev/$(PREFIX)/share/

	dh_install --sourcedir=debian/tmp


binary-common :
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
	dh_python
#	dh_installinit
#	dh_installcron
#	dh_installinfo
#	dh_installman
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
#	dh_perl
	dh_makeshlibs
	dh_installdeb
	LD_LIBRARY_PATH=$(PREFIX)/lib dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary :  install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

.PHONY: build clean binary install
