#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# This version is for a hypothetical package that can build a kernel modules
# architecture-dependant package via make-kpkg, as well as an
# architecture-independent module source package, and other packages
# either dep/indep for things like common files or userspace components
# needed for the kernel modules.

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

-include /usr/share/dpkg/architecture.mk

ifeq (,$(findstring modules,${DEB_BUILD_PROFILES}))
WITH_DKMS = 1
else
WITH_DKMS = 0
endif

WITH_MOD_SIGN ?= 0
MLXNUMC = $(shell grep ^processor /proc/cpuinfo | wc -l)
NJOBS ?= $(shell if [ $(MLXNUMC) -lt 16 ]; then echo $(MLXNUMC); else echo 16; fi)
ifeq ($(WITH_DKMS),1)
# 1 for dkms >= 3 . 0 for dkms 2 (or no DKMS support at all)
DKMS3 = $(shell dpkg-query -W -f='$${source:Upstream-Version}' dh-dkms 2>/dev/null | awk -F. '$$1 > 2 {print 1}')
else
DKMS3 = 0
endif


pname:=mlnx-ofed-kernel
ifeq ($(WITH_DKMS),1)
pdkms:=$(pname)-dkms
else
pdkms:=$(pname)-modules
endif
putils:=$(pname)-utils

pversion := $(shell dpkg-parsechangelog | sed -n 's/^Version: *\([^-]\+\)-.\+/\1/p')
prel := $(shell dpkg-parsechangelog | sed -n 's/^Version: *\([^-]\+\)-\(.\+\)/\2/p')
dkms_version := $(pversion)-$(prel)

# for dh_exec:
export dkms_version
export pname
export pversion

export INSTALL_MOD_DIR:=updates
export INSTALL_MOD_PATH:=$(CURDIR)/debian/$(pdkms)

DIST_NAME := $(shell lsb_release -si)
DIST_RELEASE := $(DIST_NAME)/$(shell lsb_release -sc)

kernelver ?= $(shell uname -r)
kernelver1 = $(shell echo $(kernelver) | sed -e 's/_/-/g')
kernel_source_dir ?= "/lib/modules/$(kernelver)/build"
deb_arch = $(shell uname -m)

%:
ifeq ($(WITH_DKMS),1)
	dh $@ --with dkms
else
	dh $@
endif

override_dh_auto_clean:

override_dh_auto_configure:
	ofed_scripts/ofed_patch.sh
	if [ ! -f compat/configure ]; then cd compat && ./autogen.sh; fi
	# backup clean sources
	/bin/rm -rf source || true
	mkdir -p source
	ls -1 | grep -v source | xargs -iELEM cp ELEM -r source
ifneq ($(WITH_DKMS),1)
	@echo Building for $(kernelver)
	find compat -type f -exec touch -t 200012201010 '{}' \; || true
	$(CURDIR)/configure --kernel-version=$(kernelver) --kernel-sources=$(kernel_source_dir) --default $$configure_options --with-njobs=$(NJOBS)
endif

mod_dev_dir = /usr/src/ofa_kernel/$(deb_arch)/$(kernelver)
override_dh_auto_build:
	sed -e 's/@VERSION@/$(dkms_version)/' -e '/^PACKAGE_NAME=/s/=.*/="$(pname)"/'  <dkms.conf >debian/dkms.conf
ifneq ($(WITH_DKMS),1)
	make distclean || true
	make -j$(NJOBS)
endif
	# Prepare /etc/infiniband/info
	echo '#!/bin/sh'						>  debian/info
	echo								>> debian/info
	echo 'echo prefix=/usr'						>> debian/info
	echo 'echo Kernel=`uname -r`'					>> debian/info
	echo 'echo'							>> debian/info
	echo 'echo Configure options: --default $$configure_options'	>> debian/info
	echo 'echo'							>> debian/info
	chmod 755 debian/info
override_dh_auto_test:

override_dh_auto_install:
ifneq ($(WITH_DKMS),1)
	make install_modules INSTALL_MOD_DIR=$(INSTALL_MOD_DIR) INSTALL_MOD_PATH=$(INSTALL_MOD_PATH) KERNELRELEASE=$(kernelver)
	find $(INSTALL_MOD_PATH) \( -type f -a -name "modules.*" \) -delete
