#! /bin/sh

usage="USAGE: configure [--prefix <path>]"
help="Valid options are:

--default-architecture <architecture>
        Set the default architecture (default: autodetected)

--default-format <format>
        Set the default output format (default: elf)

--prefix <path>
        Install to <path> (default: /usr/local)
"

VERSION=`cat VERSION`

#
# Process command line
#
while [ $# -gt 0 ]
do
    case "$1" in
    --help)
	echo "$usage"
        echo "$help"
        exit
    ;;
    --default-architecture)
	shift
	DEFAULT_ARCHITECTURE="$1"
    ;;
    --default-format)
	shift
	DEFAULT_FORMAT="$1"
    ;;
    --prefix)
        shift
	PREFIX="$1"
    ;;
    -*)
        echo "Invalid option: $1" >&2
	exit 128
    ;;
    *)
        echo "Invalid argument: $1" >&2
        exit 128
    esac
    shift
done

#
# Defaults
#
[ -n "$PREFIX" ] || PREFIX=/usr/local
[ -n "$BINDIR" ] || BINDIR="\$(PREFIX)/bin"
[ -n "$DOCDIR" ] || DOCDIR="\$(PREFIX)/share/doc"
[ -n "$MANDIR" ] || MANDIR="\$(PREFIX)/share/man"
[ -n "$RUBYLIBDIR" ] || RUBYLIBDIR="\$(PREFIX)/lib/site_ruby"

[ -n "$DEFAULT_ARCHITECTURE" ] || DEFAULT_ARCHITECTURE=auto
[ -n "$DEFAULT_FORMAT" ] || DEFAULT_FORMAT=elf

printf 'Testing for ruby...'
[ -n "$RUBY" ] || RUBY=`which ruby 2>/dev/null`
if [ $? -eq 0 ]
then
    echo $RUBY
else
    echo "not found"
    cat <<EOT
WARNING: Ruby interpreter not found. You need a Ruby interpreter to
run the voodooc compiler.

EOT
fi

printf 'Testing for nasm...'
[ -n "$NASM" ] || NASM=`which nasm 2>/dev/null` || NASM=`which yasm 2>/dev/null`
if [ $? -eq 0 ]
then
    echo $NASM
else
    echo "not found"
    cat <<EOT
WARNING: NASM not found. You need NASM to create i386 object code.

EOT
fi

printf 'Testing for GNU assembler...'
if [ -z "$GAS" ]
then
    for x in gas as
    do
	GAS=`which "$x"` 2>/dev/null
	if [ $? -eq 0 ]
	then
	    # Found an executable that has the right name;
	    # test if it is really the GNU assembler
	    output=`$GAS --version < /dev/null 2>&1`
	    if echo "$output" | grep -q '^GNU assembler'
	    then
		# Yes, it is the GNU assembler
		true
	    else
		# Not the GNU assembler, continue the search
		unset GAS
	    fi
	fi
	[ -z "$GAS" ] || break 
    done
fi

if [ -n "$GAS" ]
then
    echo $GAS
else
    echo "not found"
    cat <<EOT
WARNING: GNU assembler not found. You need the GNU assembler to create
mips or mipsel object code.

EOT
fi

printf 'Testing for rdoc...'
[ -n "$RDOC" ] || RDOC=`which rdoc 2>/dev/null`
if [ $? -eq 0 ]
then
    echo $RDOC
else
    echo "not found"
    cat <<EOT
WARNING: rdoc not found. You need rdoc to generate API documentation
from the library files.

EOT
fi

printf 'Testing for gem...'
[ -n "$GEM" ] || GEM=`which gem 2>/dev/null`
if [ $? -eq 0 ]
then
    echo $GEM
else
    echo "not found"
    cat <<EOT
WARNING: RubyGems gem command not found. You need the gem command to
create gems.

EOT
fi

printf 'Writing Makefile.cfg...'
cat <<EOF > Makefile.cfg
VERSION = $VERSION

DEFAULT_ARCHITECTURE = $DEFAULT_ARCHITECTURE
DEFAULT_FORMAT = $DEFAULT_FORMAT

PREFIX = $PREFIX
BINDIR = $BINDIR
DOCDIR = $DOCDIR
MANDIR = $MANDIR
RUBYLIBDIR = $RUBYLIBDIR

GAS = $GAS
GEM = $GEM
NASM = $NASM
RDOC = $RDOC
RUBY = $RUBY
EOF
if [ $? -eq 0 ]
then
    echo 'done'
    echo ''
    cat Makefile.cfg
fi

