#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk

# FIXME:
# Gentoo report a bug that parallel builds don't work. subst -j 1.

# magic debhelper rule
%:
	dh $@ --sourcedirectory=src  --no-parallel  --without autoreconf

DESTDIR:=$(CURDIR)/debian/tmp/
LIBDIR:=/usr/lib/$(DEB_HOST_MULTIARCH)

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

FFLAGS_GFORTRAN=$(dh_fortran get_fc_flags --fc gfortran) -fallow-argument-mismatch
FFLAGS_FLANG=$(dh_fortran get_fc_flags --fc flang)

export CC=gcc
export NEQN="neqn -Tutf8"
export LD_X11=-lX11

# Path for runtime files, during build-time. Use this to avoid putting build path in fortc exe
export FORTC_LIBPATH=$(CURDIR)/src/port/fortc

override_dh_auto_configure:
	# Hack, but build system obsolete
	cp -a src build-gfortran
	cp -a src build-flang
	( cd build-gfortran && ../src/configure --prefix=/usr FC=gfortran CC="$(CC)"  NEQN=$(NEQN) \
		CFLAGS="$(CFLAGS) -std=gnu99" LDFLAGS="$(LDFLAGS)" CPPFLAGS="$(CPPFLAGS)" FFLAGS="$(FFLAGS_GFORTRAN)" )
	$(DO_FLANG) && ( cd build-flang && ../src/configure --prefix=/usr FC="$(FC_FLANG)" CC="$(CC)"  NEQN=$(NEQN) \
		CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" CPPFLAGS="$(CPPFLAGS)" FFLAGS="$(FFLAGS_FLANG)" ) \
		|| echo "Flang compiler not present; skipping"

override_dh_auto_build:
	# build process uses libs in their _installed_ location, so we need these:
	mkdir -p $(DESTDIR)/$(LIBDIR) $(DESTDIR)/usr/bin $(DESTDIR)/usr/include
	$(MAKE) -j1 -C build-gfortran all  DESTDIR=$(DESTDIR)
	# Stash a copy of these, as they'll be deleted on the next $(MAKE)
	cp ./build-gfortran/port/misc/libudport.so.2.6 build-gfortran
	cp ./build-gfortran/port/misc/libudport.a build-gfortran
	$(MAKE) -j1 -C build-gfortran/lib clean all LIBRARY=xgks-gnu DESTDIR=$(DESTDIR)
	$(DO_FLANG) && ( \
		cp build-gfortran/port/fortc/fortc ./build-flang/port/fortc/fortc ; \
		$(MAKE) -j1 -C build-flang/lib clean all LIBRARY=xgks-flang DESTDIR=$(DESTDIR)   ) \
		|| echo "Flang compiler not present; skipping"
	# And for multi-arch ...
	sed -e 's@LIBDIR@$(LIBDIR)@' < debian/xgks.pc.in > debian/xgks.pc

override_dh_auto_install:
	$(MAKE) -j1 -C build-gfortran install DESTDIR=$(DESTDIR)
	$(MAKE) -j1 -C build-gfortran/progs install DESTDIR=$(DESTDIR)
	$(MAKE) -j1 -C build-gfortran/port/fortc install DESTDIR=$(DESTDIR)
	$(MAKE) -j1 -C build-gfortran/port/misc install DESTDIR=$(DESTDIR)

execute_after_dh_auto_install:
	dh_install -p libxgks-dev debian/xgks.pc $(LIBDIR)/pkgconfig
	dh_install -p libxgks2t64 ./build-gfortran/lib/c/libxgks-gnu.so.2.6 $(LIBDIR)
	dh_install -p libxgks2t64 ./build-gfortran/libudport.so.2.6 $(LIBDIR)
	dh_install -p libxgks-dev ./build-gfortran/lib/c/libxgks.a       $(LIBDIR)/fortran/gnu
	dh_install -p libxgks-dev ./build-gfortran/libudport.a $(LIBDIR)
	$(DO_FLANG) && (  \
		dh_install -p libxgks2t64 ./build-flang/lib/c/libxgks-flang.so.2.6 $(LIBDIR) ; \
		dh_install -p libxgks-dev ./build-gfortran/lib/c/libxgks.a       $(LIBDIR)/fortran/flang ; \
		) || echo "Flang compiler not present; skipping"

execute_after_dh_link:
	dh_link -p libxgks2t64 $(LIBDIR)/libxgks-gnu.so.2.6  $(LIBDIR)/libxgks-gnu.so.2
	dh_link -p libxgks2t64 $(LIBDIR)/libudport.so.2.6 $(LIBDIR)/libudport.so.2
	dh_link -p libxgks-dev $(LIBDIR)/libxgks-gnu.so.2.6 $(LIBDIR)/libxgks-gnu.so
	dh_link -p libxgks-dev $(LIBDIR)/libudport.so.2.6 $(LIBDIR)/libudport.so
	$(DO_FLANG) && (  \
		dh_link -p libxgks2t64 $(LIBDIR)/libxgks-flang.so.2.6  $(LIBDIR)/libxgks-flang.so.2 ; \
		dh_link -p libxgks-dev $(LIBDIR)/libxgks-flang.so.2.6 $(LIBDIR)/libxgks-flang.so  ; \
		dh_link -p libxgks-dev $(LIBDIR)/libxgks-flang.so.2.6 $(LIBDIR)/fortran/flang/libxgks.so ; \
		) || echo "Flang compiler not present; skipping"

override_dh_auto_test:
	@echo "Testing disabled as tests are interactive"

override_dh_auto_clean:
	rm -rf build-*

