#!/usr/bin/make -f
# -*- makefile -*-

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
FFLAGS += -fPIC -frecursive
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
ifneq (,$(findstring sid,$(shell grep PRETTY_NAME /etc/os-release)))
  NETCDF_LIB=/usr/lib/$(DEB_HOST_MULTIARCH)/libnetcdf.so
  NETCDFF_LIB=/usr/lib/$(DEB_HOST_MULTIARCH)/libnetcdff.so
  NETCDF_INC=$(shell nf-config --includedir)/netcdf.h
  SYSFFLAGS=$(shell nc-config --cflags) $(shell nf-config --fflags)
  SYSLDLIBS=$(shell nf-config --libs) $(shell nc-config --libs)
else
  NETCDF_LIB=/usr/lib/libnetcdf.a
  NETCDFF_LIB=/usr/lib/libnetcdff.a
  NETCDF_INC=$(shell nf-config --includedir)/netcdf.h
  SYSFFLAGS=$(shell nc-config --fflags)
  SYSLDLIBS=$(shell nc-config --flibs)
endif

%:
	dh $@ --with autotools-dev

override_dh_auto_configure override_dh_auto_test:

override_dh_auto_build:
	ruby -Ku /usr/bin/rd2 \
	  -r rd/rd2html-lib \
	  --with-part=EN:rd CREDITS \
	  | LANG=C w3m -T text/html -dump > debian/CREDITS
	ruby -Ku /usr/bin/rd2 \
	  -r rd/rd2html-lib \
	  --with-part=EN:rd CREDITS \
	  | LANG=C w3m -T text/html -dump > debian/HISTORY
	( cd $(CURDIR)/debian/ && \
	  ruby -Ku /usr/bin/rd2 -r rd/rd2man-lib \
	    gt5frt.rd > gt5frt.1 && \
	  ruby -Ku /usr/bin/rd2 -r rd/rd2man-lib \
	    gt5config.rd > gt5config.1 )

override_dh_auto_install:
	FC=gfortran SYSFFLAGS="$(shell nf-config --fflags) -fPIC" RUBY="ruby -Ku" \
	   ./configure \
	   --prefix=/usr/lib/$(DEB_HOST_MULTIARCH)/gtool5 \
	   --with-netcdff=$(NETCDFF_LIB) \
	   --with-netcdf=$(NETCDF_LIB) \
	   --with-netcdf-include=$(NETCDF_INC)
	$(MAKE)
	$(MAKE) test
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) distclean
	FC=mpif90 SYSFFLAGS="$(shell nf-config --fflags) -fPIC" RUBY="ruby -Ku" \
	   ./configure \
	   --prefix=/usr/lib/$(DEB_HOST_MULTIARCH)/gtool5-mpi \
	   --with-netcdff=$(NETCDFF_LIB) \
	   --with-netcdf=$(NETCDF_LIB) \
	   --with-netcdf-include=$(NETCDF_INC)
	   --with-mpiexec=mpirun
	$(MAKE)
	$(MAKE) test
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

override_dh_install:
	dh_install --sourcedir=debian/tmp
	mkdir -p $(CURDIR)/debian/gtool5/usr/bin
	(cd $(CURDIR)/debian/gtool5/usr/bin && \
	  ln -s ../lib/$(DEB_HOST_MULTIARCH)/gtool5/bin/gt5frt gt5frt && \
	  ln -s ../lib/$(DEB_HOST_MULTIARCH)/gtool5/bin/gt5config gt5config && \
	  ln -s ../lib/$(DEB_HOST_MULTIARCH)/gtool5-mpi/bin/gt5frt gt5frt.mpi && \
	  ln -s ../lib/$(DEB_HOST_MULTIARCH)/gtool5-mpi/bin/gt5config gt5config.mpi )



override_dh_auto_clean:
	@[ ! -f $(CURDIR)/Config.mk ] || $(MAKE) distclean
	@find $(CRUDIR) -name depend | xargs rm -f
	@rm -f $(CURDIR)/debian/CREDITS $(CURDIR)/debian/HISTORY
	@rm -f $(CURDIR)/debian/*.1
	@rm -f $(CURDIR)/*.cfg $(CURDIR)/*.log
	dh_clean

override_dh_installdocs:
	dh_installdocs
	find $(CURDIR)/debian/gtool5-doc -name Makefile\* | xargs rm -f
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/SRC_LIST.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_history/gtool_history_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_history/gtool_history_types_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_historyauto/gtool_historyauto_generic_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/netcdf/netcdf_f77_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dcunits_com_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/classes/dc_calendar.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool5_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_units_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_historyauto/gtool_historyauto_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtdata/gtdata_generic_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_url_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_calendar_internal_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_date_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_history_nmlinfo/gtool_history_nmlinfo_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_history_nmlinfo/gtool_history_nmlinfo_types_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/classes/dc_date.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_scaledsec_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/classes/gtool5.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_types_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_calendar_generic_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_history_nmlinfo/gtool_history_nmlinfo_internal_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/classes/dc_utils.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_history_nmlinfo/gtool_history_nmlinfo_generic_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_calendar_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/netcdf/netcdf_f77_win_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtdata/gtdata_types_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_date_internal_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtdata/gtdata_internal_map_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_clock_F90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_message_F90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gt4f90io_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_regex_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_calendar_types_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_history/gtool_history_internal_F90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_hash_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_date_types_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_present_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/classes/gt4f90io.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_iounit_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_date_generic_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_string_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_historyauto/gtool_historyauto_internal_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/classes/gtool_history_nmlinfo.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/SRC_LIST.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/rdoc-style.css
	rm -f $(CURDIR)/debian/gtoo5l-doc/usr/share/doc/gtool5-doc/doc/develop_reference/rdoc-style.css
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_utils_f90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/dc_utils/dc_trace_F90.html
	rm -f $(CURDIR)/debian/gtool5-doc/usr/share/doc/gtool5-doc/doc/code_reference/files/gtool/gtool_history/gtool_history_generic_f90.html

override_dh_installchangelogs:
	dh_installchangelogs --all --keep $(CURDIR)/debian/HISTORY

override_dh_compress:
	dh_compress -X.rd -X.f90
