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

if [ -z "$CFLAG64" ] ; then
    CFLAG64=-m64
fi

if [ "x$SAGE64" = xyes ] ; then
    echo "Building a 64-bit version of Symmetrica."
    CFLAGS="$CFLAGS $CFLAG64"
fi

export CFLAGS="-O2 -g $CFLAGS -fPIC -DFAST -DALLTRUE"


cd src


# Patching the upstream makefile doesn't make sense,
# as it has (syntactically) nothing in common with ours.
cp -f ../patches/makefile makefile
if [ $? -ne 0 ]; then
    echo >&2 "Error copying over patched Makefile."
    exit 1
fi

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

cp -f libsymmetrica.a "$SAGE_LOCAL"/lib/
if [ $? -ne 0 ]; then
    echo >&2 "Error installing the Symmetrica library."
    exit 1
fi

mkdir -p "$SAGE_LOCAL"/include/symmetrica &&
cp -f *.h "$SAGE_LOCAL"/include/symmetrica/
if [ $? -ne 0 ]; then
    echo >&2 "Error installing Symmetrica's header files."
    exit 1
fi
