#!/usr/bin/make -f

export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk

export DEB_BUILD_MAINT_OPTIONS = qa=-bug
export DEB_BUILD_MAINT_STRIP="-Wincompatible-pointer-types -Wint-conversion"
export DPKG_EXPORT_BUILDFLAGS=1
include  /usr/share/dpkg/buildflags.mk

# The magic debhelper  rule
%:
	dh $@ 

DESTDIR:=$(CURDIR)/debian/tmp/
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LIBDIR:=/usr/lib/$(DEB_HOST_MULTIARCH)
UPSTREAM_VERSION = $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's/\+.*//')

DO_FLANG:=$(if $(wildcard /usr/bin/flang),true, false)

# See #984251. Don't set flags for gfortran-9, only 10+
GF_FLAGS:=$(if $(filter $(shell readlink /usr/bin/gfortran), gfortran-9), , \
                 -fallow-invalid-boz -fallow-argument-mismatch )

# Disable these flags added for gcc14 on 32-bit platforms. The work to fix properly is too big
# for a legacy app on obsolete hardware
DISABLE_ERROR_FLAGS:= $(if $(filter $(DEB_TARGET_ARCH), armel armhf i386),  \
	-Wno-error=incompatible-pointer-types -Wno-error=int-conversion -Wno-error=implicit-function-declaration, \
	-Wno-error=int-conversion -Wno-error=implicit-function-declaration)

# For the config file. Done this way to pull in stuff from hardening, etc.
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)  $(DISABLE_ERROR_FLAGS)
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)  
# LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)
LDFLAGS:= 
FFLAGS=$(shell dpkg-buildflags --get FFLAGS) $(GF_FLAGS)
CC ?= cc


# FC set to f77 by default in make
# Read default compiler name unless FC is actually set
ifeq ($(F77),f77)
  F77:=$(shell basename $(shell readlink /etc/alternatives/f77))
endif

ifneq ($(filter flang,$(F77)),)
FFLAGS:=$(shell dpkg-buildflags --get FFLAGS |  sed -e 's/-g //')
CTOFLIBS:= -lm
endif
ifneq ($(filter gfortran,$(F77)),)
# export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
FFLAGS:=$(FFLAGS) -fno-second-underscore -fno-range-check
CTOFLIBS:= -lgfortran -lm
endif

ifneq ($(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial/libhdf5.so),)
  CFLAGS += -I/usr/include/hdf5/serial
  export HDF5_LDFLAGS := -Wl,-L/usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial
endif

export LD_LIBRARY_PATH=$(CURDIR)/shared:$(LIBDIR)/fortran/$(F77):$(LIBDIR)/libfakeroot

# Force HDF5 1.10 API to support building against HDF5 1.14
export DEB_CPPFLAGS_MAINT_APPEND = -DH5_USE_110_API

# We built it twice: first shared, to get the shared libraries (with -fpic -fPIC)
# then static, for execution speed.

LIBS:= \
	libNGcgm.so  libNGctrans.so   libNGgksPDF.so  libNGgksX.so     libNGlluC.so  libNGmisc2.so  libNGncl.so \
	libNGcn.so   libNGff.so       libNGgksPS.so   libNGhlu.so      libNGllu.so   libNGmisc.so   libNGnfp.so \
	libNGc.so    libNGgksCTXT.so  libNGgks.so     libNGictrans.so  libNGmath.so  libNGmp.so     libNGras.so \
	libfftpack5_dp.so  libNGnfpfort.so 

# Add extra libs to ensure all shared libs link
EXTRA_LIBS:= -lpng -lz -lX11 -lcairo -lsphere-$(F77)

# This done here to avoid subsitution bug on broken ymake buildsystem
INCS="-I/usr/include/freetype2 -I/usr/include/gdal  -I/usr/include/hdf-eos5 -I/usr/include/hdf -I/usr/include/${DEB_HOST_MULTIARCH}/hdf -I/usr/include/mpi"

override_dh_auto_configure:
	sed -e 's,@LIBDIR@,$(LIBDIR),' \
	    -e 's/@PREFIX@/\/usr/' \
	    -e 's/@VERSION@/$(UPSTREAM_VERSION)/' \
	< debian/ncarg.pc.in > ncarg.pc
	sed -e 's%@CC@%${CC}%' \
	    -e 's%@FC@%${FC}%' \
	    -e 's%@LD@%${CC}%' \
	    -e 's%@CFLAGS@%${CFLAGS}%' \
	    -e 's%@FFLAGS@%${FFLAGS}%' \
	    -e 's%@CTOFLIBS@%${CTOFLIBS}%' \
	    -e 's%@CPPFLAGS@%${CPPFLAGS}%' \
	    -e 's%@LDFLAGS@%${LDFLAGS}%' \
	    -e 's%@DESTDIR@%${DESTDIR}%' \
	< debian/Site.local.shared.in > config/Site.local.shared
	sed -e 's%@CC@%${CC}%' \
	    -e 's%@FC@%${FC}%' \
	    -e 's%@LD@%${CC}%' \
	    -e 's%@CFLAGS@%${CFLAGS}%' \
	    -e 's%@FFLAGS@%${FFLAGS}%' \
	    -e 's%@CTOFLIBS@%${CTOFLIBS}%' \
	    -e 's%@CPPFLAGS@%${CPPFLAGS}%' \
	    -e 's%@LDFLAGS@%${LDFLAGS}%' \
	    -e 's%@DESTDIR@%${DESTDIR}%' \
	< debian/Site.local.static.in > config/Site.local.static
	sed -e 's%@ARCH@%${DEB_HOST_MULTIARCH}%' \
	< config/Project > config/Project.post
	mv config/Project.post config/Project 

