#!/bin/bash
#
#########################################################################
#
#    ORXONOX - the hottest 3D action shooter ever to exist
#                     > www.orxonox.net <
# 
# 
#    License notice:
# 
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2
#    of the License, or (at your option) any later version.
# 
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
# 
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#    MA 02110-1301, USA.
# 
#    Author:
#       Kevin J. Young (youngk) 2010
#       
#    Co-authors:
#       ...
# 
#########################################################################

#########################################################################
#
#    Description: We were having trouble properly compiling Orxonox
#    with the dependency package on a Mac, because the install_names
#    (equivalent to linux RPATH) of our dependencies were set badly.
#    This script adjusts these install_names and is executed automati-
#    cally, each time you run CMake over Orxonox. A small warning:
#    Never build Orxonox in a path with whitespaces - it's only trouble.
#
#    V1.12 Mar 26 2011
#
#    Changelog:
#    V1.12 Mar 26 2011: Moved the lua.framework to a more convenient
#                       location, while deleting lua.dylib and associates
#    V1.11 Mar 11 2011: Needed to accomodate for additional CEGUI modules
#                       in preparation of a branch merger
#    V1.1 Mar 3 2011: Script now checks whether Xcode is installed
#    V1.0 Dec 15 2010: Initial version
#
#########################################################################

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

# Checks, whether you have installed the library tools (ie. Xcode)
DEPS_INSTALLED=0;
OTOOL_INSTALLED=`which otool`;
INSTALL_NAME_INSTALLED=`which install_name_tool`;
if [ "${OTOOL_INSTALLED}" == "" ] || [ "${INSTALL_NAME_INSTALLED}" == "" ]
    then
        echo "Could not find library tools! Please install Xcode first.";
        exit 1;
fi

# 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 (keep echo line for debugging)
    install_name_tool -id "$new_install_name_l" "$library_path_l"
    #echo -e "install_name_tool -id \n\t $new_install_name_l \n\t $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)"
    
    # Script doesn't find the correct path for some subdependencies.
    # Therefore we need to change the filter appropriately
    if [ $filter_l == "CEGUI" ]
        then
            filter_l="CEGUI.framework/Versions/A/CEGUI";
    elif [ $filter_l == "FreeImage" ]
        then
            filter_l="FreeImage.framework/Versions/A/FreeImage";
    elif [ $filter_l == "SILLY" ]
        then
            filter_l="SILLY.framework/Versions/A/SILLY";
    elif [ $filter_l == "Xerces" ]
        then
            filter_l="Xerces.framework/Versions/A/Xerces";
    elif [ $filter_l == "lua" ]
        then
            filter_l="lua.framework/Versions/A/lua";
    fi
    
    # 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 | awk '{ if(NR==1) print }')"
    
    # Append the dependency package path to the subdependency path
    local new_link_name_l="$DEP_PACK_PATH/$2"

    # TODO: Proof-read the following already-run-check sequence
    # Do the link names match? If so, the script has already been run
    #if [ $current_link_name_l == $new_link_name_l ]
    #    then
    #        DEPS_INSTALLED=1;
    #        echo "Library $filter_l already modified.";
    #else
    #    then
    #        # Reset the link name current_link_name_l in argument 1 (a library) to argument no. 2
    #        # (keep echo line for debugging)
    #        install_name_tool -change $current_link_name_l $new_link_name_l $library_path_l;
    #        #echo -e "install_name_tool -change \n\t $current_link_name_l \n\t $new_link_name_l \n\t $library_path_l";
    #fi
    
    # Reset the link name current_link_name_l in argument 1 (a library) to argument no. 2
    # (keep echo line for debugging)
    install_name_tool -change "$current_link_name_l" "$new_link_name_l" "$library_path_l"
    #echo -e "install_name_tool -change \n\t $current_link_name_l \n\t $new_link_name_l \n\t $library_path_l";
   }

echo "Orxonox: Installing dependencies on your system..."

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

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

