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

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)

pname:=srp
psource:=$(pname)-source
ifeq ($(WITH_DKMS),1)
pdkms:=$(pname)-dkms
else
pdkms:=$(pname)-modules
endif

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)


KVER ?= $(shell uname -r)
KVER1 = $(shell echo $(KVER) | sed -e 's/_/-/g')
K_BUILD ?= "/lib/modules/$(KVER)/build"

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

override_dh_auto_clean:

override_dh_auto_configure:

override_dh_auto_build:
ifneq ($(WITH_DKMS),1)
	@echo Building for $(KVER)
	make clean || true
	make -j$(NJOBS) KVER=$(KVER) K_BUILD=$(K_BUILD)
endif

override_dh_auto_test:

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

	# For dkms
ifeq ($(WITH_DKMS),1)
	dh_installdirs -p$(pdkms)  usr/src/$(pname)-$(pversion)
	cp Kconfig debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp makefile debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp Kbuild debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp dkms.conf debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp common.postinst debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp *.c debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp *.h debian/$(pdkms)/usr/src/$(pname)-$(pversion)
	cp -r scsi debian/$(pdkms)/usr/src/$(pname)-$(pversion)

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

override_dh_installinit:


ifneq ($(WITH_DKMS),1)
override_dh_gencontrol:
	dh_gencontrol -- -v$(pversion)-$(prel).kver.$(KVER1)
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)
	tools/sign-modules $(INSTALL_MOD_PATH)/lib/modules/ $(K_BUILD)
endif
endif
endif
