#!/bin/bash

START_DIR=`pwd`

echo "#####################################"
echo "# Delta3D Dependencies Build Script #"
echo "# \"Almost As Good As a SRPM (C)\"    #"
echo "#                                   #"
echo "# Version 1.2.0                     #"
echo "#####################################"

# NOTE: This is UNTESTED and is only here for your convenience.
# We've used this in some shape or form on Ubuntu and Fedore Core.
# We really want to move to package managers for all this stuff,
# so please if your distributions repos aren't up to date, bug them!
# We are looking for places to host Delta3D and its dependencies.
# For Fedora Core 4, most of these depedencies are available on
# newrpms.sunsite.dk thanks to Rudolf Kastl, look for Delta3D
# on there as well soon: 'yum install delta3d' :)

# Pay particular attention to the comments along the way...

# Dependencies
# ------------
# tools:
# - bjam
# - cmake
# - autotools (aclocal, automake, autoconf)
# libs: 
# - libfreetype
# - libjpeg 
# - libpng 
# - libtiff 
# - libgif
# - libz
# - python-devel

#-----------------------------
# Boost 1.33.1
#-----------------------------

# NOTE: Make sure the following environment variables are set 
# prior to running this! Common settings are outlined below:
# PYTHON_ROOT=/usr
# PYTHON_VERSION=2.4

# You'll probably want to do a system-wide installation of the
# the full Boost package as well. Good stuff. But at least
# get the libs and headers for Boost Python into a system-wide 
# spot.
echo "Building Boost..."

cd $START_DIR/boost_1_33_1
bjam -sTOOLS=gcc
bjam install

# Ug, sorry about this, but every library that uses Boost expects
# the subdirectory to be called 'boost' :(
cd /usr/local/include
mv boost-1_33_1/boost .
rmdir boost-1_33_1

echo "...done"

#-----------------------------
# CAL3D 0.10.0
#-----------------------------
echo "Building CAL3D..."

cd $START_DIR/cal3d-0.10.0
./autogen.sh 
./configure --enable-rtti 
make 
make install

echo "...done"

#-----------------------------
# CppUnit 1.10.2
#-----------------------------
echo "Building CppUnit..."
cd $START_DIR/cppunit-1.10.2
./configure
make
make install

echo "...done"

#-----------------------------
# e2fsprogs 1.38
#-----------------------------
echo "Building e2fsprogs..."

cd $START_DIR/e2fsprogs-1.38
./configure
make
make install-libs

echo "...done"

#-----------------------------
# FLTK 1.1.7
#-----------------------------
echo "Building FLTK..."

cd $START_DIR/fltk-1.1.7
./configure --enable-shared --enable-threads 
make 
make install

echo "...done"

#-----------------------------
# GDAL 1.3.1
#-----------------------------
echo "Building GDAL..."

cd $START_DIR/gdal-1.3.1
./configure 
make 
make install

echo "...done"

#-------------------------------
# InterSense Interface 3.83
#-------------------------------
echo "Installing InterSense..."

cd $START_DIR/isense-3.83
cp isense.h /usr/local/include
cp types.h /usr/local/include
cp -d Linux/x86/libisense.so /usr/local/lib

echo "...done"

#-----------------------------
# ODE 0.5
#-----------------------------

# NOTE: ODE needs certain config settings. These can be configured by man 
# editing the file ode-0.5/config/user-settings. Precision needs to
# be set to SINGLE, and the OPCODE line needs to be uncommented so it 
# will compile that into ODE. The patch applied below does this for you.

echo "Building ODE..."

cd $START_DIR/ode-0.5 
patch -p2 <../ode-0.5-dt-1.2.0.patch
make 
cp -r include/ode /usr/local/include
cp lib/libode.a /usr/local/lib

echo "...done"

#-----------------------------
# OpenThreads 1.4.2
#-----------------------------
echo "Building OpenThreads..."

cd $START_DIR/OSG_OP_OT-1.0/OpenThreads 
make 
make install

