#!/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

if [ "$UNAME" != "FreeBSD" ]; then
    echo "We only install the cephes library on FreeBSD."
    exit 0
fi

CUR=`pwd`
cd src

# Apply patches:
echo "Applying patches (if any)..."
for patch in ../patches/*.patch; do
    patch -p1 <"$patch"
    if [ $? -ne 0 ]; then
        echo >&2 "Patch '$patch' failed to apply."
        exit 1
    fi
done

echo "Building Cephes..."
$MAKE
if [ $? -ne 0 ]; then
   echo >&2 "Error - Failed to build Cephes ... exiting"
   exit 1
fi

echo "Installing Cephes..."
$MAKE install
if [ $? -ne 0 ]; then
   echo >&2 "Error - Failed to build Cephes ... exiting"
   exit 1
fi
