if [ -z "$SAGE_LOCAL" ]; then
    echo >&2 "Error: SAGE_LOCAL undefined ... exiting"
    echo >&2 "Maybe run 'sage -sh'?"
    exit 1
fi

if [ "x$SAGE64" = xyes ]; then
    echo "Building a 64-bit version of lcalc"

    # Both Sun and GNU compilers use -m64 to build 64-bit code, but compilers
    # from IBM (on AIX), and HP (on HP-UX) do not. So allow the environment
    # variables CFLAG64 and CXXFLAG64 to set the flag to whatever option the
    # C and C++ compilers want for 64-bit code. If not set, default to -m64.

    if [ -z "$CFLAG64" ]; then
        CFLAG64=-m64
    fi
    if [ -z "$CXXFLAG64" ]; then
        CXXFLAG64="$CFLAG64" # default to that of the C compiler
    fi

    CFLAGS="$CFLAGS $CFLAG64"
    CXXFLAGS="$CXXFLAGS $CXXFLAG64"
fi

# If SAGE_DEBUG is set to 'yes', add debugging information.  Since both
# the Sun and GNU compilers accept -g to give debugging information,
# there is no need to do anything specific to one compiler or the other.
if [ "x$SAGE_DEBUG" = xyes ]; then
    echo "Code will be built with debugging information present. Unset 'SAGE_DEBUG'"
    echo "or set it to 'no' if you don't want that."

    CFLAGS="$CFLAGS -O0 -g"
    CXXFLAGS="$CXXFLAGS -O0 -g"
else
    echo "No debugging information will be used during the build of this package."
    echo "Set 'SAGE_DEBUG' to 'yes' if you want debugging information present (-g added)."
fi

# Export everything. Probably not necessary in most cases.
export CFLAGS
export CXXFLAGS

success() {
    if [ $? -ne 0 ]; then
        echo >&2 "Error building the Lcalc package: '$1'"
        exit 1
    fi
}

export DEFINES=""

cd src/src   # Now we are in src/src.

# Build everything:
echo "Now building lcalc, example programs and the shared library..."
$MAKE
success "$MAKE"

