#!/usr/bin/env bash

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

cd src/

# Apply all patches
for patch in ../patches/*.patch; do
    [ -f "$patch" ] || continue
    basename "$patch"
    if ! patch -p1 <"$patch"; then
        echo >&2 "Error: patch '$patch' failed to apply."
        exit 1
    fi
done


if [ "$UNAME" = CYGWIN ]; then
    export BINARY_PATH="$SAGE_LOCAL/bin"
    export INCLUDE_PATH="$SAGE_LOCAL/include"
    export LIBRARY_PATH="$SAGE_LOCAL/lib"
    cp -f win32/Makefile.gcc Makefile

    if [ $? -ne 0 ]; then
        echo >&2 "Error copying over zlib's Makefile for Cygwin."
        exit 1
    fi

    # We want to install shared objects
    sed -i 's/SHARED_MODE=0/SHARED_MODE=1/' Makefile
else
    if [ "$SAGE64" = yes ]; then
        ./configure --64 --shared --prefix="$SAGE_LOCAL" --includedir="$SAGE_LOCAL/include"
    else
        ./configure --shared --prefix="$SAGE_LOCAL" --includedir="$SAGE_LOCAL/include"
    fi
    if [ $? -ne 0 ]; then
        echo >&2 "Error configuring zlib."
        exit 1
    fi
fi

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

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