#!/bin/sh
#
# Configure script for OS/161 tree.
# This generates the file "defs.mk" at the top level of the tree.
#
# Usage: ./configure [options]
#   where you can get a list of the options by doing ./configure --help.
#
# Must be run with the top of the OS/161 tree as its current directory.
#
# Note: while this superficially acts like a GNU Autoconf configure
# script, it was not generated by autoconf. Scripts generated by 
# autoconf are much harder to read. :-)
#
# If this script bombs out, you can create defs.mk by hand based on
# the comments in mk/os161.config.mk.
#

# Target hardware platform and machine type.
PLATFORM='sys161'
MACHINE='mips'

# Default optimize/debug flag: optimize.
DEBUG='-O2'

# Default location of the root of the installed system.
# Note that we quote it such that the reference to the home directory
# is a make variable, not a shell variable. This means it gets expanded
# when make runs rather than when this script runs.
OSTREE='$(HOME)/cs350-os161/root'

# Assume this
HOST_CC=gcc

##################################################

#
# Check to make sure we're in the right place.

if [ ! -d kern/startup ]; then
    echo 'Please run configure from the top of the OS/161 tree.'
    exit 1
fi

# UW Mod
# Default toolchain name.
TOOLPREFIX="cs350-"

#
# Process the command-line options.

while [ "x$1" != x ]; do
    case "$1" in
	--debug) DEBUG='-g';;
	--ostree=*) OSTREE=`echo $1 | sed 's,^[^=]*=,,'`;;
  # UW Mod
  --toolprefix=*) TOOLPREFIX=`echo $1 | sed 's,^[^=]*=,,'`;;

	--help|*)
		more <<EOF
Usage: ./configure [options]
  where the options are:
    --help               Print this message.

    --debug              Compile the user-level programs with debug info.
                         This is disabled by default because there's no
                         support for userlevel source debugging in OS/161.
                         (Note: debug info in the kernel is controlled by
                         the kernel config file.)

    --ostree=PATH        Install the compiled system in a directory tree
                         rooted at PATH. Default is \$HOME/cs161/root.

    --toolprefix=NAME    Set up to use compiler and tools named with a
                         prefix of NAME. The default is "cs350-", so the
                         tools used are called cs350-gcc, cs350-ld, etc.
                         The directory with these tools should be on your
                         shell's search path.
EOF
    exit
    ;;
    esac
shift
done


# Check if the host system supports 4.4BSD <err.h>.

echo -n "Checking for <err.h>... "

cat > __conftest.c <<EOF
#include <err.h>
int
main()
{
    err(0, "works");
    return 1;
}
EOF

OK=0
if $HOST_CC __conftest.c -o __conftest >/dev/null 2>&1; then
    if ./__conftest >/dev/null 2>&1; then
	OK=1
    fi
fi

rm -f __conf*

if [ $OK = 1 ]; then
    echo 'yes'
else
    echo 'no'
    COMPAT_CFLAGS="${COMPATCFLAGS} -DNEED_ERR"
    COMPAT_TARGETS="${HOSTTARGETS} install-errh"
fi

####################

# Now generate defs.mk.

echo 'Generating defs.mk.'

(
    # First, put an explanatory comment at the top.
    cat <<EOF
# This file was generated by configure. Edits will disappear if you rerun
# configure. If you find that you need to edit this file to make things
# work, let the course staff know and we'll try to fix the configure script.
#
# The purpose of this file is to hold all the makefile definitions
# needed to adjust the OS/161 build process to any particular
# environment. If I've done it right, all you need to do is rerun the
# configure script and make clean if you start working on a different
# host OS. If I've done it mostly right, you may need to edit this
# file but you still hopefully won't need to edit any of the
# makefiles.
#
# The things that can be set here are documented in mk/os161.config.mk.
#

EOF
    echo "OSTREE=${OSTREE}"
    echo "PLATFORM=${PLATFORM}"
    echo "MACHINE=${MACHINE}"
    echo "COMPAT_CFLAGS=${COMPAT_CFLAGS}"
    echo "COMPAT_TARGETS=${COMPAT_TARGETS}"
    # UW Mod
    echo "TOOLPREFIX=${TOOLPREFIX}"

) > defs.mk
