#!/bin/sh
#
# Madge Networks Linux Token Ring Driver Configuration Program
#
# Author:   Tom Gallagher   <tgallagh@madge.com>
# Date:     15/02/1999
#
# Large chunks copied verbatim from David Hinds
# pcmcia package.
#
# 
# Copyright (C) 1999 Madge Networks

#/********************************************************************
#*	THE USE BY YOU OF THIS SOFTWARE IS GOVERNED IN ALL
#*	RESPECTS BY THE MADGE NETWORKS LTD OPEN SOURCE LICENSE.		
#*	WE ADVISE YOU TO READ THE TERMS OF THE OPEN SOURCE LICENSE
#*	WHICH CAN BE FOUND WITHIN THE LICENSE.TXT FILE WHICH IS PART OF
#*	THE SOFTWARE OR BY VISITING THE URL
#*	http://www.madge.com/Connect/Downloads/Software/.		
#*	MADGE NETWORKS LTD RESERVES THE RIGHT TO SEEK REDRESS FOR
#*	ANY VIOLATION OF THE LICENSE'S TERMS					
#*********************************************************************/

#LINUX=/wo/auch/immer

#
# This is a shell script containing a default configuration
#
CONFIG_IN=config.in

#
# This is a shell script which set shell variables up for
# the configuration info
#
CONFIG=.config.sh

#
# A temporary file used during this script.
# This file is copied to CONFIG at the end
#
TMPCONFIG=.config.tmp.sh

#
# A C header file containing the configuration information
#
CONFIG_H=include/mtok/config.h

#
# A make include file containing the configuration information
#
CONFIG_MK=.config.mk

#
# Extract the driver version from mtok.upd
#
DRIVER_VERSION=$(sed -n -e 's/#define\( \|\t\)\+MTOK_VERSION\( \|\t\)\+\"\(.*\)\"/\3/p' < mtok.upd)

echo ""
echo "-----------------------------------------------------------------"
echo "| Madge Networks Token Ring Linux Driver Source Installation    |"
echo "| Copyright (c) 1999 Madge Networks                             |"
echo "-----------------------------------------------------------------"
echo ""

#
# If we get our configuration from the running kernel then
# we need to generate the modver.h file ourselves.
# If we get the configuration from a kernel source tree then
# we use the modver.h file in the source tree
#
# TODO: Check out include paths etc.
#
MODVER=include/linux/modversions.h

#---------------------------------------------------------------------

#
# Check for required version of GNU shell utils
#
if /bin/echo --version | grep -q -- -version ; then
    echo "Ack!  Your shell utilities are too old for this script!"
    echo "    Upgrade to GNU shell utilities version 1.16 or later."
    exit 1
fi

#---------------------------------------------------------------------

#
# Check for config.in configuration defaults
#
if [ ! -r $CONFIG_IN ] ; then
    echo "$CONFIG_IN does not exist"
    echo "Distribution corrupt?"
    exit 1
fi


#---------------------------------------------------------------------

#
# Check the machine architecture is Intel x86
#
ARCH=$(uname -m)
if [ $(expr $ARCH : i[3456]86) -eq 4 ] ; then
	ARCH=intel
fi

#---------------------------------------------------------------------

#
# Check gcc is installed and the correct version
#
oldifs="${IFS=   }" ; IFS=":"
tmp="$PATH"
foundgcc=0
for dir in $tmp; do
    test -z "$dir" && dir=.
    if test -f $dir/gcc; then
        foundgcc=1
        break
    fi
done
IFS="$oldifs"
if [ $foundgcc -eq 0 ] ; then
    echo Could not find gcc in the path.
    echo Please install gcc to compile this driver.
    exit 1
fi

#---------------------------------------------------------------------

#
# Load the distribution default values
#
. $CONFIG_IN 

#
# If we have already done a configuration load in the defaults
# from what was configured before.
# (This will overwrite the distribution default values.)

if [ -r $CONFIG ] ; then
    echo ''
    echo Using previous configuration as defaults
    echo ''
    . $CONFIG
fi

#
# Support Olicom cards?
#
# By default Olicom support will not be built but if OLICOM=y
# the choice for Olicom support will appear in advanced driver
# options
#
OLICOM=n

#
# Now check that all the Olicom UDT files are in the udt directory.  If all
# the required files are not there then disallow OLICOM support.
#
# This means OLICOM support in the distribution can be turned on and off by
# just including or excluding UDT files.
#
OLICOM_UDT_FILES="hebei.c heber.c hebe.h at24hebe.c hebeimg.c opcitmsi.c opcitmsr.c"
for i in $OLICOM_UDT_FILES ; do
    if ! [ -f udt/$i ] ; then
        OLICOM=n
        break;
    fi