echo "...done"

#-----------------------------
# Producer 1.0.1
#-----------------------------
echo "Building Producer..."

export OPENTHREADS_INC_DIR="/usr/local/include"
export OPENTHREADS_LIB_DIR="/usr/local/lib"

cd $START_DIR/OSG_OP_OT-1.0/Producer
make 
make install

echo "...done"

#-----------------------------
# OpenSceneGraph 1.0.0
#-----------------------------

# NOTE: Make sure GDAL is installed properly first as well
# as libfreetype, libjpeg, libpng, libtiff, libungif, libz

echo "Building OpenSceneGraph..."
export PRODUCER_INC_DIR="/usr/local/include"
export PRODUCER_LIB_DIR="/usr/local/lib"

cd $START_DIR/OSG_OP_OT-1.0/OpenSceneGraph 
make 
make install

echo "...done"

#-----------------------------
# OpenAL 0.0.8 (spec 1.1)
#-----------------------------
echo "Building OpenAL..."

# NOTE: The autotools build system for OpenAL and ALUT is broken.
# Luckily the CMake one works. So grab a CMake binary somewhere...

cd $START_DIR/openal-0.0.8
cmake . -DCMAKE_INSTALL_PREFIX:STRING="/usr/local" -DCMAKE_C_FLAGS:STRING="-O2"
make 
make install

echo "...done"

#-----------------------------
# FreeALUT 1.0.0
#-----------------------------
OPENAL_PREFIX=/usr/local

export CPPFLAGS="-I${OPENAL_PREFIX}/inc"
export LDFLAGS="-L${OPENAL_PREFIX}/lib"

cd $START_DIR/freealut-1.0.0
cmake . -DCMAKE_INSTALL_PREFIX:STRING="/usr/local" -DCMAKE_C_FLAGS:STRING="-O2"
make
make install

echo "...done"

#-----------------------------
# PLIB 1.8.4
#-----------------------------
echo "Building PLIB..."

cd $START_DIR/plib-1.8.4 
./configure 
make 
make install

echo "...done"

#-----------------------------
# ReplicantBody (2006-01-17)
#-----------------------------
echo "Building ReplicantBody..."
PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"

cd $START_DIR/replicantbody 
./autogen.sh
./configure --prefix="/usr/local"
make 
make install 

# NOTE: the install target seems to be a bit flaky.
# You may have to copy the include directories
# manually to /usr/local/include
cp -r include/rbody /usr/local/include
cp -r include/rvrutils /usr/local/include
cp -r include/rcfg /usr/local/include

echo "...done"

#-----------------------------
# Xerces 2.6.0
#---------------------- -------
echo "Building Xerces..."
export XERCESCROOT=$START_DIR/xerces-c-src_2_6_0

cd $XERCESCROOT/src/xercesc 
autoconf 
./runConfigure -plinux -cgcc -xg++ -minmem -nsocket -tnative -rpthread 
make 
make install

echo "...done"

#-----------------------------
# CEGUI 0.4.1
#-----------------------------
echo "Building CEGUI..."

cd $START_DIR/cegui_mk2
./configure --disable-irrlicht-renderer --without-devil --without-ogre-renderer
make
make install

echo "...done"

#-----------------------------
# HawkNL 1.68
#-----------------------------
echo "Building HawkNL..."

cd $START_DIR/HawkNL1.68
make -f makefile.linux
make -f makefile.linux install

# NOTE: HawkNL screws up the install and forget the
# following symlink. Don't ask me why they are using 
# non-lib prefixed libraries anyway...
pushd /usr/local/lib
ln -s libNL.so.1.6 NL.so.1.6
popd

echo "...done"

#----------------------------
# GNE 0.70
#----------------------------
echo "Building GNE..."

cd $START_DIR/gnelib-0.70
./fixlinux.sh
make
make install

echo "...done"

cd $START_DIR

#----------------------------
# Qt 4.0.1
#----------------------------
echo "Building Qt..."

./configure
make
make install

echo "...done"

