#!/usr/bin/make -f

# The magic rule ...
%:
	dh $@

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LIBDIR:=/usr/lib/${DEB_HOST_MULTIARCH}
LIBDIR_ESC:=\/usr\/lib\/${DEB_HOST_MULTIARCH}

include /usr/share/mpi-default-dev/debian_defaults
ifeq ($(ARCH_DEFAULT_MPI_IMPL), mpich)
        CHAN := MPI1
else
	CHAN := MPI2
endif

TOP:=$(shell pwd)/

override_dh_auto_build:
	# First run to build shared lib
	$(MAKE) -C src/mod/oasis3/util/make_dir -f TopMakefileOasis3 ARCH=debian PRISMHOME=$(TOP) CHAN=MPI1 makeshlibs
	$(MAKE) -C src/mod/oasis3/util/make_dir -f TopMakefileOasis3 ARCH=debian PRISMHOME=$(TOP) CHAN=MPI2 makeshlibs
	# move them out of the way so the executables are built statically
	mkdir -p build/lib/shared
	mv build/lib/lib*.so.* build/lib/shared
	# re-run to build the static versions
	$(MAKE)  -C src/mod/oasis3/util/make_dir -f TopMakefileOasis3 ARCH=debian PRISMHOME=$(TOP) realclean CHAN=MPI1 default toyoa3
	$(MAKE)  -C src/mod/oasis3/util/make_dir -f TopMakefileOasis3 ARCH=debian PRISMHOME=$(TOP) realclean CHAN=MPI2 default toyoa3
	# Create pkgconfig files.
	for p in clim.mpich.pc  clim.openmpi.pc  mpp_io.pc  psmile.pc  scrip.pc ; do \
		sed -e 's/@LIBDIR@/${LIBDIR_ESC}/' < debian/$${p}.in > debian/$${p}; \
		done

override_dh_auto_install:
	dh_install -p liboasis3-0d build/lib/shared/libclim*.so.0d ${LIBDIR}
	dh_install -p liboasis3-0d build/lib/shared/libfscint.so.0d  ${LIBDIR}
	dh_install -p liboasis3-0d build/lib/shared/libmpp_io.so.0d  ${LIBDIR}
	dh_install -p liboasis3-0d build/lib/shared/libpsmile*.so.0d ${LIBDIR}
	dh_install -p liboasis3-0d build/lib/shared/libscrip.so.0d   ${LIBDIR}
	dh_install -p liboasis3-0d build/lib/shared/libanais*.so.0d   ${LIBDIR}
	dh_install -p liboasis3-0d build/lib/oasis3/liboasis3.*.so.0d ${LIBDIR}
	dh_install -p liboasis3-dev build/lib/libclim.*.a   ${LIBDIR}
	dh_install -p liboasis3-dev build/lib/libpsmile.*.a ${LIBDIR}
	dh_install -p liboasis3-dev build/lib/libfscint.a   ${LIBDIR}
	dh_install -p liboasis3-dev build/lib/libmpp_io.a   ${LIBDIR}
	dh_install -p liboasis3-dev build/lib/libscrip.a    ${LIBDIR}
	dh_install -p liboasis3-dev build/lib/libanaism.a   ${LIBDIR}
	dh_install -p liboasis3-dev build/lib/libanaisg.a   ${LIBDIR}
	dh_install -p liboasis3-dev build/lib/oasis3/liboasis3.*.a  ${LIBDIR}
	dh_install -p liboasis3-dev debian/*.pc     ${LIBDIR}/pkgconfig

override_dh_link:
	ln -sf oasis3.links.$(CHAN) debian/oasis3.links
	ln -sf oasis3-examples.links.$(CHAN) debian/oasis3-examples.links
	dh_link -p liboasis3-dev ${LIBDIR}/pkgconfig/clim.${ARCH_DEFAULT_MPI_IMPL}.pc ${LIBDIR}/pkgconfig/clim.pc
	dh_link -p liboasis3-dev ${LIBDIR}/libclim.MPI2.so.0d     ${LIBDIR}/libclim.MPI2.so
	dh_link -p liboasis3-dev ${LIBDIR}/libclim.MPI1.so.0d     ${LIBDIR}/libclim.MPI1.so
	dh_link -p liboasis3-dev ${LIBDIR}/libpsmile.MPI2.so.0d   ${LIBDIR}/libpsmile.MPI2.so
	dh_link -p liboasis3-dev ${LIBDIR}/libpsmile.MPI1.so.0d   ${LIBDIR}/libpsmile.MPI1.so
	dh_link -p liboasis3-dev ${LIBDIR}/liboasis3.MPI1.so.0d   ${LIBDIR}/liboasis3.MPI1.so
	dh_link -p liboasis3-dev ${LIBDIR}/liboasis3.MPI2.so.0d   ${LIBDIR}/liboasis3.MPI2.so
	dh_link -p liboasis3-dev ${LIBDIR}/libanaism.so.0d        ${LIBDIR}/libanaism.so
	dh_link -p liboasis3-dev ${LIBDIR}/libanaisg.so.0d        ${LIBDIR}/libanaisg.so
	dh_link -p liboasis3-dev ${LIBDIR}/libfscint.so.0d        ${LIBDIR}/libfscint.so
	dh_link -p liboasis3-dev ${LIBDIR}/libmpp_io.so.0d        ${LIBDIR}/libmpp_io.so
	dh_link -p liboasis3-dev ${LIBDIR}/libscrip.so.0d ${LIBDIR}/libscrip.so
	dh_link -p liboasis3-dev ${LIBDIR}/liboasis3.${CHAN}.a       ${LIBDIR}/liboasis3.a
	dh_link -p liboasis3-dev ${LIBDIR}/libclim.${CHAN}.a         ${LIBDIR}/libclim.a
	dh_link -p liboasis3-dev ${LIBDIR}/libpsmile.${CHAN}.a       ${LIBDIR}/libpsmile.a
	dh_link -p liboasis3-dev ${LIBDIR}/libclim.${CHAN}.so.0d     ${LIBDIR}/libclim.so
	dh_link -p liboasis3-dev ${LIBDIR}/liboasis3.${CHAN}.so.0d   ${LIBDIR}/liboasis3.so
	dh_link -p liboasis3-dev ${LIBDIR}/libpsmile.${CHAN}.so.0d   ${LIBDIR}/libpsmile.so
	dh_link -p liboasis3-dev /usr/include/clim.${CHAN}          /usr/include/clim
	dh_link -p liboasis3-dev /usr/include/psmile.${CHAN}        /usr/include/psmile

override_dh_shlibdeps:
	dh_shlibdeps -l$(TOP)/build/lib/shared

override_dh_fixperms:
	dh_fixperms
	find debian/oasis3-examples/usr/share/oasis3-examples/testinterp -type f -exec chmod -x {} \;
	find debian/oasis3-examples -name 'Make*' -exec chmod -x {} \;

override_dh_clean:
	dh_clean
	rm -rf build src/mod/oasis3/doc/oasis3doc.toc debian/*.links debian/*.pc
	if [ -e src/mod/oasis3/doc/Makefile ] ; then $(MAKE) -C src/mod/oasis3/doc clean ; fi


pkgconfig-files: 