done


#
# Must delete the old config info after we have loaded it in as
# defaults!!
#
rm -f .configured $CONFIG_H $CONFIG_MK $MODVER $TMPCONFIG

#---------------------------------------------------------------------

echo Madge Linux Token Ring Driver v$DRIVER_VERSION \($ARCH\)
echo ""

#
# Find out what system we are running on.
#
for i in [ SuSE redhat mandrake ] ; do
    if [ -f /etc/$i-release ] ; then
        echo Found a $(sed -n 1p < /etc/$i-release) system.
        echo ""
        case "$i" in
            SuSE) 
                DISTVER=$(sed -ne "s/^VERSION = //gp" /etc/$i-release)
                DIST=suse
            ;;
            
            redhat) 
                DISTVER=$(sed -ne "s/^.*release \(.*\) \(.*\)$/\1/p" /etc/redhat-release)
                DIST=redhat 
            ;;
#
# Mandrake is extremely similar to redhat so make DIST=redhat
#
            mandrake) 
                DISTVER=$(sed -ne "s/^.*release \(.*\) \(.*\)$/\1/p" /etc/mandrake-release)
                DIST=redhat 
            ;;
        esac

#       echo $DIST-$DISTVER

        break;
    fi
done
if [ -z "$DIST" ] ; then
    if [ -f /etc/debian_version ] ; then
        DIST=debian
        DISTVER=$(cat /etc/debian_version)
        echo Found a Debian GNU $DISTVER System.
        echo ""
    fi
fi

#---------------------------------------------------------------------

#
# Create our include subdirs
#

mkdir -p include/mtok
mkdir -p include/linux

#---------------------------------------------------------------------

cat << 'EOF' > $TMPCONFIG
#
# Automatically generated by 'make config' -- don't edit!
#
EOF

cat << 'EOF' > $CONFIG_H
/*
  Automatically generated by 'make config' -- don't edit!
*/
#ifndef __MTOK_CONFIG_H
#define __MTOK_CONFIG_H

#define AUTOCONF_INCLUDED

EOF

#---------------------------------------------------------------------
#
# Load in the shell functions used in this script
#

FUNCS=$(dirname $0)/functions

if [ ! -r $FUNCS ] ; then 
    echo 'Couldnt find file functions'
    echo 'Broken Madge Linux Token Ring Driver distribution?'
    exit 1
fi

. $FUNCS


# The following is the Open Source License information
# The installation will proceed only if the user agrees

LIC_TERMS=n
echo ""
echo "/********************************************************************"
echo "	THE USE BY YOU OF THIS SOFTWARE IS GOVERNED IN ALL"
echo "	RESPECTS BY THE MADGE NETWORKS LTD OPEN SOURCE LICENSE."
echo "	WE ADVISE YOU TO READ THE TERMS OF THE OPEN SOURCE LICENSE"
echo "	WHICH CAN BE FOUND WITHIN THE LICENSE.TXT FILE WHICH IS PART OF"
echo "	THE SOFTWARE OR BY VISITING THE URL"
echo "	http://www.madge.com/Connect/Downloads/Software/."
echo "	MADGE NETWORKS LTD RESERVES THE RIGHT TO SEEK REDRESS FOR"
echo "	ANY VIOLATION OF THE LICENSE'S TERMS"
echo "*********************************************************************/"
echo ""
ask_bool "Have you read the license information [y/n]?" LIC_TERMS

if [ "$LIC_TERMS" = "n" -o "$LIC_TERMS" = "N" ] ; then
echo "This compilation will abort"
echo " Please go through the license information"
exit 1
fi

LIC_TERMS_AGREE=n
echo ""
echo "/********************************************************************"
echo "PLEASE READ THIS LICENSE CAREFULLY BEFORE ACCEPTING."
echo "BY ACCEPTING THE TERMS, OR BY DOWNLOADING,"
echo "INSTALLING, USING, COPYING, MODIFYING OR DISTRIBUTING THE SOFTWARE"
echo "OR DERIVATIVE WORKS, YOU ARE ACCEPTING THE TERMS OF THIS LICENSE."
echo "IF YOU DO NOT AGREE TO THE TERMS OF THIS LICENSE, THE COMPILATION"
echo "WILL BE ABORTED"
echo "*********************************************************************/"
echo ""

