#!/bin/sh
#
# Configure 1.62 1998/02/13 23:49:46 (David Hinds)
#
#=======================================================================

# 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 [ ! -L include/linux/config.h ] ; then
    echo "Ack!  The PCMCIA distribution is incomplete/damaged!"
    echo "    Unpack again -- and try using a Linux filesystem this time."
    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!"
	exit 1
    fi
    . ./config.in
fi

CONFIG=config.new
CONFIG_H=include/pcmcia/config.h
CONFIG_MK=config.mk
MODVER=include/linux/modversions.h
rm -f .prereq.ok $CONFIG $CONFIG_H $CONFIG_MK $MODVER

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

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

EOF

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

bool () {
    default=`eval echo '$'$2`
    if [ "$default" = "" ] ; then default=n ; fi
    answer=""
    while [ "$answer" != n -a "$answer" != y ] ; do
	/bin/echo "$1 (y/n) [$default]: \c"
	read answer
	if [ -z "$answer" ] ; then answer="$default" ; fi
    done
    eval "$2=$answer"
    write_bool $2
}

write_value () {
    value=`eval echo '$'$1`
    echo "$1"=\"$value\" >> $CONFIG
    echo "$1=$value" >> $CONFIG_MK
    echo "#define $1 \"$value\"" >> $CONFIG_H
}

prompt () {
    export $2
    default=`eval echo '$'$2`
    /bin/echo "$1 [$default]: \c"
    read answer
    if [ -z "$answer" ] ; then answer="$default" ; fi
    eval "$2"=\"$answer\"
    write_value $2
}

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

echo ""
echo "Linux PCMCIA Configuration Script"
echo ""
echo "The default responses for each question are correct for most users."
echo "Consult the PCMCIA-HOWTO for additional info about each option."
echo ""

prompt "Linux source directory" LINUX

if [ ! -f $LINUX/Makefile ] ; then
    echo "Linux source tree $LINUX does not exist!"
    echo "    See the HOWTO for a list of FTP sites for current" \
	 "kernel sources."
    exit 1
fi

prompt "Alternate target install directory" PREFIX

prompt "C compiler name" CC

prompt "Linker name" LD

prompt "Compiler flags for debugging" PCDEBUG

bool "Build 'trusting' versions of card utilities" UNSAFE_TOOLS

bool "Include experimental 32-bit (CardBus) card support" CONFIG_CARDBUS

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

echo ""
echo "The PCMCIA drivers need to be compiled to match the kernel they"
echo "will be used with, or some or all of the modules may fail to load."
echo "If you are not sure what to do, please consult the PCMCIA-HOWTO."
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"
echo "    3 - Set each option by hand (experts only!)"

src=""
while [ "$src" != 1 -a "$src" != 2 -a "$src" != 3 ] ; do
    /bin/echo "Enter option (1-3) [1]: \c"
    read src
    if [ -z "$src" ] ; then src=1 ; fi
done
echo ""

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

symcheck () {
    if $KSYMS | grep -q "$1" ; then
	eval "$2=y"
    else
	eval "$2=n"
    fi
}

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

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

printflag() {
    value=`eval echo '$'$2`
    /bin/echo "    $1 is \c"
    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 "PCI BIOS support" CONFIG_PCI
    printflag "Advanced Power Management (APM) support" CONFIG_APM
    printflag "SCSI support" CONFIG_SCSI
    printflag "Networking support" CONFIG_INET
    printflag " IPv6 support" CONFIG_IPV6
    printflag " Radio network interface support" CONFIG_NET_RADIO
    printflag " Token Ring device support" CONFIG_TR
    printflag "Module version checking" CONFIG_MODVERSIONS
    printflag "PCMCIA IDE device support" CONFIG_BLK_DEV_IDE_PCMCIA
    printflag "DEC Alpha UDB target platform" CONFIG_ALPHA_LCA
}

