# Changed Makefile to install app bundle in the proper location
# and to provide the install prefix to the osx-app.sh script.

diff -uNrw Makefile.am.orig Makefile.am
--- Makefile.am.orig	2015-06-18 13:09:29.000000000 -0400
+++ Makefile.am	        2015-07-14 21:33:24.000000000 -0400
@@ -1584,6 +1584,7 @@
 		cd $(srcdir)/packaging/macosx ; \
 		./osx-app.sh @OSX_APP_FLAGS@ \
 			--create-bundle \
+			-l ${prefix} \
 			--binary-path ../staging/$(PACKAGE).inst$(bindir) ; \
 	else \
 		echo "Error: OS X packaging tools not found." ; \
@@ -1603,8 +1604,8 @@

 osx-install: osx-app
 	@if test x$(HAVE_OSX_PACKAGING) = xyes ; then \
-		echo "Copying Wireshark.app to /Applications" ; \
-		cp -pr $(srcdir)/packaging/macosx/Wireshark.app /Applications ; \
+		echo "Copying Wireshark.app to $(APPLICATIONS_DIR)" ; \
+		cp -pPR $(srcdir)/packaging/macosx/Wireshark.app $(DESTDIR)$(APPLICATIONS_DIR) ; \
 	fi

 test-programs:
diff -uNrw Makefile.in.orig Makefile.in
--- Makefile.in.orig	2015-06-18 13:09:43.000000000 -0400
+++ Makefile.in	        2015-07-14 21:33:05.000000000 -0400
@@ -4294,6 +4294,7 @@
 		cd $(srcdir)/packaging/macosx ; \
 		./osx-app.sh @OSX_APP_FLAGS@ \
 			--create-bundle \
+			-l ${prefix} \
 			--binary-path ../staging/$(PACKAGE).inst$(bindir) ; \
 	else \
 		echo "Error: OS X packaging tools not found." ; \
@@ -4313,8 +4314,8 @@

 osx-install: osx-app
 	@if test x$(HAVE_OSX_PACKAGING) = xyes ; then \
-		echo "Copying Wireshark.app to /Applications" ; \
-		cp -pr $(srcdir)/packaging/macosx/Wireshark.app /Applications ; \
+		echo "Copying Wireshark.app to $(APPLICATIONS_DIR)" ; \
+		cp -pPR $(srcdir)/packaging/macosx/Wireshark.app $(DESTDIR)$(APPLICATIONS_DIR) ; \
 	fi

 test-programs:

# Change the script to build an application bundle which contains only
# symbolic links to the executables in their locations under $prefix.

diff -uNrw packaging/macosx/osx-app.sh.orig packaging/macosx/osx-app.sh
--- packaging/macosx/osx-app.sh.orig	2015-06-18 13:09:32.000000000 -0400
+++ packaging/macosx/osx-app.sh	        2015-07-14 21:34:51.000000000 -0400
@@ -227,6 +209,9 @@
 		EXTRALIBS=""
 	fi

+	# Force building in 32-bit.
+	XCODEFLAGS="$XCODEFLAGS -arch i386"
+
 	# Set the SDK root, if an SDK was specified.
 	# (-sdk is only supported by the xcodebuild in the version of the
 	# developer tools that came with Snow Leopard and later versions)
@@ -265,7 +250,7 @@
 	mkdir -p "$pkgplugin"

 	if [ "$ui_toolkit" = "qt" ] ; then
-		cp -v "$binary_path/$wireshark_bin_name" "$pkgexec/Wireshark"
+		ln -sv "$LIBPREFIX/bin/$wireshark_bin_name" "$pkgexec/Wireshark"
 	else
 	# Build and add the launcher
 	#----------------------------------------------------------
@@ -290,9 +275,10 @@
 		for binary in $binary_list wireshark ; do
 			# Copy the binary to its destination
 			dest_path="$pkgbin/$binary-bin"
+			# wireshark-bin is wireshark-gtk
+			[ "$binary" = "wireshark" ] && binary=$wireshark_bin_name
+
-			cs_binary_list="$cs_binary_list $dest_path"
-			cp -v "$binary_path/$binary" "$dest_path"
-			# TODO Add a "$verbose" variable and command line switch, which sets wether these commands are verbose or not
+			ln -sv "$LIBPREFIX/bin/$binary" "$dest_path"

 			if [ "$binary" != "wireshark" ] ; then
 				ln -sv ./wireshark "$pkgbin/$binary"