ask_bool "Do you agree to the above terms [y/n]?" LIC_TERMS_AGREE

if [ "$LIC_TERMS_AGREE" = "n" -o "$LIC_TERMS_AGREE" = "N" ] ; then
echo "This compilation will abort"
exit 1
fi

#---------------------------------------------------------------------
if [ "$DIST" = "debian" ] ; then
    #
    # Debian stores its kernel source in slightly different place to
    # other distributions.
    #
    LINUX=/usr/src/kernel-source-$(uname -r)
fi

write_str ARCH

######################################################################
#
# Find the Linux Kernel Headers or Source Tree
#
######################################################################

ask_str "Linux source/header prefix" LINUX

if ! [ -f $LINUX/Rules.make ] ; then

    #
    # ONLY FOUND HEADERS
    #

    LINUXINC=$LINUX/include

    #
    # Check for RedHat 6.1 wierdness
    # Setup the VERSION_H variable with the correct filename
    #
    if [ -f /boot/kernel.h ] ; then
        if uname -r | grep -q smp ; then
            VERSION_H=$LINUXINC/linux/version-smp.h
        else
            VERSION_H=$LINUXINC/linux/version-up.h
        fi
    else
        VERSION_H=$LINUXINC/linux/version.h
    fi

    #
    # Check that the version.h header file exists
    #
    if  ! [ -f $VERSION_H ] ; then
        echo "Error: Cannot find Linux kernel headers in $LINUX"
        echo "Please install the package kernel-headers-x.x.x-x.x.i386.rpm"
        echo "or install the complete kernel source kernel-source-x.x.x-x.x.i386.rpm"
        exit 1
    fi

    #
    # Extract the kernel version from the source tree
    #
    SRC_RELEASE=$(grep UTS_RELEASE $VERSION_H | sed -e 's/.*"\(.*\)"/\1/' )
    VERSION_CODE=$(grep VERSION_CODE $VERSION_H )
    VERSION_CODE=$(echo $VERSION_CODE | sed -e 's/#define\( \|\t\)\+LINUX_VERSION_CODE\( \|\t\)\+//' )

    echo "Found headers in $LINUXINC/linux for kernel version $SRC_RELEASE"

    #
    # Check source tree is new enough for us
    #
    if [ $VERSION_CODE -lt $(version 2 0 0) ] ; then
        echo "This package requires at least a 2.0.x series kernel."
        exit 1
    else
        if [ "$ARCH" != "intel" ] ; then
	    if [ $VERSION_CODE -lt $(version 2 1 72) ] ; then
                echo Driver only works on non Intel architectures after Linux version 2.1.72
                exit 1
            fi
        fi
    fi

    #
    # Now find the kernel version of the currently running kernel
    #
    CUR_RELEASE=$(uname -r)

    FORCE_OPTIONS_FROM_CURRENT=0

    if [ "$SRC_RELEASE"smp = "$CUR_RELEASE" ] ; then

        #
        # If we found the kernel headers for 2.2.5-15 for instance and the
        # currently running kernel is actuall 2.2.5-15smp then we force the
        # options from the currently running kernel because the kernel
        # headers are probably configured for a UP kernel.
        #

        SRC_RELEASE=$CUR_RELEASE
        FORCE_OPTIONS_FROM_CURRENT=1

    elif [ "$SRC_RELEASE" != "$CUR_RELEASE" ] ; then
        echo "Warning: the currently running kernel is actually version $CUR_RELEASE."
        echo "The driver you are building might not work on the currently running kernel"
    fi

