#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=-all
export CFLAGS =
export CXXFLAGS =
export LDFLAGS =

COMPILER_FAMILY := gnu15
PNAME           := R
VERSION         := 4.5.3
INSTALL_PATH    := /opt/ohpc/pub/libs/$(COMPILER_FAMILY)/$(PNAME)/$(VERSION)
SRCDIR          := $(CURDIR)/$(PNAME)-$(VERSION)
PKG             := $(CURDIR)/debian/r-$(COMPILER_FAMILY)-ohpc
ENV             := OHPC_MODULES="openblas" bash /build/devel/build-comp.sh

%:
	dh $@

override_dh_auto_configure:
	test -s SOURCES/$(PNAME)-$(VERSION).tar.gz || \
		wget -q -O SOURCES/$(PNAME)-$(VERSION).tar.gz \
			https://cran.r-project.org/src/base/R-4/R-$(VERSION).tar.gz
	rm -rf $(SRCDIR)
	tar xf SOURCES/$(PNAME)-$(VERSION).tar.gz
	cd $(SRCDIR) && $(ENV) sh -c ' \
		export BLAS="-L$${OPENBLAS_LIB} -lopenblas" && \
		./configure --with-lapack="$$BLAS" --with-blas="$$BLAS" --enable-R-shlib \
			--prefix=$(INSTALL_PATH) --without-system-zlib --without-system-bzlib \
			--without-x LIBnn=lib64 \
	'

override_dh_auto_build:
	cd $(SRCDIR) && $(ENV) $(MAKE) -j$$(nproc)

override_dh_auto_install:
	cd $(SRCDIR) && $(ENV) $(MAKE) DESTDIR=$(PKG) install
	# Remove backup files
	rm -f $(PKG)$(INSTALL_PATH)/lib64/R/library/survival/NEWS.Rd.orig
	find $(PKG) -name '*.la' -delete
	# Install modulefile
	mkdir -p $(PKG)/opt/ohpc/pub/moduledeps/$(COMPILER_FAMILY)/$(PNAME)
	install -m 644 debian/modulefile $(PKG)/opt/ohpc/pub/moduledeps/$(COMPILER_FAMILY)/$(PNAME)/$(VERSION)

override_dh_auto_clean:
	rm -rf $(SRCDIR)

override_dh_usrlocal:

override_dh_shlibdeps:
	dh_shlibdeps -l$(PKG)$(INSTALL_PATH)/lib64:$(PKG)$(INSTALL_PATH)/lib64/R/lib:/opt/ohpc/pub/libs/$(COMPILER_FAMILY)/openblas/0.3.32/lib -- --ignore-missing-info || true

override_dh_strip:
	dh_strip --no-automatic-dbgsym || true

override_dh_auto_test:
