#!/usr/bin/make -f

# export DH_VERBOSE=1

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

include /usr/share/mpi-default-dev/debian_defaults
MPI=$(ARCH_DEFAULT_MPI_IMPL)

# TODO: This should come from `pkg-config mpi --libs` ...
ifeq ($(MPI), mpich)
MPI_LIBS:= -Wl,-z,relro -lmpich -lopa -lmpl -lrt -lcr -lpthread
ifeq ($(ARCH),hppa)
MPI_LIBS:= -Wl,-z,relro  -lmpich -lopa -lmpl  -lrt -lpthread
endif
ifeq ($(ARCH), m68k)
MPI_LIBS:= -Wl,-z,relro -lmpich -lopa -lmpl -lrt -lpthread
endif
ifeq ($(ARCH), x32)
MPI_LIBS:= -Wl,-z,relro -lmpich -lopa -lmpl -lrt -lpthread
endif
endif

ifeq ($(MPI), openmpi)
MPI_LIBS:= $(pkg-config mpi-fort --libs)
endif
ifeq ($(ARCH),s390x)
MPI_LIBS:= -Wl,-z,relro  -lmpich -lopa -lmpl  -lrt -lpthread
endif
ifeq ($(ARCH),hurd-i386)
  WITH_IBVERBS:=
endif
ifeq ($(ARCH),kfreebsd-amd64)
  WITH_IBVERBS:=
endif
ifeq ($(ARCH),kfreebsd-i386)
  WITH_IBVERBS:=
endif

# TODO: also mpich2 for x32 ...
# 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
PY2VERS:=python2.7
PY3VERS:= $(shell py3versions -s)
CFLAGS:=$(shell  dpkg-buildflags --get CFLAGS )

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
	find . -name '*.o' -delete
	find . -name '*.a' -delete
	find . -name '*.mod' -delete
	rm -fr wrappers/numpy/build wrappers/java/build debian/tmpp
	# Remove dodgy symlinks
	find . -type l -delete
	rm -f ./examples/staging/stage_write/writer_adios config.log 
	rm -f libtool config.h  config.status adios_config.flags  adios_config
	rm -f adios.pc stamp-h1

override_dh_auto_configure:
	dh_auto_configure --  $(WITH_LUSTRE) $(WITH_IBVERBS) --enable-shared \
		--with-hdf5-incdir=/usr/include/hdf5/serial \
		--with-hdf5-libdir=$(LIBDIR)/hdf5/serial \
		--with-phdf5-incdir=/usr/include/hdf5/$(MPI) \
		--with-phdf5-libdir=$(LIBDIR)/hdf5/$(MPI) \
		--with-bzip2=/usr  --with-zlib=/usr --with-glib=/usr \
		--prefix=/usr MPICC=mpicc.$(MPI) MPIFC=mpif90.$(MPI) CFLAGS='$(CFLAGS) -I$(LIBDIR)/glib-2.0/include' \
		LIBS=' $(MPI_LIBS) -lpthread' 

override_dh_auto_build:
	# First do a build shared, with -fPIC, for the python lib.
	$(MAKE) -C src CFLAGS='$(CFLAGS) -fPIC' VERBOSE=1 install DESTDIR=$(DESTDIR)
	# Need to hack the libs into the right place, to match adios_configs view
	# as this is then used by setup.py
	mv debian/tmp/usr/lib/*/*.a debian/tmp/usr/lib
	# Now build Python wrapper. Uses adios_config and the debian/tmp install.
	chmod +x adios_config
	$(MAKE) -C wrappers/numpy python CYTHON=y CYTHON3=y MPI=y
	( cd wrappers/numpy && \
		    ${PY2VERS} ./setup.py     install --prefix=$(TOP)/debian/tmpp  ;\
		    ${PY2VERS} ./setup_mpi.py install --prefix=$(TOP)/debian/tmpp)
	for p in $(PY3VERS) ; do \
		( cd wrappers/numpy && \
		    $$p ./setup3.py     install --prefix=$(TOP)/debian/tmpp   ;\
		    $$p ./setup3_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 DESTDIR=$(DESTDIR)
	install -D -m 644  adios.pc $(TOP)/debian/tmp/$(LIBDIR)/pkgconfig/adios.pc
	# Sort python stuff
	mkdir -p $(TOP)/debian/tmp/usr/lib/python3/dist-packages
	mkdir -p $(TOP)/debian/tmp/usr/lib/${PY2VERS}/dist-packages
	for p in $(PY3VERS); do \
		v=`echo $$p | cut -c7,9` ;\
		CYV=cpython-$${v}m ;\
		CYVM=cpython-$${v}m-${DEB_HOST_MULTIARCH} ;\
		if [ $$p = python3.5 ]; then \
			CYV=$$CYVM ;\
		fi;\
		mv $(TOP)/debian/tmpp/lib/$${p}/site-packages/adios3.$${CYV}.so \
			debian/tmp/usr/lib/python3/dist-packages/adios.$${CYVM}.so  ; \
		mv $(TOP)/debian/tmpp/lib/$${p}/site-packages/adios3_mpi.$${CYV}.so \
			debian/tmp/usr/lib/python3/dist-packages/adios_mpi.$${CYVM}.so  ; \
	done
	mv $(TOP)/debian/tmpp/lib/${PY2VERS}/site-packages/adios_mpi.so \
		debian/tmp/usr/lib/${PY2VERS}/dist-packages/adios_mpi.${DEB_HOST_MULTIARCH}.so
	mv $(TOP)/debian/tmpp/lib/${PY2VERS}/site-packages/adios.so \
		debian/tmp/usr/lib/${PY2VERS}/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-dev/usr/share/doc/libadios-dev/examples -name '*.o' -delete 
	find debian/libadios-dev/usr/share/doc/libadios-dev/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
