#!/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

WITH_DKMS ?= 1
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 dkms --version 2>/dev/null | grep  -c dkms-3)
else
DKMS3 = 0
endif


pname:=mlnx-ofed-kernel
psource:=$(pname)-source
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')

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:
	# 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) $(shell $(CURDIR)/ofed_scripts/dkms_ofed $(kernelver) $(kernel_source_dir) get-config) --with-njobs=$(NJOBS)
endif

mod_dev_dir = /usr/src/ofa_kernel/$(deb_arch)/$(kernelver)
override_dh_auto_build:
ifneq ($(WITH_DKMS),1)
	make distclean || true
	make -j$(NJOBS)
endif
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


ifeq ($(WITH_DKMS),1)
	# For dkms
	dh_installdirs -p$(pdkms)  usr/src/$(pname)-$(pversion)
	cp -a source/compat*		debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp -a source/include		debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp -a source/drivers		debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp -a source/fs			debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp -a source/net		debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp -a source/backports		debian/$(pdkms)/usr/src/$(pname)-$(pversion) || true
	cp source/backports_applied	debian/$(pdkms)/usr/src/$(pname)-$(pversion) || true
	cp -a source/ofed_scripts	debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp -a source/COPYING		debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	$(CURDIR)/ofed_scripts/generate_dkms_conf.sh > debian/dkms.conf
ifeq ($(DKMS3),1)
	# Removed in DKMS 3. Should be removed from the script when we
	# make it the default:
	sed -i -e '/REMAKE_INITRD/d' debian/dkms.conf
endif
	cd debian/$(pdkms)/usr/src/$(pname)-$(pversion); ln -s ofed_scripts/configure
	cd debian/$(pdkms)/usr/src/$(pname)-$(pversion); ln -s ofed_scripts/makefile
	cd debian/$(pdkms)/usr/src/$(pname)-$(pversion); ln -s ofed_scripts/Makefile
	cd debian/$(pdkms)/usr/src; ln -snf $(pname)-$(pversion) ofa_kernel-$(pversion)
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)
	dh_installdirs -p$(pdkms)  usr/src/ofa_kernel/
	# copy the ofa-kernel build headers using ofed_scripts/pre_build.sh in the DKMS MAKE step (called from ofed_scripts/pre_build.sh).
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

	# For utils
	dh_installdirs -p$(putils)  etc/infiniband
	dh_installdirs -p$(putils)  etc/modprobe.d
	dh_installdirs -p$(putils)  sbin
	dh_installdirs -p$(putils)  bin
	dh_installdirs -p$(putils)  usr/bin
	dh_installdirs -p$(putils)  usr/sbin
	dh_installdirs -p$(putils)  lib/udev
	dh_installdirs -p$(putils)  lib/systemd/system
	dh_installdirs -p$(putils)  lib/udev/rules.d
	dh_installdirs -p$(putils)  etc/init.d
	dh_installdirs -p$(putils)  etc/init
	dh_installdirs -p$(putils)  etc/systemd/system
	dh_installdirs -p$(putils)  usr/share/mlnx_ofed

	cp source/ofed_scripts/openib.conf debian/$(putils)/etc/infiniband
	cp source/ofed_scripts/mlx5.conf   debian/$(putils)/etc/infiniband
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

	install -m 0644 source/ofed_scripts/90-ib.rules   debian/$(putils)/lib/udev/rules.d
	install -m 0644 source/ofed_scripts/83-mlnx-sf-name.rules   debian/$(putils)/lib/udev/rules.d
	install -m 0644 ofed_scripts/openibd.service   debian/$(putils)/lib/systemd/system
	install -m 0644 ofed_scripts/mlnx_interface_mgr\@.service  debian/$(putils)/etc/systemd/system

	# Prepare /etc/infiniband/info
	echo '#!/bin/bash'						> debian/$(putils)/etc/infiniband/info
	echo								>> debian/$(putils)/etc/infiniband/info
	echo 'echo prefix=/usr'						>> debian/$(putils)/etc/infiniband/info
	echo 'echo Kernel=`uname -r`'					>> debian/$(putils)/etc/infiniband/info
	echo 'echo'							>> debian/$(putils)/etc/infiniband/info
	echo 'echo Configure options: --all'				>> debian/$(putils)/etc/infiniband/info
	echo 'echo'							>> debian/$(putils)/etc/infiniband/info
	chmod 755 debian/$(putils)/etc/infiniband/info

	install -m 0755 source/ofed_scripts/sf-rep-netdev-rename	debian/$(putils)/lib/udev
	install -m 0755 source/ofed_scripts/auxdev-sf-netdev-rename	debian/$(putils)/lib/udev
	install -m 0755 source/ofed_scripts/net-interfaces		debian/$(putils)/usr/sbin
	install -m 0755 source/ofed_scripts/ibdev2netdev		debian/$(putils)/usr/sbin
	install -m 0755 source/ofed_scripts/mlnx-ofed-kernel-utils.openibd.init	debian/$(putils)/etc/init.d/openibd
	install -m 0644 source/ofed_scripts/mlnx.conf			debian/$(putils)/etc/modprobe.d
	install -m 0644 source/ofed_scripts/mlnx-bf.conf	debian/$(putils)/etc/modprobe.d
	install -m 0644 source/ofed_scripts/ib_ipoib.conf		debian/$(putils)/etc/modprobe.d
	install -m 0755 source/ofed_scripts/mlnx_interface_mgr_deb.sh	debian/$(putils)/bin/mlnx_interface_mgr.sh
	install -m 0755 source/ofed_scripts/mlnx_conf_mgr.sh		debian/$(putils)/bin/
	install -m 0755 source/ofed_scripts/setup_mr_cache.sh		debian/$(putils)/usr/sbin
	install -m 0755 ofed_scripts/mlnx_bf_assign_ct_cores.sh		debian/$(putils)/usr/share/mlnx_ofed
	install -m 0755 ofed_scripts/mlnx_drv_ctl			debian/$(putils)/usr/share/mlnx_ofed
	install -m 0755 ofed_scripts/mod_load_funcs			debian/$(putils)/usr/share/mlnx_ofed

override_dh_installinit:


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

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 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
