#!/usr/bin/env bash
###########################################
## PARI
###########################################

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

# Let the user choose a flag other than -m64 for 64-bit builds
# if needed:
if [ -z "$CFLAG64" ]; then
    CFLAG64=-m64
fi

if [ "x$SAGE64" = xyes ]; then
    CFLAGS="$CFLAGS $CFLAG64"
    CC="$CC $CFLAG64" && export CC
fi

if [ "x$SAGE_DEBUG" = xyes ] ; then
    CFLAGS="$CFLAGS -O0 -g" # Disable optimisation, add debug symbols. Good
                            # for debugging or working around compiler bugs.
else
    CFLAGS="-O3 -g $CFLAGS" # Default optimisation, with debug symbols.
                            # Prepend to not override user's setting.
fi

export CFLAGS

cd src

$MAKE test-all

if [ $? -ne 0 ]; then
    echo "Error: PARI failed the self-tests when running '$MAKE test-all'"
    exit 1
fi

if [ -d ../parigit ]; then
    echo "WARNING: You should delete the parigit directory before submitting this spkg."
fi

echo "The PARI self-tests all passed"
