#!/bin/bash

# Author: Eric Moore<eric.moore@lsi.com>
# Date: January 8, 2008
# Name: compile
# Description: A script compiling the driver source.


# enable scripting debug
#set -x

prepare_kernel()
{
	source_folder="/lib/modules/${1}/source"
	build_folder="/lib/modules/${1}/build"
	if [ ! -e ${build_folder} ]; then
		return;
	fi;
	if [ `grep -c CONFIG_SCSI_MPT2SAS ${build_folder}/.config` -ne 0 ]; then
		return;
	fi;
	echo -e \\n >> ${build_folder}/.config;
	echo "CONFIG_SCSI_MPT2SAS=m" >> ${build_folder}/.config;
	echo "CONFIG_SCSI_MPT2SAS_MAX_SGE=128"  >> ${build_folder}/.config;
	echo "CONFIG_SCSI_MPT2SAS_LOGGING=y" >> ${build_folder}/.config;

	mkdir -p ${source_folder}/drivers/scsi/mpt2sas
	cp Kconfig ${source_folder}/drivers/scsi/mpt2sas/
	cp Makefile ${source_folder}/drivers/scsi/mpt2sas/

	sed -e "/Kconfig.megaraid/ a\source \"drivers\/scsi\/mpt2sas\/Kconfig\"" \
		${source_folder}/drivers/scsi/Kconfig > \
		${source_folder}/drivers/scsi/Kconfig.append;
	mv ${source_folder}/drivers/scsi/Kconfig.append \
		${source_folder}/drivers/scsi/Kconfig;

	current_path=`pwd`
	cd ${build_folder};
	if [ -f /etc/redhat-release ]; then
		cp include/config/kernel.release include/config/kernel.release.original
	fi;
	make oldconfig > /dev/null
	make prepare > /dev/null
	if [ -f /etc/redhat-release ]; then
		mv include/config/kernel.release.original include/config/kernel.release
	fi;
	cd ${current_path}
}

# Sparse - a semantic parser, provides a set of annotations designed to convey
# semantic information about types, such as what address space pointers point
# to, or what locks a function acquires or releases. You can obtain the latest
# sparse code by typing the following command:
# git clone git://git.kernel.org/pub/scm/devel/sparse/sparse.git

# Set SPARSE to non-zero value to enable sparse checking
#	kernel="2.6.18-8.el5"
#	kernel="2.6.18-53.el5"
#	kernel="2.6.27.15-2-default"
	kernel=`uname -r`
SPARSE=0

rm -fr output.log
./clean
ctags -R *
prepare_kernel $kernel

if [ ${SPARSE} == 0 ] ; then
	make -j4 CONFIG_DEBUG_INFO=1 -C /lib/modules/${kernel}/build \
		M=`pwd` 2>&1 | tee output.log
else
	make C=2 CONFIG_DEBUG_INFO=1 -C /lib/modules/${kernel}/build \
		M=`pwd` 2>&1 | tee output.log
fi;
