#!/bin/sh
#
# Configure 1.42 1996/08/31 15:14:36 (David Hinds)
#
#=======================================================================

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=modules/config.h
CONFIG_MK=config.mk
rm -f .prereq.ok $CONFIG $CONFIG_H $CONFIG_MK

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

function 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
}

function 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
}

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

function 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

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

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

function symcheck () {
    if /sbin/ksyms -a | grep $1 >/dev/null 2>&1 ; then
	eval "$2=y"
    else
	eval "$2=n"
    fi
    write_bool $2
}

function configcheck () {
    if grep "define $1 1" $AUTOCONF >/dev/null 2>&1 ; then
	eval "$1=y"
    else
	eval "$1=n"
    fi
    write_bool $1
}

function printflag() {
    /bin/echo "    $1 is \c"
    if [ "$2" = "y" ] ; then
	echo "enabled."
    else
	echo "disabled."
    fi
}

function printconfig () {
    echo "Kernel configuration options:"
    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 "Module version checking" $CONFIG_MODVERSIONS
    printflag "PCMCIA IDE device support" $CONFIG_BLK_DEV_IDE_PCMCIA
    printflag "Token Ring device support" $CONFIG_TR
    printflag "DEC Alpha UDB target platform" $CONFIG_ALPHA_LCA
}

case $src in
    1)
	echo "# Options from current kernel" >> $CONFIG
	echo "/* Options from current kernel */" >> $CONFIG_H
	symcheck pcibios CONFIG_PCI
	symcheck apm_register_callback CONFIG_APM
	symcheck scsi_register CONFIG_SCSI
	symcheck register_netdev CONFIG_INET
	symcheck Using_Versions CONFIG_MODVERSIONS
	symcheck ide_register CONFIG_BLK_DEV_IDE_PCMCIA
	symcheck tr_setup CONFIG_TR
	symcheck hwrpb CONFIG_ALPHA_LCA
	printconfig
	;;
    2)
	AUTOCONF=$LINUX/include/linux/autoconf.h
	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
	configcheck CONFIG_PCI
	configcheck CONFIG_APM
	configcheck CONFIG_SCSI
	configcheck CONFIG_INET
	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 "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 "Set version information for module symbols" CONFIG_MODVERSIONS
	bool "Include PCMCIA IDE device support" CONFIG_BLK_DEV_IDE_PCMCIA
	bool "Token Ring device support" CONFIG_TR
	bool "DEC Alpha UDB target platform" CONFIG_ALPHA_LCA
	;;
esac

if [ "$CONFIG_MODVERSIONS" = "y" ] ; then
    if [ ! -r $LINUX/include/linux/modversions.h ] ; then
	echo "$LINUX/include/linux/modversions.h does not exist!"
	echo "    To fix, run 'make dep' in $LINUX."
	exit 1
    fi
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

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 running kernel is actually"\
	 "version $CUR_RELEASE. "
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."
	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' \
	    '$(LINUX)/include/linux/modversions.h' >> $CONFIG_MK
    fi
    if [ $SUBLEVEL -ge 49 -a $SUBLEVEL -lt 57 ] ; then
	echo "AHAFLAG=-DDEBUG_AHA152X" >> $CONFIG_MK
    fi
    ;;
1.99|2.0)
    echo 'MFLAG=-DMODVERSIONS -include' \
	'$(LINUX)/include/linux/modversions.h' >> $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
    ;;
*)
    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 series kernel."
    exit 1
    ;;
esac

UTS_RELEASE=$SRC_RELEASE
write_value UTS_RELEASE
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_SCSI" = "y" -a "$CONFIG_MODVERSIONS" = "y" ] ; then
    if grep "define MODULE" ${LINUX}/drivers/scsi/aha152x.c >/dev/null 2>&1
    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 "define MODULE" ${LINUX}/drivers/scsi/fdomain.c >/dev/null 2>&1
    then
	echo "Your kernel is recent enough for Future Domain SCSI support."
	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 PCMCIA ${LINUX}/drivers/cdrom/aztcd.c > /dev/null 2>&1
#    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 PCMCIA ${LINUX}/drivers/net/ibmtr.c > /dev/null 2>&1
    then
	echo "Your kernel is recent enough for the IBM Token Ring driver."
	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 NCR53C400 ${LINUX}/drivers/scsi/g_NCR5380.c >/dev/null 2>&1
#    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

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

# Is the boot setup OK?

if [ "$PREFIX" = "" ] ; then
    if [ -e /etc/lilo.conf -a -e /boot/map ] ; then
	if [ /vmlinuz -nt /boot/map ] ; then
	    echo "Your boot map file is older than /vmlinuz. "\
		 "If you installed"
	    echo "/vmlinuz by hand, please run 'lilo' to update"\
		 "your boot data."
#	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 rc.pcmcia /etc/rc.d/rc.M >/dev/null 2>&1 ; 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

echo ""

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

# Optional stuff

HAS_FORMS=n
if [ -r /usr/include/X11/Xlib.h ] ; then
    echo "X Windows include files found."
    for f in /usr/{X11/,}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

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

# 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

/sbin/insmod 2>&1 | grep 'sym=value' >/dev/null 2>&1
if [ $? -ne 0 ] ; then
    echo "Your module utilities are out of date!"
    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

#echo "Your module utilities appear to be OK."

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

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

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

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

mv $CONFIG config.out

touch .prereq.ok
