#!/bin/sh

# Given a list of files, rewrite the non-system shared library
# dependencies of executables and libraries to be
# @executable_path/../lib/condor/... or @executable_path/../lib/...
# Other types of files are ignored.

if [ "$1" = "" ] ; then
  echo "Usage:" `basename $0` "<executable/library> ..."
  exit 1
fi
for filename in "$@" ; do
  type=`file -h $filename`
  if echo $type | grep -q -e 'dynamically linked shared library' -e 'executable' ; then
    basefile=`basename $filename`
    libraries=`otool -L $filename | grep '^[[:space:]][^@]' | sed 's|^.\([^ ]*\) .*|\1|' | grep -v '^/usr/lib' | grep -v '^/System/' | grep -v "$basefile "`
    
    for lib in $libraries ; do
      echo $lib
      baselib=`basename $lib`
      if echo $baselib | grep -q -e 'classad' -e 'condor' ; then
        install_name_tool -change "$lib" "@executable_path/../lib/$baselib" $filename
      else
        install_name_tool -change "$lib" "@executable_path/../lib/condor/$baselib" $filename
      fi
    done
  fi
done