case $src in
    1)
	if /sbin/ksyms -a | grep -q printk ; 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 "/* Options from current kernel */" >> $CONFIG_H
	echo "CHECK=\"/proc/version\"" >> $CONFIG
	echo "CKSUM=\"`cksum < /proc/version`\"" >> $CONFIG
	symcheck smp_invalidate_needed CONFIG_SMP
	symcheck pcibios CONFIG_PCI
	symcheck apm_register_callback CONFIG_APM
	symcheck scsi_register CONFIG_SCSI
	modcheck scsi_mod CONFIG_SCSI
	modcheck serial CONFIG_SERIAL
	symcheck register_netdev CONFIG_INET
	cc -o /tmp/has_ipv6 cardmgr/has_ipv6.c
	if /tmp/has_ipv6 ; then
	    CONFIG_IPV6=y
	else
	    CONFIG_IPV6=n
	fi
	rm /tmp/has_ipv6
	symcheck inet_proto_ops CONFIG_IPV6_MODULE
	if [ -r /proc/net/wireless ] ; then
	    CONFIG_NET_RADIO=y
	else
	    CONFIG_NET_RADIO=n
	fi
	symcheck printk_R CONFIG_MODVERSIONS
	symcheck ide_register CONFIG_BLK_DEV_IDE_PCMCIA
	symcheck tr_setup CONFIG_TR
	symcheck hwrpb CONFIG_ALPHA_LCA
	echo "Extracting kernel symbol versions..."
	$KSYMS | sed -ne 's/.* \(.*\)_R\([0-9a-f]*\)/\1 \2/p' | \
	    awk '{ printf "#define %s\t%s_R%s\n", $1, $1, $2 }' \
	    > $MODVER
	printconfig
	;;
    2)
	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" >> $CONFIG
	echo "# Options from $AUTOCONF" >> $CONFIG_MK
	echo "/* Options from $AUTOCONF */" >> $CONFIG_H
	echo "CHECK=\"$AUTOCONF\"" >> $CONFIG
	echo "CKSUM=\"`cksum < $AUTOCONF`\"" >> $CONFIG
	if grep -q "^ *SMP *= *1" $LINUX/Makefile ; then
	    CONFIG_SMP=y
	else
	    configcheck CONFIG_SMP
	fi
	configcheck CONFIG_PCI
	configcheck CONFIG_APM
	configcheck CONFIG_SCSI
	configcheck CONFIG_SERIAL
	configcheck CONFIG_INET
	configcheck CONFIG_IPV6
	configcheck CONFIG_IPV6_MODULE
	configcheck CONFIG_NET_RADIO
	configcheck CONFIG_MODVERSIONS
	configcheck CONFIG_BLK_DEV_IDE_PCMCIA
	configcheck CONFIG_TR
	configcheck CONFIG_ALPHA_LCA
	printconfig
	;;
    3)
	echo "# Configured manually" >> $CONFIG
	echo "# Configured manually" >> $CONFIG_MK
	echo "/* Configured manually */" >> $CONFIG_H
	bool "Symmetric multiprocessing support" CONFIG_SMP
	bool "PCI BIOS support" CONFIG_PCI
	bool "Advanced Power Management (APM) support" CONFIG_APM
	bool "SCSI support" CONFIG_SCSI
	bool "TCP/IP networking" CONFIG_INET
	bool " IPv6 protocol support" CONFIG_IPV6
	bool " Radio network interface support" CONFIG_NET_RADIO
	bool " Token Ring device support" CONFIG_TR
	bool "Set version information for module symbols" CONFIG_MODVERSIONS
	bool "Include PCMCIA IDE device support" CONFIG_BLK_DEV_IDE_PCMCIA
	bool "DEC Alpha UDB target platform" CONFIG_ALPHA_LCA
	;;
esac

if [ "$CONFIG_PCI" != "y" -a "$CONFIG_CARDBUS" = "y" ] ; then
    echo "Cardbus support requires kernel PCI BIOS support!"
    echo "    To fix, re-run 'make config' and disable Cardbus support."
    exit 1
fi

if [ $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."
	exit 1
    fi
else
    MODVER="../$MODVER"
fi

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

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

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

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

# What kernel are we compiling for?

echo ""
VERSION=`sed -n -e 's/^VERSION = \(.*\)/\1/p' $LINUX/Makefile`
PATCHLEVEL=`sed -n -e 's/^PATCHLEVEL = \(.*\)/\1/p' $LINUX/Makefile`
SUBLEVEL=`sed -n -e 's/^SUBLEVEL = \(.*\)/\1/p' $LINUX/Makefile`
SRC_RELEASE=$VERSION.$PATCHLEVEL.$SUBLEVEL
VERSION_CODE=`expr $VERSION \* 65536 + $PATCHLEVEL \* 256 + $SUBLEVEL`
echo "The kernel source tree is version $SRC_RELEASE."
CUR_RELEASE=`uname -r`
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]* //'`
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]* //'`
    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
if [ $src -eq 1 ] ; then UTS_VERSION=`uname -v` ; fi

case "$VERSION.$PATCHLEVEL" in
1.2)
    if [ $SUBLEVEL -lt 8 ] ; then
	echo "Your kernel is out of date.  Upgrade to 1.2.8 or higher."
	exit 1
    fi
    echo "MFLAG=-DCONFIG_MODVERSIONS" >> $CONFIG_MK
    ;;
