
#!/bin/sh
#Copyright (C) 2004  Kenney He
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#
# Parse the arguments that were passed into the script
#
#
#
# Configure the Adaptec iSCSI driver.
#
# type:
#	configure --help
#
# for a list of options
#

parse_args()
{
	num_args=$#

	while [ $num_args -ne 0 ]
	do
		case $1 in
			--version=*)
				LINUX_VERSION=`echo $1 | sed 's/--version=//'`
				;;

			--smp_enabled=*)
				SMP_ENABLED=`echo $1 | sed 's/--smp_enabled=//'`
				;;

			--module_versions=*)
				MODULE_VERSIONS=`echo $1 | sed 's/--module_versions=//'`
				;;

			--tree=*)
				SOURCE_TREE=`echo $1 | sed 's/--tree=//'`
				;;

			--use_booted_kernel=*)
				USE_BOOTED_KERNEL=`echo $1 | sed 's/--use_booted_kernel=//'`
				;;

			--help)
				echo "configure [options]"
				echo "   --version=<linux kernel version> 2.4.2"
				echo "   --smp_enabled=y|n"
				echo "   --module_versions=y|n"
				echo "   --tree=<path to source tree>"
				echo "   --use_booted_kernel=y|n"
				exit 0
				;;

			*)
				echo "Unknown flag $1"
				exit 0
				;;
		esac

		shift

		num_args=$#
	done
}

#
# Test /proc/ksyms to see if we have module versioning turned on, or
# if we have SMP turned on.
#
check_boot_mod_versions()
{
	if [ ! -r /proc/ksyms ]
	then
		echo -n "... Can't determine mod versions from /proc/ksyms"
		MODULE_VERSIONS=""
		return
	fi

	printk_value=`awk '{ print $2 }' /proc/ksyms | grep "^printk"`

	if [ "$printk_value" = "printk" ]
	then
		echo -n ... module versions turned OFF in booted kernel
		MODULE_VERSIONS="n"
	else
		echo -n ... module versions turned ON in booted kernel
		MODULE_VERSIONS="y"
	fi
}

#
# Check to see if the booted kernel has SMP compiled in
#
check_boot_smp()
{
	if [ ! -r /proc/ksyms ]
	then
		echo -n "... Can't determine mod versions from /proc/ksyms"
		SMP_ENABLED=""
		return
	fi

	if [ `echo "$printk_value" | grep "printk_Rsmp.*"` ]
	then
		echo -n ... booted kernel has SMP enabled, so
		SMP_ENABLED="y"
	else
		echo -n ... booted kernel is uniprocessor, so
		SMP_ENABLED="n"
	fi
}

decipher_configuration()
{
	if [ "$LINUX_VERSION" = "" ]
	then
		LINUX_VERSION=`grep "^VERSION = " $SOURCE_TREE/Makefile | sed 's/VERSION = //'`
		LINUX_VERSION=$LINUX_VERSION.`grep "^PATCHLEVEL = " $SOURCE_TREE/Makefile | sed 's/PATCHLEVEL = //'`
	
		LINUX_VERSION=$LINUX_VERSION.`grep "^SUBLEVEL = " $SOURCE_TREE/Makefile | sed 's/SUBLEVEL = //'`
		
	fi
}