override_dh_auto_build:
	cp config/Site.local.shared config/Site.local
	cp debian/LINUX.DEBIAN.$(F77) config/LINUX
	ln -sf LINUX config/DEBIAN
	# regenerate lex file; shipped was non-dfsg
	( cd ./ncarview/src/lib/libictrans ; flex -o lex.yy_linux.c lex.l )
	echo 'n' | ./Configure -v
	$(MAKE) Makefiles includes depend
	# Static libraries need to be built first
	# Add symlink for non-Linux archs
	cp config/Site.local.static config/Site.local
	echo 'n' | ./Configure -v
	$(MAKE) Makefiles includes depend
	for d in common external ngmath/src/lib ncarg2d/src/libncarg_gks ncarg2d/src/libncarg ncarg2d/src/liboptional  ncarview ni/src/lib ; do \
		$(MAKE) -C $$d install INCSEARCH=${INCS} INC_SEARCH=${INCS} ; done
	LD_LIBRARY_PATH=$(DESTDIR)/lib:$(LIBDIR)/fortran/$(F77) $(MAKE) install INCSEARCH=${INCS} INC_SEARCH=${INCS} 
	# Install them safely in $(DESTDIR) as originals will be removed making shared libs
	mkdir -p $(DESTDIR)/staticlibs
	mv $(DESTDIR)/lib/*.a $(DESTDIR)/staticlibs
	$(MAKE) clean
	find . -name '*.[o]' -delete
	# Shared libraries next, in the right order.
	cp config/Site.local.shared config/Site.local
	echo 'n' | ./Configure -v
	$(MAKE) Makefiles includes depend
	for d in common external ngmath/src/lib ncarg2d/src/libncarg_gks ncarg2d/src/libncarg ncarg2d/src/liboptional  ncarview ni/src/lib/nfpfort  ni/src/lib ; do \
		LD_LIBRARY_PATH=$(DESTDIR)/lib:$(LIBDIR)/fortran/$(F77):$(CURDIR)/shared $(MAKE) -C $$d install \
			DEV_SYS_LIBS=$(EXTRA_LIBS)   INCSEARCH=${INCS} INC_SEARCH=${INCS}; done
	# Then build all the rest
	LD_LIBRARY_PATH=$(DESTDIR)/lib:$(LIBDIR)/fortran/$(F77):$(CURDIR)/shared $(MAKE) Everything \
		DEV_SYS_LIBS=$(EXTRA_LIBS) INCSEARCH=${INCS} INC_SEARCH=${INCS} LD_REL="ld -zmuldefs" LIBSEARCH=-L$(LIBDIR)/hdf5/serial/lib
	mv $(DESTDIR)/staticlibs/* $(DESTDIR)/lib

override_dh_auto_install:
	dh_auto_install
	mkdir -p debian/libncarg-dev/$(LIBDIR)/ncarg
	# TODO: These may not be present on mips64el. 
	mkdir -p debian/libncarg-data/usr/share/ncarg/nug
	[ -d $(DESTDIR)/lib/ncarg/fontcaps ]   && cp -a $(DESTDIR)/lib/ncarg/fontcaps debian/libncarg-data/usr/share/ncarg || true
	[ -d $(DESTDIR)/lib/ncarg/nclex/nug  ] && cp -a $(DESTDIR)/lib/ncarg/nclex/nug debian/libncarg-data/usr/share/ncarg/nug || true
	cp $(DESTDIR)/lib/*.a debian/libncarg-dev/$(LIBDIR)/ncarg
	find debian -name '*.ksh' -exec chmod +x {} \;
	# Sanitise paths for bit reproducibility
	for f in $(DESTDIR)/usr/bin/ncargcc \
		 $(DESTDIR)/usr/bin/ncargf77 \
		 $(DESTDIR)/usr/bin/ncargf90 \
		 $(DESTDIR)/usr/bin/nhlcc \
		 $(DESTDIR)/usr/bin/nhlf77 \
		 $(DESTDIR)/usr/bin/nhlf90 ; do cat $$f | \
		 sed -e 's%${CURDIR}%/build/ncl-ncarg%g'  \
                        > debian/tmpff ; \
		mv debian/tmpff $$f ; \
		done

override_dh_auto_clean:
	find . -name '*.so.1' -delete
	find . -type l -delete
	find . -name '*.o' -delete
	rm -f ncarg.pc config/Site.local.* config/DEBIAN
ifneq ($(wildcard config/LINUX.ORIG),)
	mv config/LINUX.ORIG config/LINUX
endif

ifneq (,$(filter $(DEB_BUILD_ARCH),mipsel alpha))
override_dh_dwz:
	@echo bypass dwz that causes build failues
endif
