#!/bin/sh
#
# Installation script for Python with readline, setuptools and PIL
# on Mac OS X 10.4 (Tiger).
# Only dependency is gcc (can be installed with XCode).
# Follows http://www.libgd.org/DOC_INSTALL_OSX for the PIL deps.
#

## Set the libraries versions for download
READLINE_VERSION=5.2
PYTHON_VERSION=2.4.4
ZLIB_VERSION=1.2.3
LIBPNG_VERSION=1.2.23
LIBJPEG_VERSION=6b
FREETYPE2_VERSION=2.3.5
IMAGING_VERSION=1.1.6

installsteps () {
    ## Comment out the ones you want to skip
    Python
    PILDeps
    PIL
    ez_setup
}

## use wget if available (just as an exercise, it's not that needed after all)
[ `command -v wget >/dev/null` ] && alias dl='wget' || alias dl='curl -O'

## Set the installation path (destination)
PREFIX=`pwd`

## Create a folder for the downloads and their expanded archives
[ ! -d install_files ] && mkdir install_files
cd install_files

## Define some useful commands
removeFile () {
    # file path must be provided as func param.
    [ -e $1 ] && \
        ( rm -rf $1 && echo $1 was removed. ) || \
        echo $1 did not exist.
}

start () {
    echo "Starting to install $PACKAGE (version: $VERSION)."
}

end () {
    echo "Finished installing $PACKAGE."; echo
    unset PACKAGE VERSION INSTALL TARBALL BASEURL
}

prepareInstall () {
    start
    [ ! -e $TARBALL ] && \
        ( echo -n "Downloading tarball: $BASEURL/$TARBALL... "
          dl $BASEURL/$TARBALL &>/dev/null && echo Done. || \
          echo Failed. )
    [ -d $INSTALL ] && \
        ( echo -n "Removing existing folder: $INSTALL... "
          rm -rf $INSTALL && echo Done. )
    [ -e $TARBALL ] && \
        ( echo -n "Unpacking tarball: $TARBALL... "
          tar xzvf $TARBALL &>/dev/null && echo Done. )
    cd $INSTALL
}

normalConfig () {
    echo -n "Configuring package... "
    ./configure --prefix=$PREFIX &>/dev/null && echo Done.
}

finalizeInstall () {
    echo -n "make... "; make &>/dev/null && echo Done. || Failed.
    echo -n "make install... "; make install &>/dev/null && \
        echo Done. || echo Failed.
    cd ..
    end
}

normalInstall() { prepareInstall; normalConfig; finalizeInstall; }

loglevelConfig () {
    echo -n "Configuring package... "
    ./configure --prefix=$PREFIX >/dev/null && echo Done.
}

finalizeLoglevelInstall () {
    echo -n "make... " && make >/dev/null && echo Done.
    echo -n "make install... " && make install >/dev/null && \
        echo Done.
    cd ..
    end
}

loglevelInstall() { prepareInstall; loglevelConfig; finalizeLoglevelInstall; }

## Install readline then Python
buildReadline () {
    PACKAGE="The GNU Readline Library"
    VERSION=$READLINE_VERSION
    INSTALL=readline-$VERSION
    TARBALL=$INSTALL.tar.gz
    BASEURL=ftp://ftp.cwru.edu/pub/bash
    normalInstall
    #loglevelInstall
}

buildPython () {
    PACKAGE=Python
    VERSION=$PYTHON_VERSION
    INSTALL=Python-$VERSION
    TARBALL=$INSTALL.tgz
    BASEURL=http://python.org/ftp/python/$VERSION
    normalInstall
    #loglevelInstall
    echo -n "Test readline support in Python... "
    if `$PREFIX/bin/python -c "import readline"`
        then echo OK.; RLINSTALLED=1
        else echo Failed.; fi
}

Python () { buildReadline; buildPython; }

## Install PIL Dependencies
buildZlib () {
    PACKAGE=zlib
    VERSION=$ZLIB_VERSION
    INSTALL=zlib-$VERSION
    TARBALL=$INSTALL.tar.gz
    BASEURL=http://www.zlib.net
    normalInstall
    #loglevelInstall
}

