#!/usr/bin/make -f

# export DH_VERBOSE=1

# Cope with lam not providing mpicc.
include /usr/share/mpi-default-dev/debian_defaults
ifeq ($(ARCH_DEFAULT_MPI_IMPL),lam)
  CC:=mpicc.lam
  CXX:=mpicc.lam
else
  CC:=mpicc
  CXX:=mpic++
endif

ARCH:=$(shell dpkg --print-architecture)
WITH_LUSTRE :=
WITH_IBVERBS:= --with-ibverbs=/usr
DO_TEST:= true

# Do tests on little-endian only for the moment;
# lustre currently not provided in Debian sid

ifeq ($(ARCH), i386)
#  WITH_LUSTRE:= --with-lustre=/usr
endif
ifeq ($(ARCH), amd64)
#  WITH_LUSTRE:= --with-lustre=/usr
endif
ifeq ($(ARCH), powerpc)
#  WITH_LUSTRE:= --with-lustre=/usr
   DO_TEST:= false
endif
ifeq ($(ARCH), ia64)
#  WITH_LUSTRE:= --with-lustre=/usr
endif
ifeq ($(ARCH), sparc)
   DO_TEST:= false
endif
ifeq ($(ARCH), mips)
   DO_TEST:= false
endif
ifeq ($(ARCH),hurd-i386)
  WITH_IBVERBS:=
endif
ifeq ($(ARCH),kfreebsd-amd64)
  WITH_IBVERBS:=
endif
ifeq ($(ARCH),kfreebsd-i386)
  WITH_IBVERBS:=
endif

# Temporary, allowing hdf5 transition. Before jessie release,
# (when netcdf has transitioned to multiple hdf5), allow both
#  serial and mpi versions of ADIOS libraries
ifneq ($(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial/libhdf5.so),)
  WITH_HDF5:=--with-hdf5=/usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial
else
  WITH_HDF5:=--with-hdf5=/usr
endif

DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

DESTDIR:=$(shell pwd)/debian/tmp
DEB_HOST_MULTIARCH:=$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LIBDIR:=/usr/lib/$(DEB_HOST_MULTIARCH)
BINDIR:=$(DESTDIR)/usr/bin
PY2:=python2.7
PY3VERS:= $(shell py3versions -s)

TOP:=$(shell pwd)
export PATH:=$(TOP):$(PATH)

# magic debhelper rule
%:
	dh $@ --with autoreconf,autotools_dev,python2,python3

override_dh_auto_clean:
	dh_clean
	find . -name '*.pyc' -delete
	rm -fr wrappers/numpy/build wrappers/java/build debian/tmpp
	# Remove dodgy symlink
	rm -f ./examples/staging/stage_write/writer_adios

override_dh_auto_configure:
	dh_auto_configure --  $(WITH_LUSTRE) $(WITH_IBVERBS) --enable-shared \
		$(WITH_HDF5) --with-bzip2=/usr  --with-zlib=/usr \
		--prefix=$(DESTDIR) MPIFC=mpif77 CC=$(CC) # CFLAGS='-fPIC'

override_dh_auto_build:
	# First do a build shared, with -fPIC, for the python lib.
	$(MAKE) -C src CFLAGS='-fPIC' VERBOSE=1 install
	# Need to hack the libs into the right place, to match adios_configs view
	mv debian/tmp/lib/*/*.a debian/tmp/lib
	# Now build Python wrapper. Uses adios_config and the debian/tmp install.
	for p in ${PY2} ${PY3VERS} ; do \
		( cd wrappers/numpy && \
		    $$p ./setup.py     install --prefix=$(TOP)/debian/tmpp  ;\
			$$p ./setup_mpi.py install --prefix=$(TOP)/debian/tmpp ) ; done
	# Clean up.
	rm -r debian/tmp/*
	# Now do the static build we want.
	dh_auto_build

override_dh_auto_install:
	find . -name .gitignore -delete
	$(MAKE) install prefix=$(DESTDIR)/usr
	install -D -m 644  adios.pc $(TOP)/debian/tmp/$(LIBDIR)/pkgconfig
	# Sort python stuff
	mkdir -p $(TOP)/debian/tmp/usr/lib/python3/dist-packages
	mkdir -p $(TOP)/debian/tmp/usr/lib/${PY2}/dist-packages
	for p in $(PY3VERS); do \
		v=`echo $$p | cut -c7,9` ;\
		CYV=cpython-$${v}m ;\
		mv $(TOP)/debian/tmpp/lib/$${p}/site-packages/adios.$${CYV}.so \
			debian/tmp/usr/lib/python3/dist-packages/adios.$${CYV}-${DEB_HOST_MULTIARCH}.so  ; \
		mv $(TOP)/debian/tmpp/lib/$${p}/site-packages/adios_mpi.$${CYV}.so \
			debian/tmp/usr/lib/python3/dist-packages/adios_mpi.$${CYV}-${DEB_HOST_MULTIARCH}.so  ; \
	done
	mv $(TOP)/debian/tmpp/lib/${PY2}/site-packages/adios_mpi.so \
		debian/tmp/usr/lib/${PY2}/dist-packages/adios_mpi.${DEB_HOST_MULTIARCH}.so
	mv $(TOP)/debian/tmpp/lib/${PY2}/site-packages/adios.so \
		debian/tmp/usr/lib/${PY2}/dist-packages/adios.${DEB_HOST_MULTIARCH}.so
	rm -r debian/tmpp
	# remove due to it not working in multi-arch;
	rm -f  $(BINDIR)/adios_config.flags
	# renamed as per Adios CCS issue #118716, Debbug #639257
	mv $(BINDIR)/gpp.py $(BINDIR)/adiosxml2h
	mv $(BINDIR)/ad_config.py $(BINDIR)/ad_config
	mv $(BINDIR)/skel_cat.py $(BINDIR)/skel_cat
	mv $(BINDIR)/skel_extract.py $(BINDIR)/skel_extract
	cp debian/adios_config $(BINDIR)/adios_config
	# mv $(DESTDIR)/bin/xmlparser.py $(DESTDIR)/bin/xmlparser

override_dh_auto_test:
	$(DO_TEST) && $(MAKE) check || echo "Tests disabled on bigendian systems for the moment"

override_dh_installexamples:
	dh_installexamples
	find debian/libadios-examples/usr/share/doc/libadios-examples/examples -name '*.o' -delete
	find debian/libadios-examples/usr/share/doc/libadios-examples/examples/C -type f -exec chmod -x {} \;

override_dh_fixperms:
	dh_fixperms
	chmod +x debian/libadios-bin/etc/adios/skel/templates/*.tpl
	chmod -x debian/libadios-bin/etc/adios/skel/templates/*.tmpl
