#!/bin/bash

echo "install name sourcedir: $sourcedir"
cd $sourcedir
for file in $(ls -1); do
    if [ $file != "__init__.py" ]; then
        otool -l $file | grep -A5 LC_ID_DYLIB | grep name | awk '{print $2}'
        dir_name=$(dirname $file)
        file_name=$(basename $file)
        install_name_tool -id @loader_path/$file_name $file

        for sub_name in $(otool -l $file | grep -A5 LC_LOAD_DYLIB | grep name | awk '{print $2}'); do
            if [[ -e "$sub_name" ]]; then
                sub_dir_name=$(dirname "$sub_name")
                sub_file_name=$(basename "$sub_name")
                echo "2ND LVL - sub_dir_name: $sub_dir_name  sub_file_name: $sub_file_name"
                install_name_tool -change $sub_dir_name/$sub_file_name @loader_path/$sub_file_name $file
            fi
        done
    fi
done