ifeq ($(WITH_MOD_SIGN),1)
	ofed_scripts/tools/sign-modules $(INSTALL_MOD_PATH)/lib/modules/ $(kernel_source_dir)
endif
endif


	#
	# Sources for building stuff over MLNX_OFED
	#
	# With DKMS, copy the below files and folders during post_build since the
	# new DKMS versions started to remove the whole build directory.
	# however, some old versions of DKMS are buggy and ignores POST_BUILD
ifeq ($(WITH_DKMS),1)
else
	dh_installdirs -p$(pdkms)  $(mod_dev_dir)
	cp -a include/			debian/$(pdkms)$(mod_dev_dir)
	cp -a config*			debian/$(pdkms)$(mod_dev_dir)
	cp -a compat*			debian/$(pdkms)$(mod_dev_dir)
	cp -a ofed_scripts		debian/$(pdkms)$(mod_dev_dir)
	cp -a Module*.symvers		debian/$(pdkms)$(mod_dev_dir)
endif

	# Force DKMS to install our modules.
	# This is mostly needed for modules that do not have a version number info, as DKMS
	# will compare their srcversion field, which does not really say which module is newer.
ifeq ($(WITH_DKMS),1)
	dh_installdirs -p$(pdkms)  usr/share/dkms/modules_to_force_install/
	echo "$(pname)" > debian/$(pdkms)/usr/share/dkms/modules_to_force_install/$(pname).force
endif
ifneq ($(WITH_DKMS),1)
	$(CURDIR)/ofed_scripts/install_helper $(CURDIR)/debian/$(putils)
	# For macsec:
	var=; \
	if apt-cache show "linux-modules-extra-$(kernelver)" >/dev/null 2>&1 \
	  && LC_ALL=C apt-cache show linux-modules-extra-$(kernelver) 2>/dev/null| grep -q -v ^N:; \
	then \
	  var="linux-modules-extra-$(kernelver)"; \
	fi; \
	echo "extra:Depends=$$var" >> debian/$(pdkms).substvars
endif

override_dh_installinit:

# Because of the generated file debian/info:
override_dh_installinfo:


ifneq ($(WITH_DKMS),1)
override_dh_gencontrol:
	dh_gencontrol -- -v$(pversion)-$(prel).kver.$(kernelver1)
endif

override_dh_install:
	dh_install
	mv debian/mlnx-ofed-kernel-utils/bin/mlnx_interface_mgr_deb.sh \
	   debian/mlnx-ofed-kernel-utils/bin/mlnx_interface_mgr.sh

override_dh_installdeb:
	dh_installdeb
	# override conf files list
	echo "/etc/infiniband/openib.conf"    >   debian/$(putils)/DEBIAN/conffiles
	echo "/etc/infiniband/mlx5.conf"      >>   debian/$(putils)/DEBIAN/conffiles
	echo "/etc/modprobe.d/mlnx.conf"      >>  debian/$(putils)/DEBIAN/conffiles
	echo "/etc/modprobe.d/mlnx-bf.conf"      >>  debian/$(putils)/DEBIAN/conffiles
	echo "/etc/modprobe.d/ib_ipoib.conf"  >>  debian/$(putils)/DEBIAN/conffiles
ifeq ($(WITH_DKMS),1)
ifeq ($(DKMS3),1)
	# Currently this is a special case. At some point this should become
	# debian/mlnx-ofed-kernel-dkms.triggers and delete it for older
	# versions:
	echo "activate-await update-initramfs" >debian/$(pdkms)/DEBIAN/triggers
endif
else
	sed -i \
		-e 's/@KVER@/$(kernelver)/' \
		-e 's/@ARCH@/$(deb_arch)/' \
		debian/$(pdkms)/DEBIAN/postinst \
		debian/$(pdkms)/DEBIAN/prerm
endif

ifneq ($(MLNX_KO_NO_STRIP),1)
ifneq ($(WITH_DKMS),1)
override_dh_strip:
	dh_strip
	find debian -name '*.ko' | xargs -r strip -g
ifeq ($(WITH_MOD_SIGN),1)
	ofed_scripts/tools/sign-modules $(INSTALL_MOD_PATH)/lib/modules/ $(kernel_source_dir)
endif
endif
endif

override_dh_compress:
	dh_compress -X vf-net-link-name.sh