@@ -300,26 +286,24 @@
 		done
 	elif [ "$ui_toolkit" = "qt" ] ; then
 		for binary in $binary_list ; do
-			# Copy the binary to its destination
-			cp -v "$binary_path/$binary" "$pkgexec"
-			cs_binary_list="$cs_binary_list $pkgexec/$binary"
+			ln -sv "$LIBPREFIX/bin/$binary" "$pkgexec/"
 		done
 	fi

-	# The rest of the Wireshark installation (we handled bin above)
-	rsync -av \
-		--exclude bin/ \
-		--exclude lib/ \
-		"$binary_path/.."/* "$pkgres"
+	# Symlink each item that would have been copied to the Resources folder.
+	mkdir -p "$pkgres/"
+	for rsrcdir in "$(find "$binary_path/../" -depth 1 \! -name bin \! -name lib)"; do
+		ln -sv "$LIBPREFIX/$(basename "$rsrcdir")" "$pkgres/"
+	done

-	rsync -av $binary_path/../lib/*.dylib "$pkglib/"
+	# Don't copy libraries, symlink Frameworks (or Resources/lib) to $LIBPREFIX/lib.
+	ln -sfv "$LIBPREFIX/lib" "$pkglib"

-	# Copy the plugins from the "make install" location for them
-	# to the plugin directory, removing the version number
-	find "$binary_path/../lib/wireshark/plugins" \
-		-type f \
-		\( -name "*.so" -o -name "*.dylib" \) \
-		-exec cp -fv "{}" "$pkgplugin/" \;
+	# Symlink ${bundle}/Contents/PlugIns to ${prefix}/lib/wireshark/plugins/${version}
+	rm -rf "$pkgplugin"
+	for plvers in "$(find "$binary_path/../lib/wireshark/plugins" -depth 1 -type d -name ?.*.*)"; do
+		ln -sfv "$LIBPREFIX/lib/wireshark/plugins/$(basename "$plvers")" "$pkgplugin"
+	done

 	cp "$plist" "$bundle/Contents/Info.plist"

@@ -333,22 +317,26 @@
 		res_list="
 			$res_list
 			bin
-			etc
 			openDoc
 			script
 			MenuBar.nib
 			ProgressWindow.nib
-			themes
 		"
 	fi

 	for rl_entry in $res_list ; do
-		rsync -av "$resdir"/Resources/$rl_entry "$bundle"/Contents/Resources/
+		rsync -av "$resdir"/Resources/$rl_entry "$pkgres/"
 	done

+	# Make sure that GTK launcher scripts are executable.
+	[ "$ui_toolkit" = "gtk" ] && chmod a+x "$pkgres/"{script,openDoc} "$pkgbin/wireshark"
+
 	# PkgInfo must match bundle type and creator code from Info.plist
 	echo "APPLWshk" > $bundle/Contents/PkgInfo

+	# Stop execution here. This build is not meant to be relocated.
+	return 0
+
 	if [ "$ui_toolkit" = "gtk" ] ; then
 		echo -e "\nPulling in GTK+ libraries and resources...\n"

@@ -424,6 +412,9 @@
 	create_bundle
 fi

+# Do not execute the rest of the script as the app is not meant to be relocated.
+exit 0
+
 echo -e "\nFixing up $bundle...\n"

 # Find out libs we need from Fink, MacPorts, or from a custom install

# Change the launcher script to remove all parts where environment variables
# are set to accomodate relocatable application bundles since ours isn't.

diff -uNrw packaging/macosx/Resources/bin/wireshark.orig packaging/macosx/Resources/bin/wireshark
--- packaging/macosx/Resources/bin/wireshark.orig   2015-06-18 13:09:32.000000000 -0400
+++ packaging/macosx/Resources/bin/wireshark        2015-07-14 21:28:24.000000000 -0400
@@ -21,35 +21,11 @@
 #	http://developer.apple.com/qa/qa2001/qa1067.html
 # but since no one does, we correct this by making the 'classic' PATH additions here:
 #	/usr/local/bin which, though standard, doesn't seem to be in the PATH
-# 	newer python as recommended by MacPython http://www.python.org/download/mac/
 #	Fink
 #	MacPorts (former DarwinPorts)
-export PATH="/opt/local/bin:/sw/bin/:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/local/bin:$CWD:$PATH"
+export PATH="/opt/local/bin:/sw/bin:/usr/local/bin:$CWD:$PATH"

 if [ "$APPNAME" == "wireshark" ] ; then # Apply GUI settings
-	# For Panther and Tiger, override the default fontconfig configuration
-        # XXX - This apparently isn't needed?
-	#if [[ $VERSION -le 4 ]]; then
-		export FONTCONFIG_PATH="$TOP/etc/fonts"
-	#fi
-	export PANGO_RC_FILE="$HOME/.wireshark-etc/pangorc"
-	export GTK_IM_MODULE_FILE="$HOME/.wireshark-etc/gtk.immodules"
-	export GDK_PIXBUF_MODULE_FILE="$HOME/.wireshark-etc/gdk-pixbuf.loaders"
-	export GTK_DATA_PREFIX="$TOP"
-	export GTK_EXE_PREFIX="$TOP"
-	export GNOME_VFS_MODULE_CONFIG_PATH="$TOP/etc/gnome-vfs-2.0/modules"
-	export GNOME_VFS_MODULE_PATH="$TOP/lib/gnome-vfs-2.0/modules"
-
-	# Set GTK theme (only if there is no .gtkrc-2.0 in the user's home)
-	if [[ ! -e "$HOME/.gtkrc-2.0" ]]; then
-		# prepare the theme depending on OS X appearance settings
-		(
-			cd "$TOP/themes/Clearlooks-Quicksilver-OSX/gtk-2.0/"
-			./sync_osx_look.sh
-			# TODO: make this more robust, probably by integrating this script directly inline here
-		)
-		export GTK2_RC_FILES="$TOP/themes/Clearlooks-Quicksilver-OSX/gtk-2.0/gtkrc"
-	fi

 	# TODO: Have to add ".UTF-8" to the LANG since ommiting causes Wireshark
 	#       to crash on startup in locale_from_utf8().
@@ -57,19 +33,6 @@
 		2>&1\`_\" /usr/share/locale/locale.alias | tail -n1 | sed 's/\./ /' | \
 		awk '{print $2}'`.UTF-8"

-	# Handle the case where the directory storing Wireshark has a '#' in the name.
-	# This '#' needs to be escaped in pango.modules for Pango to work properly.
-	ESCAPEDTOP=`echo $TOP | sed 's/#/\\\\\\\\#/'`
-
-	mkdir -p "${HOME}/.wireshark-etc"
-	sed 's|${HOME}|'"$HOME|g" "$TOP/etc/pango/pangorc" > "${HOME}/.wireshark-etc/pangorc"
-	sed 's|${CWD}|'"$ESCAPEDTOP|g" "$TOP/etc/pango/pango.modules" \
-		> "${HOME}/.wireshark-etc/pango.modules"
-	cp -f "$TOP/etc/pango/pangox.aliases" "${HOME}/.wireshark-etc/"
-	sed 's|${CWD}|'"$TOP|g" "$TOP/etc/gtk-2.0/gtk.immodules" \
-		> "${HOME}/.wireshark-etc/gtk.immodules"
-	sed 's|${CWD}|'"$TOP|g" "$TOP/etc/gtk-2.0/gdk-pixbuf.loaders" \
-		> "${HOME}/.wireshark-etc/gdk-pixbuf.loaders"
 fi

 exec "$CWD/$APPNAME-bin" "$@"

# Remove all parts of launcher scripts that are specific to Mac OS X 10.4 and earlier
# since this version of wireshark doesn't run on it anyway.

diff -uNrw packaging/macosx/Resources/bin/getdisplay.sh.orig packaging/macosx/Resources/bin/getdisplay.sh
--- packaging/macosx/Resources/bin/getdisplay.sh.orig	2015-06-18 13:09:32.000000000 -0400
+++ packaging/macosx/Resources/bin/getdisplay.sh        1969-12-31 19:00:00.000000000 -0500
@@ -1,9 +0,0 @@
-#!/bin/sh
-#
-# Author: Aaron Voisine <aaron@voisine.org>
-
-if [ "$DISPLAY"x == "x" ]; then
-    echo :0 > /tmp/display.$UID
-else
-    echo $DISPLAY > /tmp/display.$UID
-fi
diff -uNrw packaging/macosx/Resources/openDoc.orig packaging/macosx/Resources/openDoc
--- packaging/macosx/Resources/openDoc.orig	2015-06-18 13:09:32.000000000 -0400
+++ packaging/macosx/Resources/openDoc      2015-07-14 21:28:14.000000000 -0400
@@ -5,14 +5,6 @@

 CWD="`dirname \"$0\"`"

-# System version: 3 for Panther, 4 for Tiger, 5 for Leopard
-export VERSION=`/usr/bin/sw_vers | grep ProductVersion | cut -f2 -d'.'`
-
-if [[ $VERSION -le 4 ]]; then
-        export "DISPLAY=`cat /tmp/display.$UID`"
-fi
-
 BASE="`echo "$0" | sed -e 's/\/[A-Za-z]*.app\/Contents\/Resources\/openDoc/\//'`"
 cd "$BASE"
 exec "$CWD/bin/wireshark" "$@"
-
diff -uNrw packaging/macosx/Resources/script.orig packaging/macosx/Resources/script
--- packaging/macosx/Resources/script.orig	2015-06-18 13:09:32.000000000 -0400
+++ packaging/macosx/Resources/script       2015-07-14 21:31:51.000000000 -0400
@@ -5,49 +5,10 @@

 CWD="`dirname \"$0\"`"

-# System version: 3 for Panther, 4 for Tiger, 5 for Leopard
-export VERSION=`/usr/bin/sw_vers | grep ProductVersion | cut -f2 -d'.'`
-
-# On Leopard, X11.app is installed by default, and will be started
-# automatically via launchd.  On older systems, we need to start
-# X11 ourself.
-
-# For Panther and Tiger, start X11
-if [[ $VERSION -le 4 ]]; then
-	# FIXME apparently this removes the xterm that starts with X
-	# from xinitrc but when is it really used? Should we modify
-	# the .xinitrc of the user without warning?
-	ps -wx -ocommand | grep -e '[X]11' > /dev/null
-	if [ "$?" != "0" -a ! -f "${HOME}/.xinitrc" ]; then
-	    echo "rm -f \"${HOME}/.xinitrc\"" > "${HOME}/.xinitrc"
-	    sed 's/xterm/# xterm/' /usr/X11R6/lib/X11/xinit/xinitrc >> "${HOME}/.xinitrc"
-	fi
-
-	# Start X11 and get DISPLAY
-	cp -f "$CWD/bin/getdisplay.sh" /tmp/
-	rm -f /tmp/display.$UID
-	open-x11 /tmp/getdisplay.sh || \
-	open -a XDarwin /tmp/getdisplay.sh || \
-	echo ":0" > /tmp/display.$UID
-
-	while [ "$?" == "0" -a ! -f /tmp/display.$UID ]; do
-	    sleep 1
-	done
-	export DISPLAY="`cat /tmp/display.$UID`"
-
-	ps -wx -ocommand | grep -e '[X]11' > /dev/null || exit 11
-fi
-
-
 # Warn the user about time-consuming generation of fontconfig caches.
-test -f "${HOME}/.wireshark/.fccache-new" || exit 12
-
+# Check first if the user already has a cache, then check if we already warned.
+test -d "${HOME}/.cache/fontconfig" || test -f "${HOME}/.wireshark/.fccache-new" || exit 12

 BASE="`echo "$0" | sed -e 's/\/Contents\/Resources\/script/\//'`"
 cd "$BASE"
 exec "$CWD/bin/wireshark" "$@"
-# TODO examine wether it would be wisest to move the code from wireshark shell
-# script and getdisplay.sh to here and only keep the real binary in bin. This
-# may make things easier on Leopard and may also help using Wireshark on the
-# command line
-
