#!/usr/bin/make -f

# 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)
DEB_BUILD_ARCH      ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

CFLAGS = -Wall -g

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

ifneq (,$(filter sparc mips hppa hurd-i386,$(DEB_BUILD_ARCH)))
	GRIP = --without-grib
else
	GRIP = --with-grib
endif

config.status: configure
	dh_testdir
	# Add here commands to configure the package.
	dh_autotools-dev_updateconfig
	dh_autoreconf
	# Patched source FTBFS if this embedded code copy is present
	rm -rf src/antlr
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs" --with-fftw=yes --with-python=yes --with-wxWidgets $(GRIP) --with-udunits --with-pslib

build: build-stamp

build-stamp:  config.status
	dh_testdir
	$(MAKE)
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_autoreconf_clean
	dh_autotools-dev_restoreconfig
	dh_clean --exclude ./src/ncdf_att_cl.cpp.orig --exclude ./src/basic_pro.cpp.orig --exclude ./src/basic_pro.hpp.orig --exclude ./src/libinit.cpp.orig --exclude ./src/ncdf_var_cl.cpp.orig

install: build
	dh_testdir
	dh_testroot
	dh_prep --exclude ./src/ncdf_att_cl.cpp.orig --exclude ./src/basic_pro.cpp.orig --exclude ./src/basic_pro.hpp.orig --exclude ./src/libinit.cpp.orig --exclude ./src/ncdf_var_cl.cpp.orig
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/gnudatalanguage install

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installman doc/gdl.1
	dh_link
	dh_strip
	dh_compress -X.pro
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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