#!/bin/sh
#
# Configure 1.5 1995/09/29 06:44:16 (David Hinds)
#
#=======================================================================

if [ -r config.out ] ; then
    . config.out
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
rm -f .prereq.ok $CONFIG $CONFIG_H

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

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

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
    if [ "$answer" = "y" ] ; then
	echo "$2=y" >> $CONFIG
	echo "#define $2 1" >> $CONFIG_H
    else
	echo "# $2 is not defined" >> $CONFIG
	echo "#undef  $2" >> $CONFIG_H
    fi
    eval "$2=$answer"
}

function prompt () {
    export $2
    default=$(eval echo "\$$2")
    /bin/echo "$1 [$default]: \c"
    read answer
    if [ -z "$answer" ] ; then answer="$default" ; fi
    echo "$2=$answer" >> $CONFIG
    echo "#define $2 \"$answer\"" >> $CONFIG_H
    eval "$2=$answer"
}

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

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_H

function symcheck () {
    if ksyms -a | grep $1 >/dev/null 2>&1 ; then
	echo "$2=y" >> $CONFIG
	echo "#define $2 1" >> $CONFIG_H
	eval "$2=y"
    else
	echo "# $2 is not defined" >> $CONFIG
	echo "#undef  $2" >> $CONFIG_H
	eval "$2=n"
    fi
}

function configcheck () {
    if grep "define $1 1" $AUTOCONF >/dev/null 2>&1 ; then
	echo "$1=y" >> $CONFIG
	echo "#define $1 1" >> $CONFIG_H
	eval "$1=y"
    else
	echo "# $1 is not defined" >> $CONFIG
	echo "#undef  $1" >> $CONFIG_H
	eval "$1=n"
    fi
}

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
}

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
	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_H
	configcheck CONFIG_PCI
	configcheck CONFIG_APM
	configcheck CONFIG_SCSI
	configcheck CONFIG_INET
	configcheck CONFIG_MODVERSIONS
	printconfig
	;;
    3)
	echo "# Configured manually" >> $CONFIG
	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
	;;
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

echo "" >> $CONFIG
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
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 "$PATCHLEVEL" in
2)
    if [ $SUBLEVEL -lt 8 ] ; then
	echo "Your kernel is out of date.  Upgrade to 1.2.8 or higher."
	exit 1
    fi
    echo "# KERNEL_PL_3 is not defined" >> $CONFIG
    echo "#undef  KERNEL_PL_3" >> $CONFIG_H
    ;;
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 -gt 30 ] ; then
	echo "Warning: this package has not been tested with kernels more"
	echo "recent than 1.3.30.  Proceed with caution."
    fi
    echo "KERNEL_PL_3=y" >> $CONFIG
    echo "#define KERNEL_PL_3 1" >> $CONFIG_H
    ;;
*)
    echo "This package requires either a 1.2 series kernel 1.2.8 or higher,"
    echo "or a 1.3 series kernel 1.3.30 or higher."
    exit 1
    ;;
esac

echo "UTS_RELEASE=$SRC_RELEASE" >> $CONFIG
echo "#define UTS_RELEASE \"$SRC_RELEASE\"" >> $CONFIG_H
echo ""

#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 [ -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"
	    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?

if [ "$PREFIX" = "" ] ; then
    if [ -d /etc/rc.d/init.d ] ; then
	echo "It looks like you have a System V init file setup."
	SYSV_INIT=y
	echo "SYSV_INIT=y" >> $CONFIG
	echo "#define SYSV_INIT 1" >> $CONFIG_H
    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=
	echo "# SYSV_INIT is not defined" >> $CONFIG
	echo "#undef  SYSV_INIT" >> $CONFIG_H
    fi
else
    SYSV_INIT=n
    bool "System V init script layout" SYSV_INIT
fi

if [ "SYSV_INIT" = "y" ] ; then
    ETC=/etc/sysconfig/pcmcia-scripts
else
    ETC=/etc/pcmcia
fi
echo "The PCMCIA startup scripts will be installed in $ETC."
echo "ETC=$ETC" >> $CONFIG
echo "#define ETC \"$ETC\"" >> $CONFIG_H
echo ""

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

# Optional stuff

if [ -r /usr/lib/libforms.a ] ; then
    echo "/usr/lib/libforms.a found."
    echo "#define HAS_FORMS 1" >> $CONFIG_H
    echo "HAS_FORMS=y" >> $CONFIG
else
    echo "/usr/lib/forms.a not found."
    echo "    If you want to build the 'cardinfo' PCMCIA control" \
         "panel, you need"
    echo "    to install the Forms Library.  See the HOWTO for details."
    echo "#undef  HAS_FORMS" >> $CONFIG_H
    echo "# HAS_FORMS is not defined" >> $CONFIG
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

/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 are 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

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

mv $CONFIG config.out

touch .prereq.ok
