#! /bin/sh

# Removed older versions in the python site-packages builtin and in the python site-packages from python.org directories
PATHS=/Library/Python/2.6/site-packages/:/Library/Python/2.7/site-packages/:/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
for path in ${PATHS//:/ }; do
    if cd $path; then
        if [ -f pyo.py ]; then
            sudo rm pyo.py;
        fi    
        if [ -f pyo64.py ]; then
            sudo rm pyo64.py;
        fi    
        if [ -f pyo.pyc ]; then
            sudo rm pyo.pyc;
        fi    
        if [ -f pyo64.pyc ]; then
            sudo rm pyo64.pyc;
        fi    
        if [ -f _pyo.so ]; then
            sudo rm _pyo.so;
        fi    
        if [ -f _pyo64.so ]; then
            sudo rm _pyo64.so;
        fi    
        if [ -d pyolib ]; then
            sudo rm -rf pyolib/;
        fi    
        ls -1 pyo*-info > /dev/null 2>&1
        if [ "$?" = "0" ]; then
            sudo rm pyo*-info;
        fi    
    fi
done

# Install pyo in the python site-packages builtin directories
if cd /Library/Python/2.7/site-packages/; then
    sudo cp -r /tmp/python27/* .
else
    sudo mkdir -p /Library/Python/2.7/site-packages/
    cd /Library/Python/2.7/site-packages/
    sudo cp -r /tmp/python27/* .
fi

if cd /Library/Python/2.6/site-packages/; then
    sudo cp -r /tmp/python26/* .
else
    sudo mkdir -p /Library/Python/2.6/site-packages/
    cd /Library/Python/2.6/site-packages/
    sudo cp -r /tmp/python26/* .
fi

# Install pyo in the python.org site-packages directories
if cd /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/; then  
    sudo cp -r /tmp/python26/* .
else
    sudo mkdir -p /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
    cd /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
    sudo cp -r /tmp/python26/* .
fi

if cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/; then  
    sudo cp -r /tmp/python27/* .
else
    sudo mkdir -p /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
    cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
    sudo cp -r /tmp/python27/* .
fi

# Check if anaconda is already installed and copy files to site-packages directory
if cd ~/anaconda/lib/python2.6/site-packages/; then  
    sudo cp -r /tmp/python26/* .
fi

if cd ~/anaconda/lib/python2.7/site-packages/; then  
    sudo cp -r /tmp/python27/* .
fi

sudo rm -rf /tmp/python2*

# Add /usr/local/lib in .bash_profile if not already done
searchString="/usr/local/lib"

if [ -f ~/.bash_profile ]; then
    if `cat ~/.bash_profile | grep "${searchString}" 1>/dev/null 2>&1`; then
        echo "path already in PATH variable";
    else
        echo "adding path to .bash_profile..."
        echo "export PATH=/usr/local/lib:/usr/local/bin:\$PATH" >> ~/.bash_profile;
    fi
else
    echo "creating .bash_profile and adding path to it..."
    echo "export PATH=/usr/local/lib:/usr/local/bin:\$PATH" > ~/.bash_profile;
fi
