#!/bin/sh

echo "Configuring AllocFS..."

install_path="/usr"
linux_source_path="/usr/src/linux"

IFS=" ="
export IFS

while [ $# -gt 0 ]
do
	case $1 in
		--prefix)
			shift
			install_path=$1
			;;
		--with-linux-source)
			shift
			linux_source_path=$1
			;;
		--with-*-path)
			echo "ignoring unknown package $1"
			shift
			;;
		--without-*)
			echo "ignoring unknown package $1"
			shift
			;;
		-h | -help | --h | --help)
			cat <<EOF
Use: configure [options]
Where options are:
  --help
  --prefix <path>
  --with-linux-source <path>
EOF
			exit 1
			;;
		*)
			echo "Unknown argument $1"
			exit 1
			;;
	esac
	shift
done

linux_error()
{
	cat <<EOF

Sorry, I can't continue.

In order to build this module, you must download
configure, and compile a Linux kernel that is the
same version as the system you wish to run AllocFS on.

Once you have done that, tell configure where
this package is with a command like this:

./configure --with-linux-source /path/to/linux-2.4.21

EOF
	exit 1
}

IFS=" "
export IFS

rm -f Makefile.config

echo "Checking for an appropriate Linux source in $linux_source_path..."
if [ -d $linux_source_path ]
then
	echo "ok, $linux_source_path exists."
	if [ -f $linux_source_path/Makefile ]
	then
		echo "ok, it contains a Makefile."
		if [ -f $linux_source_path/.config ]
		then
			echo "ok, it has actually been configured."
		else
			linux_error
		fi
	else
		linux_error
	fi
else
	linux_error
fi

echo "Creating Makefile.config..."

cat >Makefile.config <<EOF
INSTALL_PATH = ${install_path}
LINUX_SOURCE_PATH = ${linux_source_path}
EOF

echo ""
echo "To build, type 'make'"
echo "To install the tools, type 'make install'"
echo ""

exit 0
