#!/bin/bash

# this script should be executed from the directory that contains the sound-access.app directory (application bundle)
# it copies the required 3rd party libraries into the application bundle and corrects the library install names and references

LIBPREFIX=/usr/local/lib/
#QTPREFIX=/usr/local/Trolltech/Qt-4.1.0/lib/
TARGETPATH=sound-access.app/Contents/Frameworks/

mkdir $TARGETPATH

# add other required libs (e.g. oggz)
# use "otool -L <executable>" to get a list of required libraries

#LIBS="libmad.0.2.1.dylib libsndfile.1.dylib libsamplerate.0.dylib libportaudio.dylib.0.0.18 liboggz.1.dylib libfishsound.1.dylib libvorbisenc.2.dylib libvorbis.0.dylib libogg.0.dylib"

# libjack.0.dylib  liblrdf.2.dylib libraptor.1.dylib

LIBS=

for LIB in $LIBS;
do
  cp $LIBPREFIX$LIB $TARGETPATH
done

for LIB in $LIBS;
do
  install_name_tool -id @executable_path/../Frameworks/${LIB} sound-access.app/Contents/Frameworks/${LIB}
done

for LIB in $LIBS;
do
  install_name_tool -change ${LIBPREFIX}${LIB} @executable_path/../Frameworks/${LIB}   sound-access.app/Contents/MacOS/sound-access
done

pushd $TARGETPATH

for LIB in $LIBS;
do
for TARGETLIB in $LIBS;
do
    install_name_tool -change ${LIBPREFIX}${TARGETLIB} @executable_path/../Frameworks/${TARGETLIB} $LIB
done
done

popd

QTPREFIX=/Library/Frameworks/
QTFWKS="QtXml QtCore QtGui QtNetwork"

# copy the dynamic libraries into the app bundle

for FWK in $QTFWKS; do
  cp ${QTPREFIX}${FWK}.framework/Versions/4/${FWK} ${TARGETPATH}
done

# change the id's of the dylibs
for FWK in $QTFWKS; do
  install_name_tool -id @executable_path/../Frameworks/${FWK} sound-access.app/Contents/Frameworks/$FWK
done

# tell the linker to look for dylibs in the app bundle
for FWK in $QTFWKS; do
  install_name_tool -change ${FWK}.framework/Versions/4/${FWK} @executable_path/../Frameworks/${FWK} sound-access.app/Contents/MacOS/sound-access
done

# correct dependencies between QT dylibs
for FWK in $QTFWKS; do
  case $FWK in QtCore) continue;; esac
  install_name_tool -change QtCore.framework/Versions/4/QtCore @executable_path/../Frameworks/QtCore  sound-access.app/Contents/Frameworks/${FWK}
done

