#!/bin/bash

# TODO
# - remove excess Makefiles's, etc.
# - debug/release command-line arguments
# - install directly to ext/

START_DIR=`pwd`
INC_DEST="$START_DIR/../inc"
LIB_DEST="$START_DIR/../lib"

echo "#####################################"
echo "# Delta3D Dependencies Build Script #"
echo "# \"Almost As Good As a SRPM (C)\"    #"
echo "#                                   #"
echo "# Version 1.0.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' :)

#-----------------------------
# Boost 1.32
#-----------------------------

# 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.

cd $START_DIR/boost_1_32_0/libs/python/build
bjam -sTOOLS=gcc

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

cd $START_DIR/cal3d-0.10.0
./autogen.sh 
./configure --prefix=`pwd` --enable-rtti 
make 
cp -d src/cal3d/.libs/libcal3d.so.11.0.0 $LIB_DEST 
cp -d src/cal3d/.libs/libcal3d.so.11 $LIB_DEST 
cp -d src/cal3d/.libs/libcal3d.so $LIB_DEST 
mkdir $INC_DEST/cal3d 
cp src/cal3d/*.h $INC_DEST/cal3d 
echo "...done"

#-----------------------------
# FLTK 1.1.6
#-----------------------------
echo "Building FLTK..."

cd $START_DIR/fltk-1.1.6 
./configure --enable-shared --enable-threads --prefix=`pwd` 
#./configure --enable-shared --enable-threads --prefix=/current_directory --enable-debug
make 
cp -d src/libfltk.so.1.1 $LIB_DEST 
cp -d src/libfltk.so $LIB_DEST 
cp -r FL $INC_DEST 
echo "...done"

#-----------------------------
# GDAL 1.2.6 
#-----------------------------
echo "Building GDAL..."

cd $START_DIR/gdal-1.2.6 
./configure --prefix=`pwd` 
make 
PATH="$PATH:$START_DIR/gdal-1.2.6/apps"
cp -d .libs/libgdal.so* $LIB_DEST 
cp alg/*.h $INC_DEST 
cp gcore/*.h $INC_DEST 
cp ogr/*.h $INC_DEST 
cp port/*.h $INC_DEST 
echo "...done"

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

cd $START_DIR/isense-3.83-beta
cp isense.h $INC_DEST 
cp types.h $INC_DEST 
cp -d Linux/x86/libisense.so $LIB_DEST 
echo "...done"

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

# Note: ODE needs certain config settings. These can be configured by 
# 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. I've made the changes already into the
# user-settings included with this distribution.

echo "Building ODE..."

cd $START_DIR/ode-0.5 
make 
cp lib/libode.a $LIB_DEST 
cp -r include/ode $INC_DEST 
# hack alert! boost_python needs these "hidden" headers to compile
# the physical bindings
mkdir $INC_DEST/ode/src 
cp ode/src/*.h $INC_DEST/ode/src 
echo "...done"

#-----------------------------
# OpenAL 1.0 
#-----------------------------
echo "Building OpenAL..."

cd $START_DIR/openal/linux 
./autogen.sh 
./configure --prefix=`pwd` --enable-optimization 
#./configure --prefix=/current_directory --enable-debug
make 
cp -r include/AL $INC_DEST 
cp $START_DIR/openal/include/AL/*.h $INC_DEST/AL
cp -d src/libopenal.so.0.0.7 $LIB_DEST 
cd $LIB_DEST 
ln -sf libopenal.so.0.0.7 libopenal.so 
ln -sf libopenal.so.0.0.7 libopenal.so.0 
echo "...done"

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

cd $START_DIR/OSG_OP_OT-0.9.9/OpenThreads 
make 
cp -d pthread_src/Linux32.Opt/libOpenThreads.so $LIB_DEST 
cp -r include/OpenThreads $INC_DEST 
echo "...done"

#-----------------------------
# Producer
#-----------------------------
echo "Building Producer..."
export OPENTHREADS_INC_DIR="$START_DIR/OSG_OP_OT-0.9.9/OpenThreads/include"
export OPENTHREADS_LIB_DIR="$START_DIR/OSG_OP_OT-0.9.9/OpenThreads/lib/Linux32"

cd $START_DIR/OSG_OP_OT-0.9.9/Producer 
make 
cp -d src/Linux32.Opt/libProducer.so $LIB_DEST 
cp -r include/Producer $INC_DEST 
echo "...done"

#-----------------------------
# OpenSceneGraph 0.9.9 
#-----------------------------
echo "Building OpenSceneGraph..."
export PRODUCER_INC_DIR="$START_DIR/OSG_OP_OT-0.9.9/Producer/include"
export PRODUCER_LIB_DIR="$START_DIR/OSG_OP_OT-0.9.9/Producer/lib/Linux32"

cd $START_DIR/OSG_OP_OT-0.9.9/OpenSceneGraph 
make 
cd src/osgTerrain
make GDAL_LIBS=$START_DIR/../lib/libgdal.so
cd ../..
cp lib/Linux32/*.so $LIB_DEST 
mkdir $LIB_DEST/osgPlugins 
cp lib/Linux32/osgPlugins/* $LIB_DEST/osgPlugins 
cp -r include/* $INC_DEST 
echo "...done"

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

cd $START_DIR/plib-1.8.4 
./configure --prefix=`pwd` 
make 
cp src/js/libplibjs.a $LIB_DEST 
cp src/util/libplibul.a $LIB_DEST 
cp src/js/js.h $INC_DEST 
cp src/util/ul.h $INC_DEST 
echo "...done"

#-----------------------------
# ReplicantBody (2005-09-28)
#-----------------------------
echo "Building ReplicantBody..."
PKG_CONFIG_PATH="$START_DIR/cal3d"

cd $START_DIR/replicantbody 
./autogen.sh --am automake-1.7 --al aclocal-1.7 --prefix=`pwd` CPPFLAGS=-I$INC_DEST LDFLAGS=-L$LIB_DEST 
make 
make install 
cp -r include/rcfg $INC_DEST 
cp -r include/rvrutils $INC_DEST 
cp -r include/rbody $INC_DEST 
cp -d lib/*.so* $LIB_DEST 
rm -f $LIB_DEST/*osgdb_char.*
find $INC_DEST -type f -iname Makefile.am -exec rm -f {} \;
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 
#./runConfigure -plinux -cgcc -xg++ -d -minmem -nsocket -tnative -rpthread # debug
make 
cd $XERCESCROOT
cp -r include/xercesc $INC_DEST 
cp -d lib/libxerces-c.so* $LIB_DEST 
echo "...done"

#-----------------------------
# CEGUI 0.4.0
#-----------------------------
echo "Building CEGUI..."

cd $START_DIR/cegui_mk2
./configure --prefix=`pwd` --disable-irrlicht-renderer --without-devil --without-ogre-renderer CPPFLAGS=-I$INC_DEST LDFLAGS=-L$LIB_DEST 
make 
make install
cp -r include/CEGUI $INC_DEST
cp -d lib/*.so* $LIB_DEST
rm -f $LIB_DEST/libCEGUILua*
rm -f $LIB_DEST/libCEGUIlua*
rm -f $LIB_DEST/libCEGUISample*
echo "...done"

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

cd $START_DIR/HawkNL1.68
make -f makefile.linux
cp src/libNL.a $LIB_DEST
cp include/nl.h $INC_DEST

#----------------------------
# GNE
#----------------------------

# Note: the file LibPaths.mak must be edited to let GNE
# find you installation of Boost and HawkNL. I have given it
# defaults for this directory structure.

echo "Building GNE..."

cd $START_DIR/gnelib-0.70
./fixlinux.sh
make HAWKNL_INCLUDE_PATH=../../inc HAWKNL_LIBRARY_PATH=../../lib BOOST_INCLUDE_PATH=$START_DIR/boost_1_32_0
cp lib/linux/libgne.a $LIB_DEST
cp -r include/gnelib $INC_DEST
cp include/gnelib.h $INC_DEST

cd $START_DIR