#
# Check to see how the source tree is configured, and use that
#
check_source_tree()
{
	if [ ! -d "$SOURCE_TREE" ]
	then
		echo ""
		echo "$SOURCE_TREE directory doesn't exist !!"
		echo ""
		echo "Kernel source is not located in $SOURCE_TREE"
		echo "Please run the configure script again with option --tree"
		echo "and specify the location of the kernel source"
		echo ""
        echo "For example, the following line works for Red Hat Linux 7.2:"
        echo "     # ./configure --tree=/usr/src/linux-2.4.7-10 "
		echo ""
		exit  0
	fi

	if [ ! -f "$SOURCE_TREE/include/linux/autoconf.h" ]
	then
		echo ""
		echo "no autoconf.h file in $SOURCE_TREE/include/linux !!"
		echo ""
		return
	fi

	if [ ! -f "$SOURCE_TREE/Makefile" ]
	then
		echo "no Makefile file in $SOURCE_TREE !!"
		return
	fi

	# 
	# Check if modversion.h is available.
	# If not, disable modversions by force.
	#
	if [ ! -f "$SOURCE_TREE/include/linux/modversions.h" ]
	then
		echo ""
		echo "no modversions.h file in $SOURCE_TREE/include/linux !!"
		echo ""
		MODULE_VERSIONS=n
	fi

	echo ""
	echo "... using source tree $SOURCE_TREE"

	if [ "$LINUX_VERSION" = "" ]
	then
		LINUX_VERSION=`grep "^VERSION = " $SOURCE_TREE/Makefile | 
                               sed 's/VERSION = //'`
		LINUX_VERSION=$LINUX_VERSION.`grep "^PATCHLEVEL = " 
                              $SOURCE_TREE/Makefile | sed 's/PATCHLEVEL = //'`
		LINUX_VERSION=$LINUX_VERSION.`grep "^SUBLEVEL = " 
                              $SOURCE_TREE/Makefile | sed 's/SUBLEVEL = //'`
		
	fi

	if [ "$MODULE_VERSIONS" = "" ]
	then
		MODULE_VERSIONS=`grep 'define CONFIG_MODVERSIONS' $SOURCE_TREE/include/linux/autoconf.h | wc -l | sed 's/ //g'`
		if  [ "$MODULE_VERSIONS" = 1 ]
		then	
			MODULE_VERSIONS=y
		else
			MODULE_VERSIONS=n
		fi
	fi

	if [ "$SMP_ENABLED" = "" ]
	then
		SMP_ENABLED=`grep 'define CONFIG_SMP' $SOURCE_TREE/include/linux/autoconf.h | wc -l | sed 's/ //g'`
		if  [ "$SMP_ENABLED" = 1 ]
		then	
			SMP_ENABLED=y
		else
			SMP_ENABLED=n
		fi
	fi
}

#
# Create the flags that will be used in Make.inc
#
check_release()
{
	#
	# Based on the module versions option, set the module versions flag
	#
	case $MODULE_VERSIONS in
		y)
			echo ... Module versions enabled
			MODVERSIONS="-DMODVERSIONS"
			INCLUDE_MOD="-include ${SOURCE_TREE}/include/linux/modversions.h"
			;;
		n)
			echo ... Module versions disabled
			MODVERSIONS=""
			INCLUDE_MOD=""
			;;
	esac

	#
	# Based on the kernel type, set the SMP_FLAGS
	#
	case $SMP_ENABLED in
		y)
			echo ... SMP enabled
			SMP_FLAGS="-D__SMP__ -DCONFIG_SMP"
			;;
		n)
			echo ... SMP disabled
			SMP_FLAGS=""
			;;
	esac

	#
	# Based on the Linux version compute the rest of the flags
	#
	case $LINUX_VERSION in
		2.2.*)
			echo "... 2.2 kernel"
			ENUM_VALUE="-DFIBRE_PROC_ENUM=PROC_SCSI_AFC9XXX"
			ARCH="-fno-strength-reduce -m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2 -DCPU=686"
			KERNEL_VER="2.2"
			;;
		2.4.*)
			echo "... 2.4 kernel"
			ENUM_VALUE=""
			ARCH=""
			KERNEL_VER="2.4"

			case $SMP_ENABLED in
				y)
					STACK_BOUNDARY="-mpreferred-stack-boundary=2"
					ARCH="-march=i686"
					;;
				n)
					ARCH="-march=i686"
					;;
			esac
			;;
		*)
			echo "... Can't determine boot version"
			echo "... Make.inc not created"
			exit 0
			;;
	esac

        if [ "$KERNEL_VER" = "2.2" ]
        then
	   echo ... inserting PROC_SCSI_ASA72XX into proc_fs.h  
	   insert_proc_fs
        fi

	echo "... configuring for architecture $BOOTARCH"

	case $LINUX_VERSION in

		2.4.7-10)
			echo "... Probably Red Hat 7.2"
			;;

		2.4.7-10smp)
			echo "... Probably Red Hat 7.2 SMP"
			;;

		2.4.7-10*)
			echo "... Probably Red Hat 7.2 (unknown)"
			;;

		2.4.20-64GB-SMP)
			echo "... Probably SuSe 8.2 SMP"
		        SMP_ENABLED="y"
			USE_SOURCE_TREE_NEW="y"
			;;

		2.4.20-4GB-SMP)
			echo "... Probably SuSe 8.2 SMP"
		        SMP_ENABLED="y"
			USE_SOURCE_TREE_NEW="y"
			;;

		2.4.19-64GB-SMP)
			echo "... Probably SLES 8.0 SMP"
		        SMP_ENABLED="y"
			USE_SOURCE_TREE_NEW="y"
			;;

		2.4.19-4GB-SMP)
			echo "... Probably SLES 8.0 SMP"
		        SMP_ENABLED="y"
			USE_SOURCE_TREE_NEW="y"
			;;

		2.4.4-64GB-SMP)
			echo "... Probably SuSe 7.2 SMP"
			USE_SOURCE_TREE_NEW="y"
			;;

		2.4.4-64GB)
			echo "... Probably SuSe 7.2"
			USE_SOURCE_TREE_NEW="y"
			;;

		2.4.2-2)
			echo "... Probably Red Hat 7.1"
			;;

		2.4.2-2smp)
			echo "... Probably Red Hat 7.1 SMP"
			;;

		2.4.2-2enterprise)
			echo "... Probably Red Hat 7.1 Enterprise"
			;;

		2.2.19pre17)
			echo "... Probably Debian 2.2r3 UP"
			;;

		2.2.19pre17smp)
			echo "... Probably Debian 2.2r3 SMP"
			;;

		2.2.18-2)
			echo "... Probably Turbo Linux 6.5 Server UP"
			;;

		2.2.18-2smp)
			echo "... Probably Turbo Linux 6.5 Server SMP"
			;;

		2.2.16)
			echo "... Probably SuSe 7 UP"
			;;

		2.2.16smp)
			echo "... Probably SuSe 7 SMP"
			;;

		2.2.16-22)
			echo "... Probably Red Hat 7 Up"
			CC=kgcc
			;;

		2.2.16-22smp)
			echo "... Probably Red Hat 7 SMP"
			CC=kgcc
			;;
		*)
			echo "... Not sure what distribution ... guessing"
			CC=gcc
			;;
	esac
}