LIBRARY_NAME="lua.framework/Versions/A/lua"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/${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

unset SUBDEPS

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

unset SUBDEPS

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

unset SUBDEPS

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

unset SUBDEPS


#########################################################################
# CEGUI Resources

# CoronalImageCodec
echo "CEGUI: Mangling CoronalImageCodec..."

LIBRARY_NAME="CEGUICoronaImageCodec.bundle/Contents/MacOS/CEGUICoronaImageCodec"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/CEGUI.framework/Versions/A/Resources/${LIBRARY_NAME}"

# Change install name
# These CEGUI bundles do not have any install names

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

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

unset SUBDEPS

# ExpatParser
echo "CEGUI: Mangling ExpatParser..."

LIBRARY_NAME="CEGUIExpatParser.bundle/Contents/MacOS/CEGUIExpatParser"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/CEGUI.framework/Versions/A/Resources/${LIBRARY_NAME}"

# Change install name
# These CEGUI bundles do not have any install names

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

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

unset SUBDEPS

# FalagardWRBase
echo "CEGUI: Mangling FalagardWRBase..."

LIBRARY_NAME="CEGUIFalagardWRBase.bundle/Contents/MacOS/CEGUIFalagardWRBase"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/CEGUI.framework/Versions/A/Resources/${LIBRARY_NAME}"

# Change install name
# These CEGUI bundles do not have any install names

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

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

unset SUBDEPS

# FreeImageImageCodec
echo "CEGUI: Mangling FreeImageImageCodec..."

LIBRARY_NAME="CEGUIFreeImageImageCodec.bundle/Contents/MacOS/CEGUIFreeImageImageCodec"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/CEGUI.framework/Versions/A/Resources/${LIBRARY_NAME}"

# Change install name
# These CEGUI bundles do not have any install names

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

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

unset SUBDEPS

# SILLYImageCodec
echo "CEGUI: Mangling SILLYImageCodec..."

LIBRARY_NAME="CEGUISILLYImageCodec.bundle/Contents/MacOS/CEGUISILLYImageCodec"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/CEGUI.framework/Versions/A/Resources/${LIBRARY_NAME}"

# Change install name
# These CEGUI bundles do not have any install names

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

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

unset SUBDEPS

# TGAImageCodec
echo "CEGUI: Mangling TGAImageCodec..."

LIBRARY_NAME="CEGUITGAImageCodec.bundle/Contents/MacOS/CEGUITGAImageCodec"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/CEGUI.framework/Versions/A/Resources/${LIBRARY_NAME}"

# Change install name
# These CEGUI bundles do not have any install names

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

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

unset SUBDEPS

# TinyXMLParser
echo "CEGUI: Mangling TinyXMLParser..."

LIBRARY_NAME="CEGUITinyXMLParser.bundle/Contents/MacOS/CEGUITinyXMLParser"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/CEGUI.framework/Versions/A/Resources/${LIBRARY_NAME}"

# Change install name
# These CEGUI bundles do not have any install names

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

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

unset SUBDEPS

# XercesParser
echo "CEGUI: Mangling XercesParser..."

LIBRARY_NAME="CEGUIXercesParser.bundle/Contents/MacOS/CEGUIXercesParser"
LIBRARY_PATH="${DEP_PACK_PATH}/Library/Frameworks/CEGUI.framework/Versions/A/Resources/${LIBRARY_NAME}"

# Change install name
# These CEGUI bundles do not have any install names

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

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

unset SUBDEPS

echo "CEGUI: Done."

#########################################################################
# CEGUIOpenGLRenderer
echo "CEGUIOpenGLRenderer: Working..."

LIBRARY_NAME="CEGUIOpenGLRenderer.framework/Versions/A/CEGUIOpenGLRenderer"
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/Versions/A/CEGUI"

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

unset SUBDEPS

echo "CEGUIOpenGLRenderer: Done."

#########################################################################
# CEGUILuaScriptModule
echo "CEGUILuaScriptModule: Working..."

LIBRARY_NAME="CEGUILuaScriptModule.framework/Versions/A/CEGUILuaScriptModule"
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/Versions/A/CEGUI"
SUBDEPS[1]="Library/Frameworks/ceguitolua++.framework/Versions/A/ceguitolua++"
SUBDEPS[2]="Library/Frameworks/lua.framework/Versions/A/lua"

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

unset SUBDEPS

echo "CEGUILuaScriptModule: Done."

#########################################################################
# ceguitolua++
echo "ceguitolua++: Working..."

LIBRARY_NAME="ceguitolua++.framework/Versions/A/ceguitolua++"
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/lua.framework/Versions/A/lua"

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

unset SUBDEPS

echo "ceguitolua++: Done."

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

#########################################################################
#    Ogre Plugins and Render Systems

# Plugin_ParticleFX.dylib
echo "Ogre: Mangling Plugin_ParticleFX..."

LIBRARY_NAME="Plugin_ParticleFX.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/bin/${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/Ogre.framework/Versions/1.7.2/Ogre"

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

unset SUBDEPS

# RenderSystem_GL.dylib
echo "Ogre: Mangling RenderSystem_GL..."

LIBRARY_NAME="RenderSystem_GL.dylib"
LIBRARY_PATH="${DEP_PACK_PATH}/bin/${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/Ogre.framework/Versions/1.7.2/Ogre"

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

unset SUBDEPS

echo "Ogre: Done."

#########################################################################
#    All done
#########################################################################

echo "Orxonox: All done. Enjoy!"
