#!/bin/sh
#
# Configure 1.74 1998/07/03 17:47:57
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and
# limitations under the License.
#
# The initial developer of the original code is David A. Hinds
# <dhinds@hyper.stanford.edu>.  Portions created by David A. Hinds
# are Copyright (C) 1998 David A. Hinds.  All Rights Reserved.
#
#=======================================================================

fail ()
{
    echo ""
    echo "Configuration failed."
    echo ""
    exit 1
}

# Minimal requirements for sanity
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
if [ ! -f linux2.4/dir.c ] ; then
    echo "Ack!  The distribution is incomplete/damaged!"
    echo "    Unpack again."
    exit 1
fi

if [ -r config.out ] ; then
    . ./config.out 2>/dev/null
else
    if [ ! -r config.in ] ; then
	echo "config.in does not exist!"
	fail
    fi
    . ./config.in
fi

#=======================================================================

PROMPT=y

arg () {
    VALUE="`echo X"$2" | sed -e 's/^X--[a-zA-Z_]*=//'`"
    eval $1=\"$VALUE\"
}

usage () {
    echo "usage: $0 [-n|--noprompt] [--kernel=src-dir] [--target=dir]"
    echo "    [--moddir=dir] [--cc=path] [--ld=path] [--debug=debug-flags]"
    echo "    [--{no}trust] [--{no}cardbus] [--current] [--srctree] [--manual]"
    exit 1
}

while [ $# -gt 0 ] ; do
    case "$1" in
    -n|--noprompt)	PROMPT=n		;;
    --kernel=*)		arg LINUX $1 		;;
    --target=*)		arg PREFIX $1		;;
    --moddir=*)		arg MODDIR $1		;;
    --cc=*)		arg CC "$1"		;;
    --ld=*)		arg LD "$1"		;;
    --debug=*)		arg PCDEBUG "$1"	;;
    --current)		CONF_SRC=1		;;
    --srctree)		CONF_SRC=2		;;
    --manual)		CONF_SRC=3		;;
    *)			usage			;;
    esac
    shift
done

#=======================================================================

CONFIG=config.new
CONFIG_H=config.h
CONFIG_MK=config.mk
MODVER=modversions.h
rm -f .prereq.ok $CONFIG $CONFIG_H $CONFIG_MK $MODVER
touch $CONFIG_MK

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

write_bool() {
    value=`eval echo '$'$1`
    if [ "$value" = "y" ] ; then
	echo "$1=y" >> $CONFIG
	echo "$1=y" >> $CONFIG_MK
    else
	echo "# $1 is not defined" >> $CONFIG
	echo "# $1 is not defined" >> $CONFIG_MK
    fi
}

write_str () {
    value=`eval echo '$'$1`
    echo "$1"=\"$value\" >> $CONFIG
    echo "$1=$value" >> $CONFIG_MK
}

prompt () {
    eval $3=\"$2\"
    if [ "$PROMPT" = "y" ] ; then
	/bin/echo -n "$1 [$2]: "
	read tmp
	if [ -n "$tmp" ] ; then eval $3=\"$tmp\" ; fi
    else
	/bin/echo "$1 [$2]"
    fi
}

ask_bool () {
    default=`eval echo '$'$2`
    if [ ! "$default" ] ; then default=n ; fi
    answer=""
    while [ "$answer" != "n" -a "$answer" != "y" ] ; do
	prompt "$1 (y/n)" "$default" answer
    done
    eval "$2=$answer"
    write_bool $2
}

ask_str () {
    default=`eval echo '$'$2`
    prompt "$1" "`echo $default`" answer
    eval $2=\"$answer\"
    write_str $2
}

#=======================================================================

echo ""
echo "Linux Coda Configuration Script"
echo ""
echo "The default responses for each question are correct for most users."
echo ""

echo ""
echo "The Coda driver needs to be compiled to match the kernel it"
echo "will be used with, or it may fail to load."
echo ""
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"

ans=""
while [ "$ans" != 1 -a "$ans" != 2  ] ; do
    prompt "Enter option (1-2)" "$CONF_SRC" ans
done
CONF_SRC=$ans
echo "CONF_SRC=$CONF_SRC" >> $CONFIG
echo ""