#
# Compute LINUX_VERSION_CODE
#
calculate_ver()
{
	VERSION=`grep "^VERSION = " $SOURCE_TREE/Makefile | sed '\
                 s/VERSION = //'`

	PATCH_LEVEL=`grep "^PATCHLEVEL = " $SOURCE_TREE/Makefile | sed '\
                 s/PATCHLEVEL = //'`
	
	SUBLEVEL=`grep "^SUBLEVEL = " $SOURCE_TREE/Makefile | sed '\
                 s/SUBLEVEL = //'`

	LINUX_VER_CODE=`expr $VERSION \* 65536 + $PATCH_LEVEL \* 256 + \
                 $SUBLEVEL`
}

insert_proc_fs()
{
  #
  # Don't put the line in more than once
  #
  grep PROC_SCSI_ASA72XX $PROC_FS_H > /dev/null 2>&1

  if [ $? -eq 0 ]
  then
	return
  fi

  #
  # Insert ourselves into the proc_fs.h file
  #

  if [ ! -f $PROC_FS_H.asa72xx ]
  then
	cp $PROC_FS_H $PROC_FS_H.asa72xx
  fi


  ed $PROC_FS_H > /dev/null 2>&1 << EOF
g/PROC_SCSI_SEAGATE,$/
i
PROC_SCSI_ASA72XX,
.
w
EOF
}

#
# Inform the user that scsi_mod and sd_mod are not loaded by default
# and is not found in /proc/ksyms
#
warning()
{
   FOUND_SCSI_MOD='grep -i scsi_mod /proc/ksyms'
   FOUND_SD_MOD='grep -i sd_mod /proc/ksyms'

   if [ "$FOUND_SCSI_MOD" = "" ]
   then
      $COLOR_WARN
      echo scsi_mod.o is need to run asa72xx.o
      $COLOR_NORMAL
   #else
      #ok
   fi

   if [ "$FOUND_SD_MOD" = "" ]
   then
      $COLOR_WARN
      echo sd_mod.o is need to run asa72xx.o
      $COLOR_NORMAL
   #else
      #ok
   fi
   
}

#
# ----------------------------------------------------------------------------
# main()
# ----------------------------------------------------------------------------
#
CC=gcc
STRICT_PROTOTYPES=""
SMP_FLAGS=""
ENUM_VALUE=""
STACK_BOUNDARY=""
ARCH=""
SOURCE_TREE="/usr/src/linux"
SOURCE_TREE_NEW="/lib/modules/`uname -r`"
USE_BOOTED_KERNEL="n"
USE_SOURCE_TREE_NEW="n"
BOOTUP_VER=`uname -r`

#
# Debug flags for oslayer.
#
DEBUG_FLAGS=
COLOR_OK="echo -en \\033[1;32m"
COLOR_WARN="echo -en \\033[1;31m"
COLOR_NORMAL="echo -en \\033[0;39m"
BOOTVER=`uname -r`
BOOTARCH=`uname -m`

LINUX_VERSION=$BOOTVER
 
#
# Read in the arguments that have been passed to this program
# These arguments will override anything that is discovered from
# the booted kernel, or from a source tree
#
parse_args $*

