#!/bin/sh


# Check required environment variable
if [ -z "$SAGE_SHARE" ]; then
    echo >&2 "SAGE_SHARE undefined ... exiting"
    echo >&2 "Maybe run 'sage --sh'?"
    exit 1
fi

cd src

# Apply all patches
for patch in ../patches/*.patch; do
    [ -r "$patch" ] || continue  # Skip non-existing or non-readable patches
    echo "Applying $patch"
    patch -p1 <"$patch"
    if [ $? -ne 0 ]; then
        echo >&2 "Error applying '$patch'"
        exit 1
    fi
done

cd ..

# Move mathjax to its final destination.
TARGET="${SAGE_SHARE}/mathjax"
rm -rf "${TARGET}"
cp -r 'src' "${TARGET}"

