#!/usr/bin/make -f

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

PNAME           := lustre
VERSION         := 2.15.1
SRCDIR          := $(CURDIR)/$(PNAME)-$(VERSION)
PKG             := $(CURDIR)/debian/lustre-client-ohpc

%:
	dh $@

override_dh_auto_configure:
	test -s SOURCES/$(PNAME)-$(VERSION).tar.gz || \
		wget -q -O SOURCES/$(PNAME)-$(VERSION).tar.gz \
			https://downloads.whamcloud.com/public/lustre/lustre-$(VERSION)/el9.5/client/SRPMS/lustre-$(VERSION)-1.src.rpm && \
		cd SOURCES && rpm2cpio lustre-$(VERSION)-1.src.rpm | cpio -id "lustre-$(VERSION).tar.gz" 2>/dev/null && \
		mv lustre-$(VERSION).tar.gz $(PNAME)-$(VERSION).tar.gz || true
	test -s SOURCES/$(PNAME)-$(VERSION).tar.gz || \
		wget -q -O SOURCES/$(PNAME)-$(VERSION).tar.gz \
			https://github.com/lustre/lustre-release/archive/refs/tags/$(VERSION).tar.gz
	rm -rf $(SRCDIR) lustre-release-$(VERSION)
	tar xf SOURCES/$(PNAME)-$(VERSION).tar.gz
	test -d $(SRCDIR) || mv lustre-release-$(VERSION) $(SRCDIR) 2>/dev/null || true

override_dh_auto_build:
	# Create kernel module build symlink (may fail as non-root in OBS chroot)
	$(eval KVER := $(shell ls /usr/src/ 2>/dev/null | grep "linux-headers-" | grep -v common | sort -V | tail -1 | sed "s/linux-headers-//"))
	mkdir -p /lib/modules/$(KVER) 2>/dev/null || true
	ln -sfn /usr/src/linux-headers-$(KVER) /lib/modules/$(KVER)/build 2>/dev/null || true
	cd $(SRCDIR) && sh autogen.sh && \
		./configure \
			--prefix=/usr \
			--sysconfdir=/etc \
			--disable-server \
			--disable-modules \
			--disable-tests \
			--enable-utils \
			--enable-client \
			--disable-gss && \
		make -j$$(nproc) \
			CFLAGS="-g -O2 -Wall -I$(SRCDIR)/lnet/include/uapi -I$(SRCDIR)/lustre/include/uapi"

override_dh_auto_install:
	cd $(SRCDIR) && make DESTDIR=$(PKG) install

override_dh_auto_clean:
	rm -rf $(SRCDIR)

override_dh_usrlocal:

override_dh_shlibdeps:
	dh_shlibdeps -- --ignore-missing-info || true

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

override_dh_auto_test:
