#!/bin/sh

#   Copyright 2006 Jason K. Jackson
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

gcc_present=`gcc --version | grep GCC`
if [ "$gcc_present" = "" ]; then
  echo "Install XCode and rerun!"
  echo "  Find XCode on the developer's CD or from Apple's dev website."
  exit
fi

backup_profile="/etc/profile_pre_installer"
if [ -f $backup_profile ]; then
  echo "Backup profile exists!"
  echo "  Skipping path addition."
else
  # Add darwinports, local builds and mysql to path
  echo 'PATH="/opt/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/mysql/bin:$PATH"' >> /etc/profile
  echo 'export PATH' >> /etc/profile
fi

# Reload paths
source /etc/profile

cd
mkdir src
cd src

READLINE=readline-5.1.tar.gz
curl -O ftp://ftp.gnu.org/gnu/readline/$READLINE
if [ -f $READLINE ]; then
  tar xzvf $READLINE
  cd readline-5.1
  ./configure --prefix=/usr/local
  make
  make install
  cd ..
else
  echo "Could not download $READLINE. Exiting..."
  exit
fi

RUBY=ruby-1.8.4.tar.gz
RUBYDIR=ruby-1.8.4
curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/$RUBY
tar xzvf $RUBY 
cd $RUBYDIR
./configure --prefix=/usr/local --enable-pthread --with-readline-dir=/usr/local
make
make install
cd ..

RUBYGEMS=rubygems-0.9.0.tgz
RUBYGEMSDIR=rubygems-0.9.0
curl -O  http://rubyforge.iasi.roedu.net/files/rubygems/$RUBYGEMS
tar xzvf $RUBYGEMS
cd $RUBYGEMSDIR
/usr/local/bin/ruby setup.rb
cd ..

gem install rails capistrano capistrano-ext --include-dependencies

# Uncomment if you want fcgi

#curl -O http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
#tar xzvf fcgi-2.4.0.tar.gz
#cd fcgi-2.4.0
#./configure --prefix=/usr/local
#make
#make install
#cd ..

#curl -O http://sugi.nemui.org/pub/ruby/fcgi/ruby-fcgi-0.8.6.tar.gz
#tar xzvf ruby-fcgi-0.8.6.tar.gz
#cd ruby-fcgi-0.8.6
#/usr/local/bin/ruby install.rb config --prefix=/usr/local
#/usr/local/bin/ruby install.rb setup
#/usr/local/bin/ruby install.rb install
#cd ..

#gem install fcgi

PCRE=pcre-6.6.tar.gz
PCREDIR=pcre-6.6
curl -O ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/$PCRE
tar xzvf $PCRE
cd $PCREDIR
./configure --prefix=/usr/local CFLAGS=-O1
make
make install
cd ..

#curl -O http://www.lighttpd.net/assets/2007/2/23/lighttpd-1.5.0-r1691.tar.gz
#tar xzvf lighttpd-1.4.11.tar.gz
#cd lighttpd-1.4.11
#./configure --prefix=/usr/local --with-pcre=/usr/local
#make
#make install
#cd ..

cd ~/Desktop

curl -O http://svn.macports.org/repository/macports/downloads/DarwinPorts-1.3.1/DarwinPorts-1.3.1-10.4.dmg
hdiutil attach DarwinPorts-1.3.1-10.4.dmg
cd /Volumes/DarwinPorts-1.3.1/
installer -pkg DarwinPorts-1.3.1.pkg -target "/"
sleep 2
cd ~/Desktop
hdiutil detach /Volumes/DarwinPorts-1.3.1/
rm -f DarwinPorts-1.3.1-10.4.dmg

port -d selfupdate
port install graphicsmagick
port install wget
gem install -y rmagick

gem install -y acts_as_taggable termios pdf-writer mechanize tzinfo mongrel

echo "Installing MySQL 5.0"

wget http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-standard-5.0.27-osx10.4-i686.dmg/from/http://mysql.he.net/
hdiutil attach mysql-standard-5.0.27-osx10.4-i686.dmg
cd /Volumes/mysql-standard-5.0.27-osx10.4-i686/
installer -pkg mysql-standard-5.0.27-osx10.4-i686.pkg -target "/"

installer -pkg MySQLStartupItem.pkg -target "/"
cp -r MySQL.prefPane /Library/PreferencePanes/
sleep 2
cd ~/Desktop
hdiutil detach /Volumes/mysql-standard-5.0.27-osx10.4-i686/
rm -f mysql-standard-5.0.27-osx10.4-i686.dmg

curl -O http://cocoamysql.sourceforge.net/beta/CocoaMySQL_0.7b3.zip
unzip CocoaMySQL_0.7b3.zip
cp -r CocoaMySQL.app /Applications/
rm -rf CocoaMySQL.app
rm -f CocoaMySQL_0.7b3.zip

curl -O http://metissian.com/downloads/macosx/subversion/subversion-client-1.3.1.dmg
hdiutil attach subversion-client-1.3.1.dmg
cd /Volumes/Subversion\ Client\ 1.3.1/
installer -pkg SubversionClient-1.3.1.pkg -target "/"
sleep 2
cd ~/Desktop
hdiutil detach /Volumes/Subversion\ Client\ 1.3.1/
rm -f subversion-client-1.3.1.dmg

echo "1" | gem install rcov

echo "Attempting to auto-select MySQL-2.7 for MySQL gem"
echo "2" | gem install mysql -- --with-mysql-dir=/usr/local/mysql

echo "Installation completed successfully."
echo "  Please restart your system to finalize the installation."

