#/bin/bash
set -eu

rm -rf src deps 

#export C_INCLUDE_PATH="$C_INCLUDE_PATH:/usr/include/c++/4.2.1"
export GIT_SSL_NO_VERIFY=true 
export CC="gcc -fPIC -std=c99 -I/usr/local/gfortran/include/c++/4.6.2 -I/usr/local/gfortran/include/c++/4.6.2/x86_64-apple-darwin11/"
export CXX="g++ -fPIC -std=c99 -I/usr/local/gfortran/include/c++/4.6.2 -I/usr/local/gfortran/include/c++/4.6.2/x86_64-apple-darwin11/"
export F77="gfortran -fPIC -I/usr/local/gfortran/include/c++/4.6.2 -I/usr/local/gfortran/include/c++/4.6.2/x86_64-apple-darwin11/"
export FC="gfortran -fPIC -I/usr/local/gfortran/include/c++/4.6.2 -I/usr/local/gfortran/include/c++/4.6.2/x86_64-apple-darwin11/"
export FFLAGS="-ff2c -I/usr/local/gfortran/include/c++/4.6.2 -I/usr/local/gfortran/include/c++/4.6.2/x86_64-apple-darwin11/"
export ARCHFLAGS="-I/usr/local/gfortran/include/c++/4.6.2 -I/usr/local/gfortran/include/c++/4.6.2/x86_64-apple-darwin11/"

git clone https://github.com/matplotlib/matplotlib
mv matplotlib src
mkdir deps
cd src

#only try this if the classic python setup.py build approach does not work. The approach in the next 3 lines can 
#trigger issues of permissions with freetype. Sometimes it is better to independently install freetype2 (from the 
#issm externalpackages) as sudo (or root), so that the python script can detect its existence automatically.
#sudo make -f make.osx PREFIX=$ISSM_DIR/externalpackages/matplotlib/deps PYVERSION=$pythonversion fetch deps mpl_install_std
#sudo make -f make.osx PREFIX=$ISSM_DIR/externalpackages/matplotlib/deps PYVERSION=$pythonversion mpl_install_std
#python -c "import matplotlib; print 'Installed matplotlib', matplotlib.__version__, matplotlib.__file__"

#comments: try exporting this first before calling python setup.py build
CFLAGS=-mmacosx-version-min=10.6

#to be tried:  first get freetype and zlib and libpng installed in sudo mode
python setup.py build
python setup.py install
