1#!/bin/bash
2
3#### Configuration:
4# The name of the executable. It is assumed
5# that it is in the current working directory.
6EXE_NAME=hidapi-testgui
7# Path to the executable directory inside the bundle.
8# This must be an absolute path, so use $PWD.
9EXEPATH=$PWD/TestGUI.app/Contents/MacOS
10# Libraries to explicitly bundle, even though they
11# may not be in /opt/local. One per line. These
12# are used with grep, so only a portion of the name
13# is required. eg: libFOX, libz, etc.
14LIBS_TO_BUNDLE=libFOX
15
16
17function copydeps {
18	local file=$1
19	# echo "Copying deps for $file...."
20	local BASE_OF_EXE=`basename $file`
21
22	# A will contain the dependencies of this library
23	local A=`otool -LX $file |cut -f 1 -d " "`
24	local i
25	for i in $A; do
26		local BASE=`basename $i`
27
28		# See if it's a lib we specifically want to bundle
29		local bundle_this_lib=0
30		local j
31		for j in $LIBS_TO_BUNDLE; do
32			echo $i |grep -q $j
33			if [ $? -eq 0 ]; then
34				bundle_this_lib=1
35				echo "bundling $i because it's in the list."
36				break;
37			fi
38		done
39
40		# See if it's in /opt/local. Bundle all in /opt/local
41		local isOptLocal=0
42		echo $i |grep -q /opt/local
43		if [ $? -eq 0 ]; then
44			isOptLocal=1
45			echo "bundling $i because it's in /opt/local."
46		fi
47
48		# Bundle the library
49		if [ $isOptLocal -ne 0 ] || [ $bundle_this_lib -ne 0 ]; then
50
51			# Copy the file into the bundle if it exists.
52			if [ -f $EXEPATH/$BASE ]; then
53				z=0
54			else
55				cp $i $EXEPATH
56				chmod 755 $EXEPATH/$BASE
57			fi
58
59
60			# echo "$BASE_OF_EXE depends on $BASE"
61
62			# Fix the paths using install_name_tool and then
63			# call this function recursively for each dependency
64			# of this library.
65			if [ $BASE_OF_EXE != $BASE ]; then
66
67				# Fix the paths
68				install_name_tool -id @executable_path/$BASE $EXEPATH/$BASE
69				install_name_tool -change $i @executable_path/$BASE $EXEPATH/$BASE_OF_EXE
70
71				# Call this function (recursive) on
72				# on each dependency of this library.
73				copydeps $EXEPATH/$BASE
74			fi
75		fi
76	done
77}
78
79rm -f $EXEPATH/*
80
81# Copy the binary into the bundle. Use ../libtool to do this if it's
82# available beacuse if $EXE_NAME was built with autotools, it will be
83# necessary.  If ../libtool not available, just use cp to do the copy, but
84# only if $EXE_NAME is a binary.
85if [ -x ../libtool ]; then
86	../libtool --mode=install cp $EXE_NAME $EXEPATH
87else
88	file -bI $EXE_NAME |grep binary
89	if [ $? -ne 0 ]; then
90		echo "There is no ../libtool and $EXE_NAME is not a binary."
91		echo "I'm not sure what to do."
92		exit 1
93	else
94		cp $EXE_NAME $EXEPATH
95	fi
96fi
97copydeps $EXEPATH/$EXE_NAME
98