#!/bin/sh

# WARNING: USE AT YOUR OWN RISK!
# It is recommended to use this script as a guide
#
# This is tested on a Ubuntu 8.10 Live Session
# get it at http://www.ubuntu.com/getubuntu/download
#
# To run this script, paste the following line in a terminal:
# wget http://einrad.eu/stuff/wikipedia-iphone; sh wikipedia-iphone
# (you need an internet connection)
#
# if everything works, download the latest dump in your language
# and rerun the last lines starting at the process command
# hint: change the lang-variable
# 
# Last change: 20 Nov 2008
# contact: felix - beerleader - net

lang="ii"


# adding universe repository to sources.list list
echo 'deb http://archive.ubuntu.com/ubuntu/ intrepid universe' | sudo tee -a /etc/apt/sources.list
sudo apt-get update

# install needed packages
sudo apt-get install build-essential automake autoconf libbz2-dev ncurses-dev zsh subversion ruby1.9 ruby1.9-dev rdoc1.9 rubygems1.9 -y

# removing last line from sources.list
sudo mv /etc/apt/sources.list /etc/apt/sources.list.old
sudo head -n -1 /etc/apt/sources.list.old | sudo tee /etc/apt/sources.list
sudo rm -rf /etc/apt/sources.list.old
sudo apt-get update

# fix: link ruby1.9 to ruby
sudo ln -s /usr/bin/ruby1.9 /usr/bin/ruby

# install needed Ruby gems
sudo gem install RubyInline

# create working directory in your home
cd /home/$USER
mkdir wikipedia-iphone-dump
cd wikipedia-iphone-dump

# get latest wikipedia-iphone source from SVN
svn checkout http://wikipedia-iphone.googlecode.com/svn/trunk/ wikipedia-iphone-read-only
mv wikipedia-iphone-read-only/* .
rm -rf wikipedia-iphone-read-only

# get findutils and mklocatedb
wget http://einrad.eu/stuff/wikipedia-iphone-tools.tar.bz2
tar xfj wikipedia-iphone-tools.tar.bz2
rm wikipedia-iphone-tools.tar.bz2

cd c

# change a line in c.indexer that it can be compiled
mv indexer.c indexer.c.old
cat indexer.c.old | head -n 29 > indexer.c
cat indexer.c.old | tail -n +31 >> indexer.c
rm indexer.c.old

# make
./bootstrap.sh
./configure
make


cd ..

# downloading the wikipedia-dump
mkdir dump
cd dump
wget http://download.wikimedia.org/"$lang"wiki/latest/"$lang"wiki-latest-pages-articles.xml.bz2

cd ../sh


# process the dump
time ./process /home/$USER/wikipedia-iphone-dump/dump/"$lang"wiki-latest-pages-articles.xml.bz2
cd ../dump
rm -rf *.index.txt*
mv *.blocks.db blocks.db
mv *.locate.db locate.db
mv *.locate.prefixdb locate.prefixdb
mv *.processed processed

nautilus /home/$USER/wikipedia-iphone-dump/dump

