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

cd src

if [ "$SAGE_FAT_BINARY" = yes ]; then
    # Let's disable a bunch of stuff which might get linked.
    # SSL/TLS still depends on the compilation environment.
    CURL_CONFIGURE="--disable-ldap --disable-ldaps --disable-rtsp --disable-ares --disable-crypto-auth --without-libpsl --without-libmetalink --without-libssh2 --without-librtmp --without-libidn --without-nghttp2 --without-gssapi $CURL_CONFIGURE"
fi

if [ "$UNAME" = "Darwin" ]; then
    if [ $MACOSX_VERSION -ge 16 ]; then
        echo "OS X 10.$[$MACOSX_VERSION-4] Building with clang and --with-darwinssl."
        CC=clang
        CURL_CONFIGURE="--with-darwinssl $CURL_CONFIGURE"
    fi
fi

./configure --prefix="$SAGE_LOCAL" --libdir="$SAGE_LOCAL/lib" \
    $CURL_CONFIGURE
if [ $? -ne 0 ]; then
    echo >&2 "Error configuring curl."
    exit 1
fi

$MAKE
if [ $? -ne 0 ]; then
    echo >&2 "Error building curl."
    exit 1
fi

$MAKE install
if [ $? -ne 0 ]; then
    echo >&2 "Error installing curl."
    exit 1
fi
