#!/usr/bin/make -f

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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH)

#DEB_BUILD_MAINT_OPTIONS:= hardening=+all
DEB_CFLAGS_MAINT_APPEND:= -Wall 
export DEB_BUILD_MAINT_OPTIONS
#export DEB_CFLAGS_MAINT_APPEND

BUILD_ARCH_OS :-= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
BUILD_ARCH_CPU := $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU)
NETCDF_LIBS := $(shell pkg-config --libs heimdal-gssapi netcdf libffi)

PYVERS:=$(shell pyversions --supported)
PY3VERS:=$(shell py3versions --supported)
DESTDIR:=$(shell pwd)/debian/tmp

export CFLAGS:=-I/usr/include/ossp  -fstack-protector \
	--param=ssp-buffer-size=4 -Wformat -Wformat-security\
	-Werror=format-security  $(CFLAGS)

DO_TEST:=false
ifeq ($(BUILD_ARCH_OS)-$(BUILD_ARCH_CPU),kfreebsd-i386)
   DO_TEST:=false
endif
ifeq ($(BUILD_ARCH_CPU),s390x)
   DO_TEST:=false
endif

%:
	dh $@ --with python2,python3

override_dh_auto_configure:
	autoreconf -fiv
	ln -sf  /usr/share/misc/config.sub 
	dh_auto_configure -- --disable-color --enable-verbose-test  --with-uuid --without-python \
		UUIDLDFLAGS="-lossp-uuid" UUIDFLAGS="-I/usr/include/ossp" \
		CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" SZLIBFLAGS=" -lsz"

override_dh_auto_clean:
	rm -rf config.sub config.log config.status libcmor.a libcmor.so.2 build test_grid configure CMIP5
	rm -f ipcc_test_code Makefile setup.py *.mod compile_line.txt include/cmor_locale.h cmor.pc test_unit_cat_unique
	find . -name '*.nc' -delete
	find . -name '*.o' -delete
	find .  -name '*.pic_o' -delete
	find . -name '*.pyc' -delete

override_dh_auto_build:
	$(MAKE) FC=gfortran

override_dh_auto_test:
ifeq ($(DO_TEST),false)
	@echo "Tests disabled; see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=598745"
else
	dh_auto_test
endif


# Disabled. Run in piuparts, etc. instead
#	for p in $(PYVERS)  ; do \
		$(MAKE) test_python PYTHONEXEC=$$p DESTDIR=$(DESTDIR)  ; done
	

override_dh_auto_install:
	$(MAKE) install DESTDIR=$(DESTDIR)
	$(MAKE) install_shlib DESTDIR=$(DESTDIR)
	for p in $(PYVERS) $(PY3VERS)  ; do \
		$$p ./setup.py install  --prefix=$(DESTDIR)/usr --install-layout=deb ; \
		done
	dh_install -p libcmor2 libcmor.so.2    $(LIBDIR)
	dh_install -p libcmor-dev libcmor.a $(LIBDIR)
	dh_install -p libcmor-dev cmor.pc  $(LIBDIR)/pkgconfig
	dh_link -p libcmor-dev $(LIBDIR)/libcmor.so.2   $(LIBDIR)/libcmor.so
	dh_numpy
	dh_numpy3
