#!/bin/bash
cross_root=/usr/cross
target=i686-apple-darwin8
bin=$cross_root/$target/bin
otool=$bin/$target-otool
install_name_tool=$bin/$target-install_name_tool

in_file=$1
dest=$2
#echo $0 input file: $in_file destination folder: $dest


deps=`$otool -L $in_file |awk '{ print $1 }' | grep /opt/mac//lib/`
#echo dependencies: $deps
for dep in $deps 
do
dep_filename=${dep##*/}
dest_dep=$dest/$dep_filename
echo "Fixing $in_file : $dep_filename"
$install_name_tool -change $dep @executable_path/../Frameworks/$dep_filename $in_file
if [ -f $dest_dep ]
then
echo >/dev/null
else
#echo Copying $dep to $dest_dep 
cp -a $dep $dest_dep
#echo Recursive self-invocation
$0 $dest_dep $dest
fi
done
