# HG changeset patch
# User t_mrc-ct@users.osdn.jp
# Date 1438603387 -32400
#      Mon Aug 03 21:03:07 2015 +0900
# Branch THUNDERBIRD3880_2016050308_RELBRANCH
# Node ID 45de47f9ea2deee3c8388df066d3d8e5e4b45d81
# Parent  00af99767f2f1ab2ce4dd06abdb4e9421789c164
refactoring 104distro.sh

diff --git a/104distro.sh b/104distro.sh
--- a/104distro.sh
+++ b/104distro.sh
@@ -1,105 +1,103 @@
 #!/bin/sh
 
-# from AuroraFox, modified for TenFourFox, modified for Tenfourbird
+# from AuroraFox, modified for TenFourFox and Tenfourbird
+
+libgcc_dylibs="libstdc++.6.dylib libgcc_s.1.dylib"
 
 local_prefix="/opt/local"
 
 if [ $# -ne 3 ]
 then
 	echo "usage:" >&2
-	echo "$0 lib_installed_dir source_app dest_app" >&2
+	echo "$0 libgcc_dir source_app dest_app" >&2
 	exit 1
 fi
 
 _pwd="${PWD}"
 cd "$1"
-lib_installed_dir="${PWD}"
+libgcc_dir="${PWD}"
 cd "${_pwd}"
 
 src_app=$2
 dest_app=$3
 
-dest_dir="${dest_app}/Contents/MacOS"
+dest_dir="${dest_app}/Contents"
+
+function exit_err() {
+	cnt=$1
+	msg=$2
+	echo "Error count: ${cnt}" >&2
+	echo "${msg}" >&2
+	exit $1
+}
 
 if [ -e "${dest_app}" ]
 then
-	echo "Error count: 1" >&2
-        echo "destination directory already existed: ${dest_app}" >&2
-	exit 1
+	exit_err 1 "destination directory already existed: ${dest_app}"
 fi
 
 cp -RL "${src_app}" "${dest_app}" || exit
 test -e "${dest_dir}" || exit
 
-libstdcxx_name="libstdc++.6.dylib"
-libgcc_name="libgcc_s.1.dylib"
+for dylib in ${libgcc_dylibs}
+do
+	ditto -v "${libgcc_dir}/${dylib}" "${dest_dir}/MacOS" || exit
+	install_name_tool -change "${libgcc_dir}/${dylib}" "@executable_path/${dylib}" \
+		-id "@executable_path/${dylib}" \
+		"${dest_dir}/MacOS/${dylib}" || exit
+done
 
-ditto -v "${lib_installed_dir}/${libstdcxx_name}" "${dest_dir}" || exit
-ditto -v "${lib_installed_dir}/${libgcc_name}" "${dest_dir}" || exit
+libgcc_prefix=`otool -L "${dest_dir}/MacOS/XUL" | sed -E -e 's#^[[:space:]]*(/opt/[^/]+/lib/[^/]*gcc[^/]*)/.+$#\1#p' -e d | head -1`
 
-eval "${local_prefix}/bin/strip" -x "${dest_dir}/${libstdcxx_name}"
+if [ -z "${libgcc_prefix}" ]
+then
+	exit_err 1 "${src_app} does not use gcc in '/opt/*' "
+fi
 
-install_name_tool -change "${lib_installed_dir}/${libgcc_name}" "@executable_path/${libgcc_name}" \
-                  -id "@executable_path/${libgcc_name}" \
-                  "${dest_dir}/${libgcc_name}"
-
-install_name_tool -change "${lib_installed_dir}/${libgcc_name}" "@executable_path/${libgcc_name}" \
-                  -change "${lib_installed_dir}/${libstdcxx_name}" "@executable_path/${libstdcxx_name}" \
-                  -id "@executable_path/${libstdcxx_name}" \
-                  "${dest_dir}/${libstdcxx_name}"
-
-libstdcxx_path="${local_prefix}/lib/libgcc/${libstdcxx_name}"
-libgcc_path="${local_prefix}/lib/libgcc/${libgcc_name}"
-
-change_sharelib()
-	{
+function change_sharelib() {
 	errcnt=0
 	while read file
 	do
-	libs=`otool -L "${file}" | sed -E -e 's/[[:space:]]+([^[:space:]](.+[^[:space:]])?)[[:space:]]+\([^(]+/\1/p' -e d`
-	if [ -z "${libs}" ]
-	then
-		continue
-	fi
+		libs=`otool -L "${file}" | sed -E -e 's/[[:space:]]+([^[:space:]](.+[^[:space:]])?)[[:space:]]+\([^(]+/\1/p' -e d`
+		if [ -z "${libs}" ]
+		then
+			continue
+		fi
 
-	opt_libs=`echo "${libs}" | grep -E -e '^/opt/local/'`
+		opt_libs=`echo "${libs}" | grep -E -e '^/opt/'`
 
-	if [ -n "${opt_libs}" ]
-	then
-		echo "file: ${file}"
-	else
-		continue
-	fi
+		if [ -n "${opt_libs}" ]
+		then
+			echo "file: ${file}"
+		else
+			continue
+		fi
 
-	rv=`echo "${opt_libs}" | grep -x -F "${libgcc_path}"`
-	if [ -n "${rv}" ]
-	then
-		echo "	change: ${rv}"
-		install_name_tool -change "${libgcc_path}" "@executable_path/${libgcc_name}" "${file}"
-	fi
+		for dylib in ${libgcc_dylibs}
+		do
+			if [ -n "`echo "${opt_libs}" | grep -xF "${libgcc_prefix}/${dylib}"`" ]
+			then
+				echo "  change: ${libgcc_prefix}/${dylib}"
+				install_name_tool -change "${libgcc_prefix}/${dylib}" "@executable_path/${dylib}" "${file}"
+				opt_libs=`echo "${opt_libs}" | grep -vxF "${libgcc_prefix}/${dylib}"`
+			fi
+		done
 
-	rv=`echo "${opt_libs}" | grep -x -F "${libstdcxx_path}"`
-	if [ -n "${rv}" ]
-	then
-		echo "	change: ${rv}"
-		install_name_tool -change "${libstdcxx_path}" "@executable_path/${libstdcxx_name}" "${file}"
-	fi
+		if [ -n "${opt_libs}" ]
+		then
+			errcnt=$(($errcnt + 1))
+			echo "error: unknown shared libraries found" >&2
+			echo "  file: ${file}" >&2
+			echo "${opt_libs}" | sed -E -e 's/.+/   shared_lib: &/p' -e d >&2
 
-	rv=`echo "${opt_libs}" | grep -v -x -F "${libstdcxx_path}" | grep -v -x -F "${libgcc_path}"`
-	if [ -n "${rv}" ]
-	then
-		errcnt=$(($errcnt + 1))
-		echo "error: unknown shared libraries found" >&2 
-		echo "	file: ${file}" >&2
-		echo "${rv}" | sed -E -e 's/.+/	shared_lib: &/p' -e d >&2
-		echo >&2
-	fi
-	echo
+			echo >&2
+		fi
+		echo
 	done
 	if [ "$errcnt" -ne 0 ]
 	then
 		echo "Error count: $errcnt" >&2
 	fi
-	}
+}
 
 find "${dest_dir}" -type f | change_sharelib