#
# If there has been a source tree passed into this program, read
# in the parameters from the source tree.  These parameters will
# override anything that is discovered from the booted kernel
#

if [ "$USE_BOOTED_KERNEL" != "y" ]
then
#        echo -n "checking path passed in .... "
#        echo "configure will attempt to get it from /proc/ksyms"
#else
	check_source_tree
fi

#
# If we haven't figured out module versions yet, get the answer from
# the booted kernel
#
echo -n "checking module version ..."
if [ "$MODULE_VERSIONS" = "" ]
then
	check_boot_mod_versions
        echo -n " [" 
        $COLOR_OK
        echo -n $MODULE_VERSIONS 
        $COLOR_NORMAL
        echo ]
else
        echo -n " [" 
        $COLOR_OK
        echo -n $MODULE_VERSIONS 
        $COLOR_NORMAL
        echo ]
fi

echo -n "checking Linux version  ..."
if [ "$LINUX_VERSION" = "" ]
then
        #do something here
        echo -n " is ["
        $COLOR_OK
        echo -n $LINUX_VERSION 
        $COLOR_NORMAL
        echo ]
else
        echo -n " is ["
        $COLOR_OK
        echo -n $LINUX_VERSION 
        $COLOR_NORMAL
        echo ]
fi

#
# warn user of module dependency
#
warning

#
# Compute the answers from what we have discovered
#
check_release

#
# If we haven't figured out whether SMP is enabled, figure that out
# from the booted kernel
#
echo -n "... checking smp  ..."
if [ "$SMP_ENABLED" = "" ]
then
	check_boot_smp
        echo -n " [" 
        $COLOR_OK
        echo -n $SMP_ENABLED 
        $COLOR_NORMAL
        echo ]
else
        #already configured
        echo -n " [" 
        $COLOR_OK
        echo -n $SMP_ENABLED 
        $COLOR_NORMAL
        echo ]
fi

# Insert PROC_SCSI_ASA72XX marker to proc_fs.h (only for 2.2 kernel)
#

PROC_FS_H=${SOURCE_TREE}/include/linux/proc_fs.h

if [ "$USE_SOURCE_TREE_NEW" = "n" ]
then
    INC="-I${SOURCE_TREE}/drivers/scsi -I${SOURCE_TREE}/include"
else
    INC="-I${SOURCE_TREE_NEW}/build/drivers/scsi -I${SOURCE_TREE_NEW}/build/include"
fi

ASA72XX_FLAGS="-DARIES -UASA72XX_VARYIO_SHT -UASA72XX_VARYIO_SHT_EXT $INC $BOOTMODVERIONS"

DEBUG_FLAGS="-UASA72XX_DBG_SERIAL -UASA72XX_DEBUG -UASA72XX_DBG_FUNC"

CFLAGS="$SMP_FLAGS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strict-aliasing -fno-common -pipe $MODVERSIONS $INCLUDE_MOD $STACK_BOUNDARY $ARCH $DEBUG_FLAGS $ASA72XX_FLAGS $ENUM_VALUE"

echo ... creating Make.inc

#
# Remove the Make.inc to start from scratch
#
if [ -f Make.inc ]
then
	rm -f Make.inc
fi

echo "CFLAGS := $CFLAGS" >> Make.inc
echo >> Make.inc
echo "CC := $CC" >> Make.inc
echo >> Make.inc
echo "KERNEL_VER := $KERNEL_VER" >> Make.inc


echo ... removing version.h

if [ -f version.h ]
then
	rm -f version.h
fi

if [ -d $SOURCE_TREE ]
then
        calculate_ver
fi

if [ -d $SOURCE_TREE ]
then
	if [ ! -f $SOURCE_TREE/include/linux/version.h ]
	then
		cp -f $SOURCE_TREE/include/linux/version.h version.h
	else
                echo "#define UTS_RELEASE \"$BOOTUP_VER\"" >> version.h
                echo "#define LINUX_VERSION_CODE $LINUX_VER_CODE" >> version.h 
                echo "#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))" >> version.h

	fi
fi

/usr/bin/perl ./modifypciids
echo "IB:1" > ./asa72xx.conf
echo "DeviceQueueDepth:128" >> ./asa72xx.conf
#maximum supported adapters is 4
mknod -m 0660 /dev/asa72xx0 c 151 0 2> /dev/null
mknod -m 0660 /dev/asa72xx1 c 151 1 2> /dev/null
mknod -m 0660 /dev/asa72xx2 c 151 2 2> /dev/null
mknod -m 0660 /dev/asa72xx3 c 151 3 2> /dev/null
echo ... finished
echo
exit 0
