#!/bin/bash

# install_dependencies.sh
# 
#
# Created by Kevin Jonas Young on 12/8/10.
# Copyright 2010 -. All rights reserved.

#########################################################################
#   Preparation
#########################################################################
# Determine the path of the dependency package
DEP_PACK_PATH="${BASH_SOURCE[0]}";
if([ -h "${DEP_PACK_PATH}" ]) then
  while([ -h "${DEP_PACK_PATH}" ]) do DEP_PACK_PATH=`readlink "${DEP_PACK_PATH}"`; done
fi
pushd . > /dev/null
cd `dirname ${DEP_PACK_PATH}` > /dev/null
DEP_PACK_PATH=`pwd`;
popd  > /dev/null

# Changes the install name of a library
function install_name_change() {
    local library_path_l="$1"
    local new_install_name_l="$2"
    
    # Change the install name of argument 1 to argument 2
    install_name_tool -id $new_install_name_l $library_path_l
}

# Changes the link name of a certain sub-dependency in a library
function link_name_change() {
    local library_path_l="$1"
    local filter_l="$(basename $2)"
    
    # From otool -L extract the line which matches the third argument
    local current_link_name_l="$(otool -L $library_path_l | awk '{ print $1 }' | egrep $filter_l)"
    
    # Append the dependency package path to the subdependency path
    local new_link_name_l="$DEP_PACK_PATH/$2"
    
    # Reset the link name current_link_name_l in argument 1 (a library) to argument no. 2
    install_name_tool -change $current_link_name_l $new_link_name_l $library_path_l
}

#########################################################################
#   Libraries
#########################################################################

# Lua library
echo "Lua: Working..."

LIBRARY_NAME="liblua.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/lib/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

echo "Lua: Done..."

#########################################################################

# Boost libraries
# Date-Time
echo "Boost Date-Time: Working..."

LIBRARY_NAME="libboost_date_time-mt.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/lib/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

echo "Boost Date-Time: Done..."

# Filesystem
echo "Boost Filesystem: Working..."

LIBRARY_NAME="libboost_filesystem-mt.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/lib/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

# Change link names of all subdependencies
# Subdependencies must always be relative to the dependency package root
SUBDEPS[0]="lib/libboost_system-mt.dylib"

for SUBDEP in ${SUBDEPS[@]}
do
    link_name_change $LIBRARY_PATH $SUBDEP
done

echo "Boost Filesystem: Done..."

# System
echo "Boost System: Working..."

LIBRARY_NAME="libboost_system-mt.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/lib/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

echo "Boost System: Done..."

# Thread
echo "Boost Thread: Working..."

LIBRARY_NAME="libboost_thread-mt.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/lib/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

echo "Boost Thread: Done..."

#########################################################################

# Ogg-Vorbis libraries
# Ogg
echo "Ogg: Working..."

LIBRARY_NAME="libogg.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/lib/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

echo "Ogg: Done..."

# Vorbis
echo "Vorbis: Working..."

LIBRARY_NAME="libvorbis.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/lib/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

# Change link names of all subdependencies
# Subdependencies must always be relative to the dependency package root
SUBDEPS[0]="lib/libogg.dylib"

for SUBDEP in ${SUBDEPS[@]}
do
    link_name_change $LIBRARY_PATH $SUBDEP
done

echo "Vorbis: Done..."

# Vorbisfile
echo "Vorbisfile: Working..."

LIBRARY_NAME="libvorbisfile.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/lib/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

# Change link names of all subdependencies
# Subdependencies must always be relative to the dependency package root
SUBDEPS[0]="lib/libvorbis.dylib"
SUBDEPS[1]="lib/libogg.dylib"

for SUBDEP in ${SUBDEPS[@]}
do
    link_name_change $LIBRARY_PATH $SUBDEP
done

echo "Vorbisfile: Done..."

#########################################################################
#   Frameworks
#########################################################################

# ALUT Framework
echo "ALUT: Working..."

LIBRARY_NAME="ALUT.framework/Versions/A/ALUT"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

echo "ALUT: Done..."

#########################################################################

# CEGUI Framework
echo "CEGUI: Working..."

LIBRARY_NAME="CEGUI.framework/Versions/A/CEGUI"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

# Change link names of all subdependencies
# Subdependencies must always be relative to the dependency package root
SUBDEPS[0]="Library/Frameworks/CEGUI.framework/Resources/freetype2.framework/Versions/A/freetype2"
SUBDEPS[1]="Library/Frameworks/CEGUI.framework/Resources/PCRE.framework/Versions/A/PCRE"

for SUBDEP in ${SUBDEPS[@]}
do
    link_name_change $LIBRARY_PATH $SUBDEP
done

echo "CEGUI: Done..."

# @TODO
# CEGUI Resources
# CoronalImageCodec
# ExpatParser
# FalagardWRBase
# FreeImageImageCodec
# SILLYImageCodec
# TGAImageCodec
# TinyXMLParser
# XercesParser

#########################################################################

# Ogre Framework
echo "Ogre: Working..."

LIBRARY_NAME="Ogre.framework/Versions/1.7.2/Ogre"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/${LIBRARY_NAME}"

# Change install name
install_name_change $LIBRARY_PATH $LIBRARY_PATH

echo "Ogre: Done..."