# HG changeset patch
# User J. H. Palmieri <palmieri@math.washington.edu>
# Date 1287624877 25200
# Node ID cc68f144611ece3768c27e0cf71cdb01590f1fa1
# Parent  1c20d7c674d6d841db63c877a8c5f07c735cf9ed
#6235: use matplotlib config dir whose name depends on the version of matplotlib

diff -r 1c20d7c674d6 -r cc68f144611e sage-env
--- a/sage-env
+++ b/sage-env
@@ -182,6 +182,30 @@ if [ -z "$SAGE_TESTDIR" ]; then
     SAGE_TESTDIR="$DOT_SAGE"/tmp && export SAGE_TESTDIR
 fi
 
+# Use a matplotlib config directory specific to Sage and specific to
+# the version number of matplotlib, by setting the environment
+# variable MPLCONFIGDIR.  See
+# http://trac.sagemath.org/sage_trac/ticket/6235 and
+# http://trac.sagemath.org/sage_trac/ticket/9221.  Find the version
+# number for matplotlib by reading the file
+# SAGE_LOCAL/lib/python/site-packages/matplotlib/__init__.py, and use
+# the version number in the name of the directory.  If that file
+# doesn't exist, matplotlib hasn't been installed yet, so don't set
+# MPLCONFIGDIR.
+MPLINITFILE="$SAGE_LOCAL/lib/python/site-packages/matplotlib/__init__.py"
+if [ -f "$MPLINITFILE" ]; then
+    MPLVERSION=`sed -n "/^__version__[ ]*=/s/[^']*'\([^']*\)'.*$/\1/p" "$MPLINITFILE"`
+    if [ "$MPLVERSION" != "" ]; then
+	MPLCONFIGDIR="$DOT_SAGE/matplotlib-$MPLVERSION"
+    else
+	# MPLVERSION is empty, so no version found.
+	MPLCONFIGDIR="$DOT_SAGE/matplotlib"
+    fi
+    export MPLCONFIGDIR
+    # Create the directory, because matplotlib doesn't seem to be happy otherwise.
+    mkdir -p "$MPLCONFIGDIR"
+fi
+
 LD_LIBRARY_PATH="$SAGE_ROOT/local/lib/:$LD_LIBRARY_PATH" && export LD_LIBRARY_PATH
 # The following is needed for openmpi:
 LD_LIBRARY_PATH="$SAGE_ROOT/local/lib/openmpi:$LD_LIBRARY_PATH" && export LD_LIBRARY_PATH
