#! /bin/bash
#
# Configure
#
#
#################################################################################
# Ralink Technology, Inc.	                                         	        #
# 4F, No. 2 Technology 5th Rd.                                          	    #
# Science-based Industrial Park                                         	    #
# Hsin-chu, Taiwan, R.O.C.                                              	    #
#                                                                       	    #
# (c) Copyright 2005, Ralink Technology, Inc.                           	    #
#                                                                       	    #
# All rights reserved. Ralink's source code is an unpublished work and the	    #
# use of a copyright notice does not imply otherwise. This source code		    #
# contains confidential trade secret material of Ralink Tech. Any attempt	    #
# or participation in deciphering, decoding, reverse engineering or in any	    #
# way altering the source code is stricitly prohibited, unless the prior	    #
# written consent of Ralink Technology, Inc. is obtained.			            #
#################################################################################

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


PROMPT=y

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

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_str () {
    value=`eval $ECHO '$'$1`
    $ECHO "$1"=\"$value\" >> $CONFIG
    $ECHO "$1=$value" >> $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_str () {
    default=`eval $ECHO '$'$2`
    prompt "$1" "`$ECHO $default`" answer
    eval $2=\"$answer\"
    write_str $2
}


$ECHO ""
$ECHO ""
$ECHO "-------------------- Ralink RT73 Station Configuration -------------------- "
$ECHO ""

CUR_RELEASE=`uname -r`
LINUX_SRC=/usr/src/linux-$CUR_RELEASE

if [ ! -d $LINUX_SRC ] ; then
	ask_str "  Linux kernel source directory" LINUX_SRC
	$ECHO   " "
	if [ ! -d $LINUX_SRC ] ; then	
		$ECHO   "Linux source tree '$LINUX_SRC' is incomplete or missing!"		
		fail
	fi
fi	

$ECHO   "  Linux kernel source directory : $LINUX_SRC"
$ECHO   " "
write_str LINUX_SRC


# What kernel are we compiling for?
version () {
	$ECHO ""
	expr $1 \* 65536 + $2 \* 256 + $3
}

for TAG in VERSION PATCHLEVEL SUBLEVEL EXTRAVERSION ; do
	eval `sed -ne "/^$TAG/s/[ 	]//gp" $LINUX_SRC/Makefile`
done

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

MODDIR=/lib/modules/$CUR_RELEASE
TARGET_MODDIR=$MODDIR

if [ -d $MODDIR/kernel/drivers/net ] ; then
	MODDIR=$MODDIR/kernel/drivers/net
else
	ask_str "  Module install directory" MODDIR	
	if [ ! -d $MODDIR ] ; then
		$ECHO   "Module install directory '$MODDIR' is incomplete or missing!"		
		fail	
	fi
fi
	
$ECHO   "  Module install directory : $MODDIR"
$ECHO   " "

TARGET_MODDIR=$MODDIR
write_str TARGET_MODDIR
