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

for patch in ../patches/*.patch; do
    [ -f "$patch" ] || continue
    patch -p1 <"$patch"
    if [ $? -ne 0 ]; then
        echo >&2 "Error applying '$patch'"
        exit 1
    fi
done

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

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

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

