#!/usr/bin/bash
#
# Copyright (c) 2013 Mellanox Technologies. All rights reserved.
#
# This Software is licensed under one of the following licenses:
#
# 1) under the terms of the "Common Public License 1.0" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/cpl.php.
#
# 2) under the terms of the "The BSD License" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/bsd-license.php.
#
# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
#    copy of which is available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/gpl-license.php.
#
# Licensee has the right to choose one of the above licenses.
#
# Redistributions of source code must retain the above copyright
# notice and one of the license notices.
#
# Redistributions in binary form must reproduce both the above copyright
# notice, one of the license notices in the documentation
# and/or other materials provided with the distribution.
#
#

WDIR=${0%*/*}

usage()
{
cat << EOF

Usage: `basename $0` <kernel version> [options]

	Options:
		get-config:	Get configuration parameters for configure script
EOF
}


get-config()
{
def_configure_options=" \
	--with-core-mod \
	--with-user_mad-mod \
	--with-user_access-mod \
	--with-addr_trans-mod \
	--with-mlx5-mod \
	--with-mlxfw-mod \
	--with-ipoib-mod \
	"
# check if dkms.conf exists and parse it, otherwise use the env variable or the default
if [ -f $WDIR/../dkms.conf ];then
	options=
	while read line; do
		if [[ $line =~ BUILT_MODULE_NAME ]];then
			name=$(echo $line | sed -e 's/BUILT_MODULE_NAME\[[0-9$i]*\]=//g')
			flag=`module_to_flag $name`
			dashFlag=$(echo $flag | sed -e 's/-/\\\-/g')
			if ! (echo $options 2> /dev/null | grep "$dashFlag" >/dev/null 2>&1); then
				options="${options} $flag"
			fi
		fi
	done < $WDIR/../dkms.conf
	extra_options=`awk '/^#:# ExtraOption/ {print $3}' $WDIR/../dkms.conf`
	options="${options} ${extra_options}"
	configure_options=$options
else
	configure_options=${configure_options:-"$def_configure_options"}
fi

echo $configure_options
}

module_to_flag()
{
	module=$1
	shift

	flag=

	case "$module" in
		mlx_compat|compat|ib_core|ib_cm|iw_cm)
		flag=--with-core-mod
		;;
		ib_umad)
		flag=--with-user_mad-mod
		;;
		ib_uverbs)
		flag=--with-user_access-mod
		;;
		rdma_cm|rdma_ucm)
		flag=--with-addr_trans-mod
		;;
		mlx5_core|mlx5_ib)
		flag=--with-mlx5-mod
		;;
		ib_ipoib)
		flag=--with-ipoib-mod
		;;
		memtrack)
		flag=--with-memtrack
		;;
		ib_mad|ib_sa|ib_addr)
		flag=--with-dummy-core-mods
		;;
		mlxfw)
		flag=--with-mlxfw-mod
		;;
		mlxdevm)
		flag=--with-mlxdevm-mod
		;;
		*)
		;;
	esac

	echo $flag
}

kernelver=$1; shift
kernel_source_dir=$1; shift

case "$1" in
	get-config)
	get-config
	;;
	-h|--help)
	usage
	exit 0
	;;
	*)
	usage
	exit 1
	;;
esac
