#!/bin/sh
#
# This is the configuration script for the lkcdutils package.
#
#

# Set some default values. 
#
if [ -z $ARCH ] ;
then
	ARCH=`uname -m | sed -e s/i.86/i386/`
fi
if [ -z $TOPDIR ] ;
then
	TOPDIR=/usr/src/linux
fi

#
# GCC Debuging Options:
#
#	Curently only one flag can currently be passed
#	down the tree to the sub-directory Makefiles.
#
# GFLAGS=-g
# GFLAGS=-g1
# GFLAGS=-g3
# GFLAGS=-ggdb
# GFLAGS=-ggdb1
# GFLAGS=-ggdb3
GFLAGS=-gstabs
# GFLAGS=-gstabs1
# GFLAGS=-gstabs3
# GFLAGS=-gstabs+

LKCDUTILS_DIR=`pwd`
outfile=.config
config_header=lcrash/include/lc_config.h


# Loop over all args

while :
do

# Get the first arg, and shuffle
	case $# in
		0)
		break
		;;
		esac

# Get the first arg, and shuffle
	option=$1
	shift

# Make all options have two hyphens
	orig_option=$option # Save original for error messages
	case $option in
		--*) ;;
		-*) option=-$option ;;
		esac

# Split out the argument for options that take them
	case $option in
	--*=*)
		optarg=`echo $option | sed -e 's/^[^=]*=//'`
		arguments="$arguments $option"
		;;
	esac

# Now, process the options. Note that there are a number of unused
# options (e.g., --prefix) that have to be here to so that configure
# runs properly via rpm -ba.
	case $option in

	--arch*)
		ARCH=$optarg
		;;

	--gflags*)
		GFLAGS=$optarg
		;;

	--topdir*)
		TOPDIR=$optarg
		;;

	--outfile*)
		outfile=$optarg
		;;

	--help | --he*)
		fatal=yes
		;;

	--prefix*)
		prefix=$optarg
		;;

	--exec-prefix*)
		exec-prefix=$optarg
		;;

	--bindir*)
		bindir=$optarg
		;;

	--sbindir*)
		sbindir=$optarg
		;;

	--sysconfdir*)
		sysconfdir=$optarg
		;;

	--datadir*)
		datadir=$optarg
		;;

	--includedir*)
		includedir=$optarg
		;;

	--libdir*)
		libdir=$optarg
		;;

	--libexecdir*)
		libexecdir=$optarg
		;;

	--localstatedir*)
		localstatedir=$optarg
		;;

	--sharedstatedir*)
		sharedstatedir=$optarg
		;;

	--mandir*)
		mandir=$optarg
		;;

	--infodir*)
		infodir=$optarg
		;;

	--bfd_version*)
		BFD_VERSION=$optarg
		;;
	--*)
        	echo "configure: Unrecognized option: \"$orig_option\"; use --help for usage." >&2
		exit 1
		;;
	esac
done

# Make sure ARCH and TOPDIR are defined and that ARCH is an architecture
# we support and that TOPDIR points to a valid directory.
# 

if [ -z $ARCH ] ;
then
        echo
        echo configure: ARCH is undefined!
        fatal=yes
elif [ $ARCH != "i386" -a $ARCH != "ia64" -a $ARCH != "s390" -a $ARCH != "s390x" ] ;
then
	echo
	echo configure: $ARCH is not a supported architecture!
	fatal=yes
fi

if [ -z $TOPDIR ] ;
then
	echo
	echo configure: TOPDIR is undefined! 
	fatal=yes
elif [ ! -d $TOPDIR ] ;
then
	echo
	echo configure: $TOPDIR is not a valid directory!
	fatal=yes
fi
if [ -z $BFD_VERSION ] ;
then
	/usr/bin/gcc -Wall get_bfd_version.c -o get_bfd_version
	if [ $? -eq 0 ] ;
	then
		BFD_VERSION_NUMBER=`./get_bfd_version`
		if [ $? -ne 0 ] ;
		then
			echo
			echo configure: Could not determine BFD_VERSION_NUMBER!
			fatal=yes
		fi
	fi
else
	/usr/bin/gcc -Wall get_bfd_version.c -o get_bfd_version -DBFD_VERSION=$BFD_VERSION
	if [ $? -eq 0 ] ;
	then
		BFD_VERSION_NUMBER=`./get_bfd_version`
		if [ $? -ne 0 ] ;
		then
			echo
			echo configure: Could not determine BFD_VERSION_NUMBER!
			fatal=yes
		fi
	fi
fi
    

if [ $fatal ] ;
then
	echo
	echo usage: configure [OPTIONS]
	echo
	echo Options: 
	echo ' --help             Print this message'
	echo ' --arch=ARCH        Target system architecture'
	echo ' --gflags=GFLAGS    One gcc Debugging Option {Ex: -gstabs, -g}'
	echo '  -g		  Set gcc Debugging Option gflags=-g
	echo '  -gstabs		  Set gcc Debugging Option gflags=-gstabs
	echo ' --topdir=TOPDIR    Top level directory of kernel source tree'
	echo ' --outfile=OUTFILE  Output file name'
	echo ' --bfd_version=BFD_VERSION'
	echo '                    version of libbfd (should be same as binutils version)'
	exit 0
fi

echo "# This file is automaticly generated by the lkcdutils configure script." > $outfile
echo "# Do not edit by hand!" >> $outfile
echo "# " >> $outfile
echo LKCDUTILS_DIR=$LKCDUTILS_DIR >> $outfile
echo TOPDIR=$TOPDIR >> $outfile
echo ARCH=$ARCH >> $outfile
echo GFLAGS=$GFLAGS >> $outfile
# Create lcrash/include/config.h
echo "/* This file is automaticly generated by the lkcdutils configure script." > $config_header
echo " * Do not edit by hand!" >> $config_header
echo " */" >> $config_header
echo "#define _BFD_VERSION_NUMBER_(a,b,c) (((a) << 24) + ((b) << 12) + (c))" >> $config_header
echo "#define BFD_VERSION_NUMBER $BFD_VERSION_NUMBER" >> $config_header

# Now clean up the workarea
make clobber
