#!/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)/cs3231/root'

# By default don't explicitly configure a Python interpreter.
PYTHON_INTERPRETER=

# Assume this
HOST_CC=gcc

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

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

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

#
# Process the command-line options.

while [ "x$1" != x ]; do
    case "$1" in
	--debug) DEBUG='-g';;
	--ostree=*) OSTREE=`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/cs3231/root.
EOF
    exit
    ;;
    esac
shift
done


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

printf "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


# Check if the host system has ntohll() and htonll().

printf "Checking for ntohll()..."

cat > __conftest.c <<EOF
#include <arpa/inet.h>
int
main()
{
    int ok = ntohll(0xbeefbeefbeefbeefULL) != 0;
    return !ok; /* exit(0) for success */
}
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_NTOHLL"
    HOST_CFLAGS="${HOST_CFLAGS} -DDECLARE_NTOHLL"
fi


# Look for a Python interpreter.
PYTHON=
for PYNAME in \
	python python2.7 \
	/usr/local/bin/python /usr/local/bin/python2.7 \
	/usr/pkg/bin/python /usr/pkg/bin/python2.7 \
	/opt/bin/python /opt/bin/python2.7 \
    ; do
    PYVERSION=`($PYNAME -V) 2>&1 || echo none`
    case "$PYVERSION" in
	Python\ 2.*|Python\ 3.*) PYTHON=$PYNAME;;
	none) ;;
	*) ;;  # ?
    esac
done
case "$PYTHON" in
    python) ;;
    /*) PYTHON_INTERPRETER="$PYTHON";;
    *) PYTHON_INTERPRETER="/usr/bin/env $PYTHON";;
esac

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

# 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}"
    echo "HOST_CC=${HOST_CC}"
    if [ "x$HOST_CFLAGS" != x ]; then
	echo "HOST_CFLAGS+=$HOST_CFLAGS"
    fi
    if [ "x$PYTHON_INTERPRETER" != x ]; then
	echo "PYTHON_INTERPRETER=$PYTHON_INTERPRETER"
    fi

) > defs.mk