1.3)
    if [ $SUBLEVEL -lt 30 ] ; then
	echo "Your kernel is out of date.  Upgrade to 1.3.30 or higher."
	exit 1
    fi
    if [ $SUBLEVEL -eq 38 ] ; then
	echo "Kernel version 1.3.38 is broken.  Upgrade to 1.3.39."
	exit 1
    fi
    if [ $SUBLEVEL -lt 38 ] ; then
	echo "MFLAG=-DCONFIG_MODVERSIONS" >> $CONFIG_MK
    fi
    if [ $SUBLEVEL -eq 39 ] ; then
	echo "MFLAG=-DMODVERSIONS" >> $CONFIG_MK
    fi
    if [ $SUBLEVEL -ge 40 ] ; then
	echo "MFLAG=-DMODVERSIONS -include $MODVER" >> $CONFIG_MK
    fi
    if [ $SUBLEVEL -ge 49 -a $SUBLEVEL -lt 57 ] ; then
	echo "AHAFLAG=-DDEBUG_AHA152X" >> $CONFIG_MK
    fi
    ;;
1.99|2.0|2.1)
    echo "MFLAG=-DMODVERSIONS -include $MODVER" >> $CONFIG_MK
    if [ $PATCHLEVEL -ne 99 -o $SUBLEVEL -ge 6 ] ; then
	echo "NEW_QLOGIC=y" >> $CONFIG_MK
	echo "#define NEW_QLOGIC 1" >> $CONFIG_H
    fi
    if [ $PATCHLEVEL -eq 1 -a $SUBLEVEL -ge 8 ] ; then
	CONFIG_BLK_DEV_IDE_PCMCIA=y
    fi
    if [ $PATCHLEVEL -eq 1 ] ; then
	if [ $SUBLEVEL -gt 30 -a $SUBLEVEL -lt 34 ] ; then
	    echo "Kernel versions 2.1.31-33 are broken.  Upgrade to 2.1.34."
	    exit 1
	fi
    fi
    ;;
*)
    echo "This package requires either a 1.2 series kernel 1.2.8 or higher,"
    echo "a 1.3 series kernel 1.3.30 or higher, any 1.99 series kernel, or"
    echo "any 2.0 or 2.1 series kernel."
    exit 1
    ;;
esac

UTS_RELEASE=$SRC_RELEASE
write_value UTS_RELEASE
write_value UTS_VERSION
echo "LINUX_VERSION_CODE=$VERSION_CODE" >> $CONFIG
echo "LINUX_VERSION_CODE=$VERSION_CODE" >> $CONFIG_MK
echo "#define LINUX_VERSION_CODE $VERSION_CODE" >> $CONFIG_H

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

if [ "$CONFIG_BLK_DEV_IDE_PCMCIA" = "y" ] ; then
    echo "DO_IDE=y" >> $CONFIG_MK
fi

if [ "$CONFIG_NET_RADIO" = "y" -a \
      -r ${LINUX}/include/linux/wireless.h ] ; then
    echo "#define HAS_WIRELESS_EXTENSIONS 1" >> $CONFIG_H
else
    echo "#undef  HAS_WIRELESS_EXTENSIONS" >> $CONFIG_H
fi

if [ "$CONFIG_SCSI" = "y" -a "$CONFIG_MODVERSIONS" = "y" ] ; then
    if grep -q "define MODULE" ${LINUX}/drivers/scsi/aha152x.c ; then
	echo "# FIX_AHA152X is not defined" >> $CONFIG_MK
    else
	echo "FIX_AHA152X=y" >> $CONFIG_MK
    fi
fi

if [ "$CONFIG_SCSI" = "y" ] ; then
    if grep -q "define MODULE" ${LINUX}/drivers/scsi/fdomain.c ; then
	echo "DO_FDOMAIN=y" >> $CONFIG_MK
    else
	echo "Future Domain SCSI support requires a 1.3.72 or higher kernel."
	echo "# DO_FDOMAIN is not defined" >> $CONFIG_MK
    fi
fi

#if grep -q PCMCIA ${LINUX}/drivers/cdrom/aztcd.c
#    echo "Your kernel is recent enough for Aztec CD-ROM support."
#    echo "DO_AZTCD=y" >> $CONFIG_MK
#else
#    echo "Aztec CD-ROM support requires a 1.3.72 or higher kernel."
#    echo "# DO_AZTCD is not defined" >> $CONFIG_MK
#fi