else
    
    #
    # FOUND COMPLETE SOURCE TREE
    #

    echo ""

    for TAG in VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION ; do
        eval $(sed -ne "/^$TAG/s/ //gp" $LINUX/Makefile)
    done
    if [ ! -z "$EXTRAVERSION" ] ; then
        echo "WARNING: This appears to be a vendor modified kernel"
    fi
    SRC_RELEASE=$VERSION.$PATCHLEVEL.$SUBLEVEL$EXTRAVERSION
    VERSION_CODE=$(version $VERSION $PATCHLEVEL $SUBLEVEL)
    echo "The kernel source tree is version $SRC_RELEASE."

    if [ $VERSION_CODE -lt $(version 2 0 0) ] ; then
        echo "This package requires at least a 2.0.x series kernel."
        exit 1
    else
        if [ "$ARCH" != "intel" ] ; then
	    if [ $VERSION_CODE -lt $(version 2 1 72) ] ; then
                echo Driver only works on non Intel architectures after Linux version 2.1.72
                exit 1
            fi
        fi
    fi

    CUR_RELEASE=$(uname -r)

    FORCE_OPTIONS_FROM_CURRENT=0

    if [ "$SRC_RELEASE"smp = "$CUR_RELEASE" ] ; then

        #
        # If we found the kernel headers for 2.2.5-15 for instance and the
        # currently running kernel is actuall 2.2.5-15smp then we force the
        # options from the currently running kernel because the kernel
        # headers are probably configured for a UP kernel.
        #

        SRC_RELEASE=$CUR_RELEASE
        FORCE_OPTIONS_FROM_CURRENT=1

    elif [ "$SRC_RELEASE" != "$CUR_RELEASE" ] ; then
        echo "Warning: the currently running kernel is actually version $CUR_RELEASE."
        echo "The driver you are building might not work on the currently running kernel"
    fi

    ###########################################################
    # Check for consistent kernel build dates                 #
    ###########################################################
    # Format the currently running kernels UTS_VERSION Nicely #
    ###########################################################
    # uname -v gives a string of this format:-
    # "#buildnum [SMP[-name]] date timezone year"

    # This sed command removes the #buildnum and [SMP[-name]] prefix to the
    # build data

    CUR_D=$(uname -v | sed -e 's/^#[0-9]* //;s/\(SMP-[^[:space:]]* \)\|\(SMP \)//')

    #
    # This sed command removes the time zone stamp from the date
    #

    CUR_D=$(echo $CUR_D | sed -e 's/\(:[0-9][0-9]\) .* \([12][90]\)/\1 \2/')

    echo "The current kernel build date is $CUR_D."

    UTS_VERSION="unknown";
    if [ -f $LINUX/include/linux/compile.h ] ; then

        #
        #   Chop the build date found in the source tree up into
        #   the same format as above.
        #
        UTS_VERSION=`grep UTS_VERSION $LINUX/include/linux/compile.h | sed -e 's/.*"\(.*\)"/\1/'`
        SRC_D=`echo $UTS_VERSION | sed -e 's/^#[0-9]* //;s/\(SMP-[^[:space:]]* \)\|\(SMP \)//'`
        SRC_D=`echo $SRC_D | sed -e 's/\(:[0-9][0-9]\) .* \([12][90]\)/\1 \2/'`

    #
    #   If the release numbers are the same but the build dates are different
    #   then the kernel that is currently running is not the kernel that
    #   has been built most recently from the source tree
    #   If this is the case then we may be configuring to be used on the 
    #   wrong kernel so print a warning message
    #   

        if [ $SRC_RELEASE = $CUR_RELEASE -a "$SRC_D" != "$CUR_D" ] ; then
            echo "WARNING: the source tree has a build date of $SRC_D."
            if [ `date -d "$SRC_D" +%s` -gt `date -d "$CUR_D" +%s` ] ; then
                echo "   Did you forget to install your new kernel?!?"
            fi
        fi
    fi

    LINUXINC=$LINUX/include

    write_str LINUXINC

    echo ""
fi

#---------------------------------------------------------------------
#PCMCIA Patch for RedHat Linux 7.1

ask_bool "Do you want to build the mtok card services helper module (y/n)?" PCMCIA_HELPER

if [ "$PCMCIA_HELPER" = "y" -o "$PCMCIA_HELPER" = "Y" ] ; then
    if [ "$DIST" = "redhat" ] ; then
	if ! grep -q cb_enabler /proc/modules ; then
	    if ! grep -q pcmcia_core /proc/modules ; then
		echo "The PCMCIA services dont appear to be loaded."
		echo "Please check if your computer supports PCMCIA"
		exit 0
	    else
		if ! /sbin/insmod /lib/modules/2.*/pcmcia/cb_enabler.o ; then
		    echo "Cardbus enabler not found. You will not be able"
		    echo "to use Cardbus Adapters."
		fi
	    fi
	fi	
    fi
fi

#---------------------------------------------------------------------


PREFIX=

#MODDIR=/lib/modules/$SRC_RELEASE

echo "Please verify Module install directory before you proceed."
ask_str "Module install directory" MODDIR

if [ "$DIST" = "suse" ] && [ "$DISTVER" = "6.0" ] ; then 
    echo ""
    echo "Detected a SuSE 6.0 system.  Compiler version needs to be 2.7.2.3."
    echo "Please see FAQ for more information."
    echo ""
    CC="gcc -V2.7.2.3"
    ask_str "C compiler name and options" CC
fi

