#!/bin/sh
#
# Creates a dkms.conf file

PKG_NAME="intel-dmabuf-dkms-prerelease"
PKG_VER="1.0"
REL_NAME="1"
REL_TYPE=""
BUILD_TYPE=""

helpFunction()
{
	echo ""
	echo "Usage: $0 -n packagename -v packageversion -r releaseversion -p prerelease/opensource -s buildversion"
	echo -e "\t-n packagename"
	echo -e "\t-v packageversion"
	echo -e "\t-r releaseversion"
	echo -e "\t-p Release type  prerelease/opensource"
	echo -e "\t-s buildversion"
	exit 1
}

while getopts "n:v:r:p:s:" opt
do
	case "$opt" in
		n ) PKG_NAME="$OPTARG" ;;
		v ) PKG_VER="$OPTARG" ;;
		r ) REL_NAME="$OPTARG" ;;
		p ) REL_TYPE="$OPTARG" ;;
		s ) BUILD_TYPE="$OPTARG" ;;
		? ) helpFunction ;; # Print helpFunction
	esac
done

# We can label the here-doc lines for conditional output to the conf file
#
# Labels:
#  $ST: this line is added only when selftest is enabled
#
sed -e '/^DEL/d' -e 's/^\t*//' <<EOF
	PACKAGE_NAME="$PKG_NAME"
	PACKAGE_VERSION="$PKG_VER-$REL_NAME"
	AUTOINSTALL="yes"
	BUILD_EXCLUSIVE_KERNEL="^(5\.(10|4)\.*)"

	BUILT_MODULE_NAME[0]="dmabuf"
	BUILT_MODULE_LOCATION[0]="drivers/dma-buf/"
	DEST_MODULE_LOCATION[0]="/updates"

	# Find out how many CPU cores can be use if we pass appropriate -j option to make.
	# DKMS could use all cores on multicore systems to build the kernel module.
	num_cpu_cores()
	{
		if [ -x /usr/bin/nproc ]; then
		np=`nproc`
			if [ "\$np" -le "64" ]; then
				echo "\$np"
			else
				echo "64"
			fi
		else
			echo "1"
		fi
	}

	MAKE="export LEX=flex; export YACC=bison; cp defconfigs/dmabuf .config; 'make' -j\$(num_cpu_cores) BUILD_CONFIG=$BUILD_TYPE KLIB=/lib/modules/\$kernelver olddefconfig; 'make' -j\$(num_cpu_cores) KLIB=/lib/modules/\$kernelver BUILD_CONFIG=$BUILD_TYPE; mkdir -p \$dkms_tree/$PKG_NAME/$PKG_VER/\$kernelver/x86_64/build/;  cp Module.symvers \$dkms_tree/$PKG_NAME/$PKG_VER/\$kernelver/x86_64/build/"
	CLEAN="export LEX=flex; export YACC=bison; 'make' mrproper"
EOF