if [ "$CONFIG_INET" = "y" ] ; then
    if grep -q PCMCIA ${LINUX}/drivers/net/ibmtr.c ; then
	echo "DO_IBMTR=y" >> $CONFIG_MK
    else
	echo "IBM Token Ring support requires a 1.3.72 or higher kernel."
	echo "# DO_IBMTR is not defined" >> $CONFIG_MK
    fi
fi

#if [ "$CONFIG_SCSI" = "y" ] ; then
#    if grep -q NCR53C400 ${LINUX}/drivers/scsi/g_NCR5380.c ; then
#	echo "Your kernel source tree has NCR 53c400 support."
#	echo "HAS_NCR53C400_SUPPORT=y" >> $CONFIG
#    else
#	echo "Your kernel source tree does not have NCR 53c400 support."
#	echo "    If you plan to use an Adaptec AHA-460 or Trantor T460 SCSI"
#	echo "    adapter, you will need to apply the 53c400 patches."
#	echo "# HAS_NCR53C400_SUPPORT is not defined" >> $CONFIG
#    fi
#    echo ""
#fi

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

# 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

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."
case "$VERSION.$PATCHLEVEL" in
1.[23])
    NEED=1.2.8
    ;;
2.0)
    NEED=2.0.0
    ;;
2.1)
    if [ $SUBLEVEL -lt 18 ] ; then
	NEED=2.0.0
    else
	NEED=2.1.23
    fi
    ;;
esac

MIN=`/bin/echo "$MOD_RELEASE\n$NEED" | sort -t. +0n -1 +1n -2 +2n | head -1`
if [ "$MIN" != "$NEED" ] ; then
    echo "Your module utilities are too old for this kernel!"
    echo "    To fix, upgrade to at least version $NEED."
    exit 1
fi

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

# Is the boot setup OK?

if [ "$PREFIX" = "" ] ; then
    if [ -r /etc/lilo.conf -a -r /boot/map ] ; then
	if [ /vmlinuz -nt /boot/map ] ; then
	    echo "Your boot map file is older than /vmlinuz. "\
		 "If you installed /vmlinuz"
	    echo "by hand, please run 'lilo' to update your boot"\
		 "data, and then reboot."
#	else
#	    echo "Your 'lilo' installation appears to be OK."
	fi
    else
	echo "It doesn't look like you are using 'lilo'."
    fi
fi

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

# How should the startup scripts be configured?

echo ""

if [ "$PREFIX" = "" ] ; then
    if [ -d /etc/rc.d/init.d -o -d /etc/init.d ] ; then
	echo "It looks like you have a System V init file setup."
	SYSV_INIT=y
	if [ -d /etc/rc.d/init.d ] ; then
	    echo "RC_DIR=/etc/rc.d" >> $CONFIG
	    echo "RC_DIR=/etc/rc.d" >> $CONFIG_MK
	else
	    echo "RC_DIR=/etc" >> $CONFIG
	    echo "RC_DIR=/etc" >> $CONFIG_MK
	fi
    else
	echo "It looks like you have a BSD-ish init file setup."
	if ! grep -q rc.pcmcia /etc/rc.d/rc.M ; then
	    echo "    You'll need to edit /etc/rc.d/rc.M to invoke" \
		 "/etc/rc.d/rc.pcmcia"
	    echo "    so that PCMCIA services will start at boot time."
	fi
	SYSV_INIT=
    fi
    write_bool SYSV_INIT
else
    bool "System V init script layout" SYSV_INIT
    if [ "$SYSV_INIT" = "y" ] ; then
	prompt "Top-level directory for RC scripts" RC_DIR
    fi
fi
write_bool INSTALL_DEPMOD

echo ""

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

# Optional stuff

HAS_FORMS=n
if [ -r /usr/include/X11/Xlib.h -o \
     -r /usr/X11R6/include/X11/Xlib.h ] ; then
    echo "X Windows include files found."
    for f in /usr/{X11/,X11R6/,local/,}lib/libforms.{so,a} ; do
	if [ -r $f ] ; then break ; fi
    done
    if [ -r $f ] ; then
	echo "$f found."
	HAS_FORMS=y
    else
	echo "Forms library not installed."
    fi
else
    echo "X Windows include files not installed."
fi

if [ "$HAS_FORMS" != "y" ] ; then
    echo "    If you want to build the 'cardinfo' PCMCIA control" \
         "panel, you need"
    echo "    to install the Forms Library, as well as the X Windows" \
	 "include files."
    echo "    See the HOWTO for details."
    HAS_FORMS=n
fi
write_bool HAS_FORMS

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

if [ ! -d /var/run ] ; then
    echo "WARNING: /var/run not found."
    echo "    To fix, do 'mkdir /var/run'."
fi

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

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

mv $CONFIG config.out

touch .prereq.ok