# What kernel are we compiling for?

version () {
    expr $1 \* 65536 + $2 \* 256 + $3
}

CUR_RELEASE=`uname -r`

if [ $CONF_SRC == 1 ]
then
    echo "The running kernel is version $CUR_RELEASE."
    VERSION=`echo $CUR_RELEASE | sed -e "s/\([0-9]\)\.\([0-9]\)\.\([0-9]*\)\(-[A-Za-z0-9]*\)*/\1/"`
    PATCHLEVEL=`echo $CUR_RELEASE | sed -e "s/\([0-9]\)\.\([0-9]\)\.\([0-9]*\)\(-[A-Za-z0-9]*\)*/\2/"`
    SUBLEVEL=`echo $CUR_RELEASE | sed -e "s/\([0-9]\)\.\([0-9]\)\.\([0-9]*\)\(-[A-Za-z0-9]*\)*/\3/"`
    EXTRAVERSION=`echo $CUR_RELEASE | sed -e "s/\([0-9]\)\.\([0-9]\)\.\([0-9]*\)\(-[A-Za-z0-9]*\)*/\4/"`
    SRC_RELEASE=$CUR_RELEASE
    echo ""
    echo "We need to link against the current kernel headers."
    echo "In some cases things will work when we use the copy of the"
    echo "kernel headers in /usr/include/linux which are used by glibc."
    echo ""
    echo "First try /usr and if the resulting module cannot be"
    echo "inserted with insmod and complains about unresolved symbols"
    echo "then install the headers or source of the running kernel and"
    echo "give the path where they are installed (i.e. /usr/src/linux)"
    echo ""
    ask_str "Linux source directory" LINUX
else
    ask_str "Linux source directory" LINUX

    if [ ! -f $LINUX/Makefile ] ; then
	echo "Linux source tree $LINUX is incomplete or missing!"
	if [ -d $LINUX/include/linux ] ; then
	    echo "    The kernel header files are present, but not " \
		 "the full source code."
	fi
	echo "    See the HOWTO for a list of FTP sites for current" \
	     "kernel sources."
	fail
    fi
    echo ""
    for TAG in VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION ; do
	eval `sed -ne "/^$TAG/s/[ 	]//gp" $LINUX/Makefile`
    done
    SRC_RELEASE=$VERSION.$PATCHLEVEL.$SUBLEVEL$EXTRAVERSION
    echo "The kernel source tree is version $SRC_RELEASE."
    if [ "$SRC_RELEASE" != "$CUR_RELEASE" ] ; then
	echo "WARNING: the current kernel is actually version $CUR_RELEASE."
    fi

    # Check for consistent kernel build dates

    #CUR_D=`uname -v | sed -e 's/^#[0-9]* //;s/SMP //'`
    #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
    #    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 //'`
    #    SRC_D=`echo $SRC_D | sed -e 's/\(:[0-9][0-9]\) .* \([12][90]\)/\1 \2/'`
    #    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
    #echo ""
fi

VERSION_CODE=`version $VERSION $PATCHLEVEL $SUBLEVEL`
if [ $VERSION_CODE -lt `version 2 4 0` ] ; then
    echo "This package requires at least a 2.4 series kernel."
    fail
fi

#=======================================================================

ask_str "Alternate target install directory" PREFIX
if [ ! "$MODDIR" ] ; then
    if [ -d /lib/modules/preferred ] ; then
	MODDIR=/lib/modules/preferred
    else
	MODDIR=/lib/modules/$SRC_RELEASE
    fi
else
    MODDIR=`echo $MODDIR | \
        sed -e "s/[0-9]\.[0-9]\.[0-9]*\(-[A-Za-z0-9]*\)*/$SRC_RELEASE/"`
fi

ask_str "  Module install directory" MODDIR

ARCH=`uname -m | sed -e 's/i.86/i386/'`

#=======================================================================

symcheck () {
    if $KSYMS | grep "$1" >/dev/null ; then
	eval "$2=y"
    else
	eval "$2=n"
    fi
}

modcheck () {
    if [ "$2" = "y" ] ; then return ; fi
    /sbin/insmod $1 > /dev/null 2>&1
    if /sbin/lsmod | grep -q "^$1 " ; then
	eval "$2=y"
	INSTALL_DEPMOD=y
    fi
}

