#!/bin/bash

### This script sets up a Wellesley public Mac to run the OpenGL programs
### for CS307, the graphics course.

### This version, Jan 2012, is targeting Python 2.7, as the smallest
### change from my previous offering of the course.  The Macs also have
### Python 3.2, so that's worth considering, too.

pyversion="2.7"

top='/Volumes/TEMPORARY SAVE FOLDER/'
if [ -e "$top" ]; then
    echo "exists: $top"
    cd "$top"
    if [ -d "Users" ]; then
        echo "Users subdir exists"
        cd Users
        top=`pwd`
        if [ -d "$USER" ]; then
            echo "My $USER subdir exists"
            cd "$USER"
            top=`pwd`
        fi
    fi
else
    echo "missing: $top"
    top=$HOME/test1
    mkdir "$top"
    echo "using $top"
fi

# this same directory goes in ~/.pydistutils, see below
sitedir="Library/Python/$pyversion/site_packages"
site="$top/$sitedir"

cd "$top"
pwd
if [ ! -d "$sitedir" ]; then
    mkdir -p "$sitedir"
fi
if [ ! -d 307 ]; then 
    mkdir 307
fi
cd 307
pwd

echo "Setting up easy_install to $site"

### Is python installed?  This would be a pretty broken machine if it's
### not, but it can hurt to check.

echo "Check whether Python $pyversion is installed"
which python$pyversion > /dev/null
if [ $? -ne 0 ]; then
    echo "Python $pyversion does not seem to be installed.  Please install it."
    exit
else
    echo "...it is"
fi

### The following steps come from
### www.mpe.mpg.ed/~williams/mac/python.html, which explains how to
### install python libraries on OS X without administrator privileges

# Make this idempotent by deleting any existing pydisutils

pydist=~/.pydistutils.cfg
echo "Create $pydist file, to do non-admin installations using easy_install"

if [ -e $pydist ]; then
    rm $pydist
fi


# Note that the literal $ has to be there, according to
# peak.telecommunity.com/DevCenter/EasyInstall#mac-os-x-user-installation

cat > $pydist <<EOF
[install]
install_lib = $top/Library/Python/\$py_version_short/site_packages
install_scripts = ~/bin
EOF

echo "Here is $pydist"
cat $pydist

echo "Make ~/bin, if necessary"
if [ ! -d ~/bin ]; then 
    mkdir ~/bin
fi

bashrc=~/.bashrc
if [ ! -e $bashrc ]; then
    touch $bashrc
fi

grep -q "added ~/bin to path" $bashrc
if [ $? -ne 0 ]; then
    # quote the EOF so that $PATH appears literally in the file
    cat >> $bashrc <<'EOF'
export PATH=~/bin:$PATH
# added ~/bin to path
EOF
fi

export PATH=~/bin:$PATH

grep -q "added stuff to PYTHONPATH" $bashrc
if [ $? -ne 0 ]; then
    echo "adding stuff to PYTHONPATH"
    cat >> $bashrc <<EOF
export PYTHONPATH="$site":"$top/307/pytw":\$PYTHONPATH
# added stuff to PYTHONPATH
EOF
fi

# putting python2.7 in ~/bin, which is first on the path, will mean that's
# the dfault when python runs, which I hope will mean that's where
# easy_install will install to.
echo "put python$pyversion first on path"
# need an absolute pathname for the symlink to work, so look it up
pyver=`which python$pyversion`
ln -s $pyver ~/bin/python

export PYTHONPATH="$site":"$top/307/pytw":\$PYTHONPATH

echo $PYTHONPATH

echo -n "Install the easy_install egg to "
pwd
egg=setuptools-0.6c11-py2.7.egg
if [ ! -e $egg ]; then
    echo "download egg"
    curl -O pypi.python.org/packages/2.7/s/setuptools/$egg 
    ls -l $egg
fi

echo "executing egg"
sh $egg

echo "Install PyOpenGL"
easy_install-2.7 PyOpenGL

# the easy_install just installs the .egg, instead of separate .py files
# that a student can run.

# easy_install-2.7 PyOpenGL-Demo

# Can't install PIL without gcc
# easy_install-2.7 PIL

# to make re-install easier
pushd ~/bin
curl -O http://cs.wellesley.edu/~cs307/reinstall-pytw.sh
chmod a+rx reinstall-pytw.sh
popd

reinstall-pytw.sh