#ask_str "Linker name" LD
#ask_str "Compiler flags for debugging" PCDEBUG

#---------------------------------------------------------------------

if [ "$SRC_RELEASE" != "$CUR_RELEASE" ] ; then
    echo ""
    echo "Must take kernel options from source tree if building driver"
    echo "for a kernel version different from the current running kernel"
    echo ""
    MTOK_OPTIONS_SOURCE=2
else
    if [ ! -f $LINUX/Makefile -o ! -f $LINUX/.config ] || [ $FORCE_OPTIONS_FROM_CURRENT -eq 1 ] ; then
        echo ""
        echo "Couldn't find a configured Linux Source Tree so using"
        echo "options from the currently running kernel."
        echo ""
        MTOK_OPTIONS_SOURCE=1
    else
        echo "How would you like to set kernel-specific options?"
        echo "    1 - Read from the currently running kernel"
        echo "    2 - Read from the Linux source tree"

        /bin/echo -n "Kernel Options [$MTOK_OPTIONS_SOURCE]: "
        read answer
        if [ ! -z "$answer" ] ; then  MTOK_OPTIONS_SOURCE=$answer ; fi
        while [ "$MTOK_OPTIONS_SOURCE" != 1 -a "$MTOK_OPTIONS_SOURCE" != 2 ] ; do
            /bin/echo -n "Kernel Options [$MTOK_OPTIONS_SOURCE]: "
            read answer
            if [ ! -z "$answer" ] ; then  MTOK_OPTIONS_SOURCE=$answer ; fi
        done

        write_num MTOK_OPTIONS_SOURCE
    fi
fi

#---------------------------------------------------------------------

echo "" >> $TMPCONFIG
echo "" >> $CONFIG_MK
echo "" >> $CONFIG_H

case $MTOK_OPTIONS_SOURCE in

#---------------------------------------------------------------------
# Get the options out of the currently running kernel by checking
# for certain kernel symbols either by executing /sbin/ksyms or by 
# reading /proc/ksyms
#
    1)
        if [ -x /sbin/ksyms ] ; then
            KSYMS="/sbin/ksyms -a"
        else
            echo "Hmmm... /sbin/ksyms is broken.  Using /proc/ksyms..."
            KSYMS="cat /proc/ksyms"
        fi
        
        $KSYMS > ksyms.tmp

        echo "# Options from current kernel" >> $TMPCONFIG
        echo "# Options from current kernel" >> $CONFIG_MK
        echo "/* Options from current kernel */" >> $CONFIG_H
        echo "CHECK=\"/proc/version\"" >> $TMPCONFIG
        echo "CKSUM=\"`cksum < /proc/version`\"" >> $TMPCONFIG
        symcheck smp_invalidate_needed CONFIG_SMP
        symcheck pcibios CONFIG_PCI
        symcheck register_netdev CONFIG_INET
        symcheck printk_R CONFIG_MODVERSIONS
        symcheck tr_setup CONFIG_TR

    	if [ $VERSION_CODE -lt $(version 2 4 0) ] ; then
        	symcheck module_symbol CONFIG_MODULES
	else
        	symcheck request_module CONFIG_MODULES
	fi
        symcheck proc_root CONFIG_PROC_FS

        rm ksyms.tmp

        tmp="$($KSYMS | tail -1 | cut -b 1)"
        if [ $(expr $tmp : [cC]) -eq 1 ] ; then
            CONFIG_1GB=y
        elif [ "$tmp" = "8" ] ; then
            CONFIG_2GB=y
        elif [ "$tmp" = "4" ] ; then
            CONFIG_3GB=y
        fi

        echo "Extracting kernel symbol versions..."

        echo "#ifndef __LINUX_MODVERSIONS_H" > $MODVER
        echo "#define __LINUX_MODVERSIONS_H" >> $MODVER

        echo '/* MTOK Automatically generated file' >> $MODVER
        date >> $MODVER
        echo '*/' >> $MODVER

        $KSYMS | sed -ne 's/.* \(.*\)_R\([0-9a-f]*\)/\1 \2/p' | \
            awk '{ printf "#define %s\t%s_R%s\n", $1, $1, $2 }' \
            >> $MODVER

        echo "#endif" >> $MODVER

        printconfig
    ;;
    2)
