#!/usr/bin/env bash

SUPP=""
if [ -f "$SAGE_EXTCODE/valgrind/python.supp" ]; then
    SUPP+=" --suppressions=$SAGE_EXTCODE/valgrind/python.supp"
else
    echo "Python suppressions not found (not installed?), skipping"
fi
SUPP+=" --suppressions=$SAGE_EXTCODE/valgrind/pyalloc.supp"
SUPP+=" --suppressions=$SAGE_EXTCODE/valgrind/sage.supp"
SUPP+=" --suppressions=$SAGE_EXTCODE/valgrind/sage-additional.supp"

MEMCHECK_FLAGS="--leak-resolution=high --leak-check=full --num-callers=25 $SUPP"

if [ "$SAGE_MEMCHECK_FLAGS" ]; then
    echo "Overwriting memcheck flags with: $SAGE_MEMCHECK_FLAGS"
    MEMCHECK_FLAGS=$SAGE_MEMCHECK_FLAGS
else
    echo "Using default flags: $MEMCHECK_FLAGS"
fi

valgrind --tool=memcheck $MEMCHECK_FLAGS python "$SAGE_LOCAL/bin/sage-ipython" "$@" -i
