#! /bin/bash
#
# Configure
#
# linux-wlan Open Sourc Project
#
# Copyright (C) 1999 AbsoluteValue Systems, Inc.  All Rights Reserved.
# -------------------------------------------------------------------------
#
# Inquiries regarding the linux-wlan Open Source Project can be
# made directly to:
#
# AbsoluteValue Systems Inc.
# info@linux-wlan.com
# http://www.linux-wlan.com
#
# -------------------------------------------------------------------------
# TODO: Since we're dependent on configured pcmcia source, we should change 
#       this such that it will ask for the pcmcia source dir and then read
#       all our stuff from pcmcia/config.mk.  Would simplify alot of things.
# -------------------------------------------------------------------------
#
# This script adapted from the pcmcia-cs/Configure file, license statement below:
#
# pcmcia-cs/Configure 1.110 1999/06/24 17:37:36
#
# 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 Configure 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.
#-------------------------------------------------------------------------

ECHO="/bin/echo -e "
fail ()
{
	$ECHO ""
	$ECHO "Configuration failed"
	$ECHO ""
	exit 1
}

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
if [ $# -gt 0 ] ; then
	if [ "$1" = "-d" ] ; then 
		PROMPT=n
		if [ $# -gt 1 -a -r $2 ]; then
			. ./$2
		fi
	fi
fi

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

usage () {
    echo "usage: Configure [-d <filename>]"
    echo ""
    echo "  -d <filename>       read configuration data from file"
    exit 1
}

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

CONFIG=config.new
CONFIG_MK=config.mk
rm -f $CONFIG $CONFIG_MK $MODVER

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
}


dump_str () {
    $ECHO "$1" >> $CONFIG
    $ECHO "$1" >> $CONFIG_MK
}

prompt () {
    eval $3=\"$2\"
    if [ "$PROMPT" = "y" ] ; then
	$ECHO "$1 [$2]: \c"
	read tmp
	if [ -n "$tmp" ] ; then eval $3=\"$tmp\" ; fi
    else
	$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 WLAN Configuration Script -------------"
$ECHO ""
$ECHO "The default responses are correct for most users."
$ECHO ""

# Collect the kernel source tree and test for sanity

ask_str "Linux source directory" LINUX_SRC

if [ ! -f $LINUX_SRC/Makefile ] ; then
    $ECHO "Linux source tree $LINUX_SRC is incomplete or missing!"
    if [ -d $LINUX_SRC/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

# What kernel are we compiling for?

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

$ECHO ""
for TAG in VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION ; do
    eval `sed -ne "/^$TAG/s/[ 	]//gp" $LINUX_SRC/Makefile`
done
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 series kernel."
    fail
fi
CUR_RELEASE=`uname -r`
if [ "$SRC_RELEASE" != "$CUR_RELEASE" ] ; then
    $ECHO "WARNING: the current kernel is actually version $CUR_RELEASE."
fi

if [ ! "$WLAN_TARGET_ARCH" ] ; then
    BUILD_ARCH=`uname -m | sed -e 's/i.86/i386/'`
else
    BUILD_ARCH=$WLAN_TARGET_ARCH
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_SRC/include/linux/compile.h ] ; then
    UTS_VERSION=`grep UTS_VERSION $LINUX_SRC/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 ""

# PCMCIA Source dir
if [ ! "$PCMCIA_SRC" ] ; then
    PCMCIA_SRC=`/sbin/cardctl -V 2>&1 | sed -e 's/cardctl version //'`
    PCMCIA_SRC=/usr/src/pcmcia-cs-$PCMCIA_SRC
fi
ask_str "pcmcia-cs source dir" PCMCIA_SRC

$ECHO ""
# Alternate target install root dir - the value of this variable
# will prefix other variables, such as modules and pcmcia directories
ask_str "Alternate target install root directory on host" TARGET_ROOT_ON_HOST

# Start looking for the destination module dir
#MODDIR=$MODULES_DIR
#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

MODDIR=/lib/modules/$SRC_RELEASE
ask_str "  Module install directory" MODDIR
TARGET_MODDIR=$TARGET_ROOT_ON_HOST$MODDIR
write_str TARGET_MODDIR
$ECHO ""

# PCMCIA script dir
ask_str "PCMCIA script directory" PCMCIA_DIR
TARGET_PCMCIA_DIR=$TARGET_ROOT_ON_HOST$PCMCIA_DIR
write_str TARGET_PCMCIA_DIR
$ECHO ""

# Just write some out (we're not prompting right now)
write_str INST_EXEDIR
TARGET_INST_EXEDIR=$TARGET_ROOT_ON_HOST$INST_EXEDIR
write_str TARGET_INST_EXEDIR

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

# How should the startup scripts be configured?

SYSV_INIT=n
if [ "$PREFIX" = "" ] ; then
    if [ -d /etc/rc.d/init.d -o -d /etc/init.d -o -d /sbin/init.d ] ; then
	$ECHO "It looks like you have a System V init file setup."
	SYSV_INIT=n
	if [ -d /etc/rc.d/init.d ] ; then
	    $ECHO "RC_DIR=/etc/rc.d" >> $CONFIG
	    $ECHO "RC_DIR=/etc/rc.d" >> $CONFIG_MK
	elif [ -d /sbin/init.d ] ; then
	    $ECHO "RC_DIR=/sbin" >> $CONFIG
	    $ECHO "RC_DIR=/sbin" >> $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.wlan /etc/rc.d/rc.S >/dev/null ; then
	    $ECHO "    You'll need to edit /etc/rc.d/rc.S to invoke" \
		 "/etc/rc.d/rc.wlan (for ISA/PCMCIA cards)"
	    $ECHO "    so that wlan cards will be started at boot time."
	fi
	SYSV_INIT=n
    fi
    write_bool SYSV_INIT
else
    ask_bool "System V init script layout" SYSV_INIT
    if [ "$SYSV_INIT" = "y" ] ; then
	ask_str "Top-level directory for RC scripts" RC_DIR
    fi
fi
if [ ! -x $TARGET_ROOT_ON_HOST/sbin/depmod ] ; then INSTALL_DEPMOD=n ; fi
write_bool INSTALL_DEPMOD

$ECHO ""

#=======================================================================
# Make sure our target architecture is the same as our build architecture.

if [ ! WLAN_INSTALL_TYPE ] ; then
	WLAN_INSTALL_TYPE=sta
fi
ask_str "Install drivers as station (sta) or access point (ap)?" WLAN_INSTALL_TYPE

$ECHO ""

#=======================================================================
# Make sure our target architecture is correct

WLAN_TARGET_ARCH=$BUILD_ARCH
ask_str "Target Architecture? (i386 or ppc)" WLAN_TARGET_ARCH

$ECHO ""

ask_str "Prefix for build host compiler? (rarely needed)" HOST_COMPILE

$ECHO ""

dump_str 'HOST_AS=$(HOST_COMPILE)as'
dump_str 'HOST_LD=$(HOST_COMPILE)ld'
dump_str 'HOST_CC=$(HOST_COMPILE)gcc'
dump_str 'HOST_CPP=$(HOST_CC) -E'
dump_str 'HOST_AR=$(HOST_COMPILE)ar'
dump_str 'HOST_NM=$(HOST_COMPILE)nm'
dump_str 'HOST_STRIP=$(HOST_COMPILE)strip'
dump_str 'HOST_OBJCOPY=$(HOST_COMPILE)objcopy'
dump_str 'HOST_OBJDUMP=$(HOST_COMPILE)objdump'
dump_str 'HOST_RANLIB=$(HOST_COMPILE)ranlib'
dump_str 'HOST_MAKE=make'

if [ "$CROSS_COMPILE_ENABLED" != "n" -a "$CROSS_COMPILE_ENABLED" != "y" ] ; then
	CROSS_COMPILE_ENABLED=n
fi

ask_bool "Compiling with a cross compiler?" CROSS_COMPILE_ENABLED

$ECHO ""

if [ "$CROSS_COMPILE_ENABLED" = "y" ] ; then
	ask_str "Prefix for cross compiler? (change if cross-compiling)" CROSS_COMPILE
	$ECHO ""
else
	CROSS_COMPILE=""
	write_str CROSS_COMPILE
fi

dump_str 'CROSS_AS=$(CROSS_COMPILE)as'
dump_str 'CROSS_LD=$(CROSS_COMPILE)ld'
dump_str 'CROSS_CC=$(CROSS_COMPILE)gcc'
dump_str 'CROSS_CPP=$(CROSS_CC) -E'
dump_str 'CROSS_AR=$(CROSS_COMPILE)ar'
dump_str 'CROSS_NM=$(CROSS_COMPILE)nm'
dump_str 'CROSS_STRIP=$(CROSS_COMPILE)strip'
dump_str 'CROSS_OBJCOPY=$(CROSS_COMPILE)objcopy'
dump_str 'CROSS_OBJDUMP=$(CROSS_COMPILE)objdump'
dump_str 'CROSS_RANLIB=$(CROSS_COMPILE)ranlib'
dump_str 'CROSS_MAKE=make'

#=======================================================================
# Should we build for debugging?

ask_bool "Build for debugging (see doc/config.debug)" WLAN_DEBUG

$ECHO ""
#=======================================================================
# Should we build for Kernel based PCMCIA

ask_bool "Build for Kernel PCMCIA?" WLAN_KERN_PCMCIA

$ECHO ""
#=======================================================================

mv $CONFIG config.out

$ECHO ""
$ECHO "Configuration successful."
$ECHO ""