#---------------------------------------------------------------------
# Get the kernel options from the kernel source tree
#
#
    AUTOCONF=$LINUX/.config
    if [ ! -r $AUTOCONF ] ; then
        echo "Config file $AUTOCONF not present!"
        echo "    To fix, run 'make config' in $LINUX."
        exit 1
    fi
    echo "# Options from $AUTOCONF" >> $TMPCONFIG
    echo "# Options from $AUTOCONF" >> $CONFIG_MK
    echo "/* Options from $AUTOCONF */" >> $CONFIG_H
    echo "CHECK=\"$AUTOCONF\"" >> $TMPCONFIG
    echo "CKSUM=\"`cksum < $AUTOCONF`\"" >> $TMPCONFIG
    if grep -q "^ *SMP *= *1" $LINUX/Makefile ; then
        CONFIG_SMP=y
    else
        configcheck CONFIG_SMP
    fi
    for C in    CONFIG_PCI CONFIG_INET CONFIG_TR CONFIG_MODVERSIONS \
                CONFIG_MODULES \
                CONFIG_PROC_FS CONFIG_1GB CONFIG_2GB CONFIG_3GB ; do
        configcheck $C
    done
    printconfig
    ;;
    3)
#---------------------------------------------------------------------
# Manually set the kernel options (NOT recommended)
#
#
        echo Manual configuration no supported yet
        exit 1
    ;;
esac

NEWNEWT=n
if [ "$DIST" = "redhat" ] && [ "$DISTVER" = "6.1" ] ; then
    NEWNEWT=y
fi

if [ "$DIST" = "redhat" ] && [ "$DISTVER" = "6.2" ] ; then
    NEWNEWT=y
fi

if [ "$DIST" = "redhat" ] && [ "$DISTVER" = "7.1" ] ; then
    NEWNEWT=y
fi

write_bool NEWNEWT

#---------------------------------------------------------------------
# Test for various prerequisites

#CONFIG_MODULES=y
if [ "$CONFIG_MODULES" != "y" ] ; then
    echo "Kernel does not have module support!"
    echo "Please recompile your kernel and include module support"
    exit 1
fi

if [ "$CONFIG_PCI" != "y" ] ; then
    echo "Kernel does not have PCI support!"
    echo "Please recompile your kernel and include PCI support"
    exit 1
fi
    

if [ "$CONFIG_TR" != "y" ] ; then
    echo "Kernel does not have Token Ring Support!"
    echo "Please recompile your kernel and include Token Ring Support"
    exit 1
fi

#
# If we are not getting options from the currently running kernel
# and we are building with versioning information we need to check
# if the versioning information exists in the source tree
#
if [ $MTOK_OPTIONS_SOURCE != "1" -a "$CONFIG_MODVERSIONS" = "y" ] ; then
    MODVER="$LINUX/$MODVER"
    if [ ! -r $MODVER ] ; then
    echo "$MODVER does not exist!"
    echo "    To fix, run 'make dep' in $LINUX."
    exit 1
    fi
fi

echo "MODVER=$MODVER" >> $CONFIG_MK

if [ ! -r $LINUXINC/asm ] ; then
    echo "$LINUXINC/asm does not exist making the link"
    ln -s $LINUXINC/asm-i386 $LINUXINC/asm
fi

if [ "$CONFIG_SMP" = "y" ] ; then
    echo "#define __SMP__ 1" >> $CONFIG_H
fi

echo "" >> $TMPCONFIG
echo "" >> $CONFIG_MK
echo "" >> $CONFIG_H

if [ $MTOK_OPTIONS_SOURCE -eq 1 ] ; then UTS_VERSION=`uname -v` ; fi

#
# check for /proc/bus
# This was only introduced in kernel 2.1.105
#
HAS_PROC_BUS=n
if [ $VERSION_CODE -gt `version 2 1 104` ] ; then
    HAS_PROC_BUS=$CONFIG_PROC_FS
fi

#
# Write the version information to the config files
#

UTS_RELEASE=$SRC_RELEASE
write_str UTS_RELEASE
write_str UTS_VERSION

echo "#ifndef CONFIG_SMP" >> $CONFIG_H
echo "#define UTS_TYPE    \"UP\"" >> $CONFIG_H
echo "#else" >> $CONFIG_H
echo "#define UTS_TYPE    \"SMP\"" >> $CONFIG_H
echo "#endif /* CONFIG_SMP */" >> $CONFIG_H


echo "LINUX_VERSION_CODE=$VERSION_CODE" >> $TMPCONFIG
echo "LINUX_VERSION_CODE=$VERSION_CODE" >> $CONFIG_MK

echo "#ifndef LINUX_VERSION_CODE" >> $CONFIG_H
echo "#define LINUX_VERSION_CODE $VERSION_CODE" >> $CONFIG_H
echo "#endif /* LINUX_VERSION_CODE */" >> $CONFIG_H

