#!/bin/bash
# Author: Yentl Van Tendeloo
# Installs a python interpreter with necessary modules to get python-igraph version 0.6.5.
# All files are installed in the ~/AToMPM folder, though Python itself likely installs some files in ~/.local too.
# These additional files can easily be removed using tools such as pip.
# Either run this script with the 'source' tool, or invoke 'source run_python-igraph.sh' seperately afterwards.

set -e
current_dir=`pwd`

if [ ! -d "$HOME/AToMPM/" ]; then
    # Install Python with development headers
    cd ~
    mkdir AToMPM
    cd AToMPM
    # Install zlib seperately
    wget http://zlib.net/zlib-1.2.8.tar.gz
    tar -xvzf zlib-1.2.8.tar.gz
    cd zlib-1.2.8
    ./configure --prefix=$HOME/AToMPM/localPython
    make
    make install
    cd ..
    wget --no-check-certificate https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
    tar -xvzf Python-2.7.8.tgz
    cd Python-2.7.8
    ./configure --prefix=$HOME/AToMPM/localPython
    sed -i "s/^#zlib/zlib/g" Modules/Setup
    make -j2
    make install
    export PATH=$HOME/AToMPM/localPython/bin:$PATH
    cd ..

    # Remove previous builds
    rm -rf python-igraph || true
    mkdir python-igraph
    cd python-igraph

    # Install igraph C core
    wget http://downloads.sourceforge.net/project/igraph/C%20library/0.6.5/igraph-0.6.5.tar.gz
    tar -xvzf igraph-0.6.5.tar.gz
    cd igraph-0.6.5
    ./configure
    make -j2

    # Install python-igraph binding
    wget --no-check-certificate https://pypi.python.org/packages/source/p/python-igraph/python-igraph-0.6.5.tar.gz
    tar -xvzf python-igraph-0.6.5.tar.gz
    cd python-igraph-0.6.5
    rm setup.cfg
    echo "[build_ext]" > setup.cfg
    echo "include_dirs = ../../../localPython/include/python2.7:../../build/include:../include:../../include:/usr/local/include:/usr/include" >> setup.cfg
    echo "library_dirs = ../../../localPython/lib:../../build/src/.libs:../lib/:../src/.libs:../../src/.libs:/usr/local/lib:/usr/lib" >> setup.cfg
    echo "" >> setup.cfg
    echo "[egg_info]" >> setup.cfg
    echo "tag_build = " >> setup.cfg
    echo "tag_date = 0" >> setup.cfg
    echo "tag_svn_revision = 0" >> setup.cfg
    python setup.py install --user
    cd "$current_dir"
fi

# Set PATHs correctly
export PATH=$HOME/AToMPM/localPython/bin:$PATH
export LD_LIBRARY_PATH="$HOME/AToMPM/python-igraph/igraph-0.6.5/src/.libs/:$HOME/AToMPM/localPython/lib"

python mt/main.py