configcheck () {
    if grep -q "^$1=y" $AUTOCONF ; then
	eval "$1=y"
    elif grep -q "^$1=m" $AUTOCONF ; then
	eval "$1=y"
	INSTALL_DEPMOD=y
    else
	eval "$1=n"
    fi
}

printflag() {
    value=`eval echo '$'$2`
    /bin/echo -n "    $1 is "
    if [ "$value" = "y" ] ; then
	echo "enabled."
    else
	echo "disabled."
    fi
    write_bool $2
}


printconfig () {
    echo "Kernel configuration options:"
    printflag "Symmetric multiprocessing support" CONFIG_SMP
    printflag "Module version checking" CONFIG_MODVERSIONS
    printflag "Proc fs support" CONFIG_PROC_FS
}

echo "" >> $CONFIG
echo "" >> $CONFIG_MK

case $CONF_SRC in
    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
	echo "# Options from current kernel" >> $CONFIG
	echo "# Options from current kernel" >> $CONFIG_MK
	echo "CHECK=\"/proc/version\"" >> $CONFIG
	echo "CKSUM=\"`cksum < /proc/version`\"" >> $CONFIG
	symcheck smp_invalidate_needed CONFIG_SMP
	symcheck printk_R CONFIG_MODVERSIONS
	CONFIG_PROC_FS=y
	echo "Extracting kernel symbol versions..."
	echo "#ifndef __LINUX_MODVERSIONS_H" > $MODVER
	echo "#define __LINUX_MODVERSIONS_H" >> $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)
	AUTOCONF=$LINUX/.config
	if [ ! -r $AUTOCONF ] ; then
	    echo "Config file $AUTOCONF not present!"
	    echo "    To fix, run 'make config' in $LINUX."
	    fail
	fi
	echo "# Options from $AUTOCONF" >> $CONFIG
	echo "# Options from $AUTOCONF" >> $CONFIG_MK
	echo "CHECK=\"$AUTOCONF\"" >> $CONFIG
	echo "CKSUM=\"`cksum < $AUTOCONF`\"" >> $CONFIG
	if grep "^ *SMP *= *1" $LINUX/Makefile >/dev/null ; then
	    CONFIG_SMP=y
	else
	    configcheck CONFIG_SMP
	fi
	for C in CONFIG_MODVERSIONS CONFIG_PROC_FS ; do
	    configcheck $C
	done
	printconfig
	;;
    3)
	echo "# Configured manually" >> $CONFIG
	echo "# Configured manually" >> $CONFIG_MK
	echo "/* Configured manually */" >> $CONFIG_H
	ask_bool "Symmetric multiprocessing support" CONFIG_SMP
	ask_bool "Set version information for module symbols" CONFIG_MODVERSIONS
	ask_bool "/proc filesystem support" CONFIG_PROC_FS
	;;
esac


ARCH=`uname -m`
echo "ARCH=$ARCH" >> $CONFIG_MK

if [ $CONF_SRC != "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."
	fail
    fi
else
    MODVER="../$MODVER"
fi


echo "MFLAG=-DMODVERSIONS -include $MODVER" >> $CONFIG_MK

if [ ! -r $LINUX/include/asm ] ; then
    echo "$LINUX/include/asm does not exist!"
    echo "    To fix, do 'ln -s asm-$ARCH asm' in $LINUX/include."
    fail
fi

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

echo "" >> $CONFIG
echo "" >> $CONFIG_MK

#=======================================================================

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

UTS_RELEASE=$SRC_RELEASE
UTS_SHORT=$VERSION.$PATCHLEVEL
write_str UTS_SHORT
write_str UTS_RELEASE
write_str UTS_VERSION
echo "LINUX_VERSION_CODE=$VERSION_CODE" >> $CONFIG
echo "LINUX_VERSION_CODE=$VERSION_CODE" >> $CONFIG_MK

echo "" >> $CONFIG
echo "" >> $CONFIG_MK
echo ""

#=======================================================================

mv $CONFIG config.out

touch .prereq.ok

echo ""
echo "Configuration successful."
echo ""