echo "" >> $TMPCONFIG
echo "" >> $CONFIG_MK
echo "" >> $CONFIG_H
echo ""

#
# Check out the module stuff
#
if [ ! -x /sbin/insmod -o ! -x /sbin/rmmod ] ; then
    echo "Your module utilities (insmod, rmmod) are missing from /sbin!"
    echo "    To fix, you should build and install the latest set" \
         "of module tools,"
    echo "    available from FTP sites listed in the HOWTO."
    exit 1
fi

#
# Make sure the module utilities are new enough for the kernel
# version.
#
MOD_RELEASE=`/sbin/insmod -V 2>&1 | \
    sed -n -e 's/.*[Vv]ersion \([0-9][0-9.]*\)/\1/p'`
echo "Your module utilities are version $MOD_RELEASE."
#
# Remove anything after the - in the version string
#
MOD_RELEASE=$(echo $MOD_RELEASE | sed -n -e "s/^\(.*\)-\(.*\)\|$/\1/p")
X=$(echo $MOD_RELEASE | sed -e 's/\./ /g')
MOD_CODE=`version $X`
if [ $VERSION_CODE -ge `version 2 1 85` ] ; then
    NEED=`version 2 1 85` ; NEED_TXT=2.1.85
elif [ $VERSION_CODE -ge `version 2 1 18` ] ; then
    NEED=`version 2 1 23` ; NEED_TXT=2.1.23
else
    NEED=`version 2 0 0` ; NEED_TXT=2.0.0
fi

if [ $NEED -gt $MOD_CODE ] ; then
    echo "Your module utilities are too old for this kernel!"
    echo "    To fix, upgrade to at least version $NEED_TXT."
    exit 1
fi

#ask_bool "Do you want to build the mtok card services helper module (y/n)?" PCMCIA_HELPER

if [ "$PCMCIA_HELPER" = "y" -o "$PCMCIA_HELPER" = "Y" ] ; then
    if [ "$DIST" = "suse" ] ; then
        echo ""
        echo "Please make sure you have installed the PCMCIA source package:-"
        echo "rpm --recompile ftp://ftp.suse.com/pub/SuSE-Linux/$DISTVER/suse/zq1/pcmcia.spm"
        echo ""
        PCMCIA=/usr/src/packages/BUILD/pcmcia-cs-*
    fi

    ask_str "pcmcia-cs installation directory" PCMCIA
    if [ ! -f $PCMCIA/include/pcmcia/cs.h ] ; then
        echo "There doesn't appear to be a PCMCIA installation in $PCMCIA"
        echo "The PCMCIA helper module will not be built"
    else
        echo "PCMCIA_MK=1" >> $CONFIG_MK
        CONFIG_MADGE_CARDBUS2=y
    fi
fi

ask_str "Utility Installation Directory " UTIL_DIR

#---------------------------------------------------------------------
# Advanced Option

echo " "
ask_bool "Do you want to set Advanced driver options" CONFIG_MADGE_ADVANCED_OPTS
if [ "$CONFIG_MADGE_ADVANCED_OPTS" = "y" ] || [ "$CONFIG_MADGE_ADVANCED_OPTS" = "Y" ] ; then

    #
    # User wants to set advanced options
    #

    echo ""
    echo "Advanced Driver Options"
    echo "-----------------------"
    echo "The defaults are fine for most people"
    ask_bool "Do you want the driver to respond to Test/XID frames?" CONFIG_MADGE_TEST_XID
   # ask_bool "Do you want a /proc/net/mtok backwards compatible file?" CONFIG_MADGE_BACK_COMPAT
    ask_bool "Do you want a sanity checking driver?" MTOK_SANITY_CHECK
    if [ $VERSION_CODE -lt $(version 2 2 0) ] ; then
	    DEBUG=n
		write_bool DEBUG
	else
	    ask_bool "Debug Driver?" DEBUG
	fi