buildLibpng () {
    PACKAGE=libpng
    VERSION=$LIBPNG_VERSION
    INSTALL=libpng-$VERSION
    TARBALL=$INSTALL.tar.gz
    BASEURL=ftp://ftp.simplesystems.org/pub/png/src
    prepareInstall
    cp scripts/makefile.darwin Makefile
    tmp_var=`echo $PREFIX | sed 's/\//\\\\\\//g'`
    sed -e 's/^prefix=\/usr\/local/prefix='$tmp_var"/" \
        -e 's/zlib$/\.\./' \
        <Makefile>NewMakefile
    mv NewMakefile Makefile
    finalizeInstall
}

buildLibjpeg () {
    PACKAGE=libjpeg
    VERSION=$LIBJPEG_VERSION
    INSTALL=jpeg-$VERSION
    TARBALL=jpegsrc.v$VERSION.tar.gz
    BASEURL=ftp://ftp.uu.net/graphics/jpeg
    prepareInstall
    ln -s `which glibtool` ./libtool
    export MACOSX_DEPLOYMENT_TARGET=10.4
    echo -n "Configuring package... "
    ./configure --prefix=$PREFIX --enable-shared &>/dev/null && \
        echo Done.
    finalizeInstall
    #finalizeLoglevelInstall
    unset MACOSX_DEPLOYMENT_TARGET
}

buildFreetype2 () {
    PACKAGE=freetype2
    VERSION=$FREETYPE2_VERSION
    INSTALL=freetype-$VERSION
    TARBALL=$INSTALL.tar.gz
    BASEURL=http://download.savannah.gnu.org/releases/freetype
    prepareInstall
    key="#define TT_CONFIG_OPTION_BYTECODE_INTERPRETER"
    sed "s/.*$key.*/$key/" <include/freetype/config/ftoption.h>tmp_ftoption
    mv tmp_ftoption include/freetype/config/ftoption.h
    normalConfig
    finalizeInstall
    #finalizeLoglevelInstall
}

PILDeps () { buildZlib; buildLibpng; buildLibjpeg; buildFreetype2; }

## Install Python Imaging Library (PIL)
PIL_cleanup () {
    echo Removing Installed files if they exist:
    removeFile $PREFIX/lib/python2.4/site-packages/PIL
    removeFile $PREFIX/lib/python2.4/site-packages/PIL.pth
}

PIL () {
    PACKAGE="PIL, the Python Imaging Library"
    VERSION=$IMAGING_VERSION
    INSTALL=Imaging-$VERSION
    TARBALL=$INSTALL.tar.gz
    BASEURL=http://effbot.org/media/downloads
    #PIL_cleanup
    prepareInstall
    echo -n "Running library tests... "
    ( $PREFIX/bin/python setup.py build_ext -i &>/dev/null && \
          $PREFIX/bin/python selftest.py &>/dev/null ) && \
        ( echo All tests passed.; echo -n "Installing package... "
          $PREFIX/bin/python setup.py install &>/dev/null && \
              ( echo Done.; PILINSTALLED=1; end ) ) || \
        ( echo Failed.
          echo Some libraries seem to be missing, PIL was not installed. )
    cd ..
}

## Install setuptools and easy_install
ez_cleanup () {
    echo Removing Installed files if they exist:
    removeFile $PREFIX/bin/easy_install
    removeFile $PREFIX/bin/easy_install-2.4
    removeFile $PREFIX/lib/python2.4/site-packages/easy-install.pth
    removeFile $PREFIX/lib/python2.4/site-packages/setuptools-0.6c7-py2.4.egg
    removeFile $PREFIX/lib/python2.4/site-packages/setuptools.pth
    [ -e ez_setup.py ] && \
        ( echo -n "Removing old installer script... "
          rm ez_setup.py; echo Done. )
}

ez_setup () {
    PACKAGE="setuptools and easy_install"
    VERSION="install script"
    #ez_cleanup
    start
    [ ! -e ez_setup.py ] && \
    ( echo -n "Downloading installer script... "
      dl http://peak.telecommunity.com/dist/ez_setup.py &>/dev/null && \
          echo Done. || echo Failed. ) || \
        echo Installer script already downloaded.
    [ -e 'ez_setup.py' ] && \
        ( echo -n "Run installer script... "
          $PREFIX/bin/python ez_setup.py >/dev/null && \
              echo Done.; end || \
              echo Failed. )
}

installsteps

echo -n "Finished Python installation; "
echo -n "readline: "; [ RLINSTALLED ] && echo -n "yes; " || echo -n "no; "
echo -n "PIL: "; [ PILINSTALLED ] && echo "yes." || echo "no."