# MPOKE_DBG_LEVEL

    ask_bool "Terse error messages?" MTOK_TERSE_ERRORS
    ask_bool "Embed the adapter microcode in the driver?" EMBEDDED_MICRO_CODE

    if [ "$EMBEDDED_MICRO_CODE" = "n" -o "$EMBEDDED_MICRO_CODE" = "N" ] ; then

        #
        # Default to the module install path
        #
 
        BIN_FILE_PATH=$MODDIR/net

        #
        # Ask where to put the download file
        #

        ask_str 'Path to micro code binary files' BIN_FILE_PATH

    fi


    echo ""
    echo "Which Adapters?"
    echo "---------------"
    ask_bool 'Madge Smart 16/4 PCI Ringnode Mk3' CONFIG_MADGE_PCI3
    ask_bool 'Madge Presto PCI Adapter' CONFIG_MADGE_PRESTO
    ask_bool 'Madge Presto PCI Plus Adapter' CONFIG_MADGE_PRESTO2
    ask_bool 'Madge Presto PCI 2000 Adapter' CONFIG_MADGE_PRESTO2000
    ask_bool 'Madge Smart 100/16/4 PCI Ringnode (Mk1 & Mk2)' CONFIG_MADGE_PCI4
    ask_bool 'Madge Smart 16/4 PCI Ringnode (3140V2)' CONFIG_MADGE_3140V2

    if [ "$PCMCIA_HELPER" = "y" -o "$PCMCIA_HELPER" = "Y" ] ; then
#       CONFIG_MADGE_PCMCIA=y
        CONFIG_MADGE_CARDBUS2=y
#       ask_bool 'Madge Smart 16/4 PCMCIA Ringnode' CONFIG_MADGE_PCMCIA
        ask_bool 'Madge 16/4 CardBus Adapter Mk2' CONFIG_MADGE_CARDBUS2
    else
        CONFIG_MADGE_PCMCIA=n
        CONFIG_MADGE_CARDBUS2=n
    fi

    if [ "z$OLICOM" = "zy" ] ; then
        ask_bool 'Olicom TI Based Adapters' CONFIG_OLICOM_PCI
        ask_bool 'Olicom RapidFire 3140/3141 Token-Ring 16/4 PCI Adapter' CONFIG_OLICOM_314X
        ask_bool 'Olicom RapidFire 3139 Token-Ring 16/4 PCI Adapter' CONFIG_OLICOM_3139
        ask_bool 'Olicom RapidFire 3540 HSTR 100/16/4 PCI Adapter' CONFIG_OLICOM_3540
        if [ "$PCMCIA_HELPER" = "y" -o "$PCMCIA_HELPER" = "Y" ] ; then
            CONFIG_OLICOM_PCMCIA=y
            CONFIG_OLICOM_CARDBUS=y
            ask_bool 'Olicom PCMCIA adapters' CONFIG_OLICOM_PCMCIA
            ask_bool 'Olicom CardBus adapters' CONFIG_OLICOM_CARDBUS
        else
            CONFIG_OLICOM_PCMCIA=n
            CONFIG_OLICOM_CARDBUS=n
        fi
    fi

else
    #
    # The User elected not to set advanced driver options so just write
    # out all the default values to the driver config file.
    #

    #
    # Write out all the default driver configure options
    #

    write_bool CONFIG_MADGE_ADVANCED_OPTS
    write_bool CONFIG_MADGE_TEST_XID
    write_bool CONFIG_MADGE_BACK_COMPAT
    write_bool MTOK_SANITY_CHECK
	write_bool DEBUG
    write_bool MTOK_TERSE_ERRORS
    write_bool EMBEDDED_MICRO_CODE

    #
    # Write out all the default MADGE options
    #

    write_bool CONFIG_MADGE_TR
    write_bool CONFIG_MADGE_PCI3
    write_bool CONFIG_MADGE_PRESTO
    write_bool CONFIG_MADGE_PRESTO2
    write_bool CONFIG_MADGE_PRESTO2000
    write_bool CONFIG_MADGE_PCI4
#    write_bool CONFIG_MADGE_PCMCIA
    write_bool CONFIG_MADGE_CARDBUS2
    write_bool CONFIG_MADGE_3140V2

    #
    # Write out all the default OLICOM options
    #

    if [ "z$OLICOM" = "zy" ] ; then
	    write_bool CONFIG_OLICOM_314X
	    write_bool CONFIG_OLICOM_3139
   	 	write_bool CONFIG_OLICOM_3540
    	write_bool CONFIG_OLICOM_PCMCIA
    	write_bool CONFIG_OLICOM_CARDBUS
	fi
fi

cat << 'EOF'

 The driver is successfully configured.

-----------------------------------------------------------------

 Now run

    make

 and then 

    make install

 to install the driver in the modules directory

EOF
    
#---------------------------------------------------------------------

echo "" >> $CONFIG_H
echo "#endif /* __MTOK_CONFIG_H */" >> $CONFIG_H

mv -f $TMPCONFIG $CONFIG

touch .configured
