#!/bin/sh

set -e

if [ -f /etc/openoffice/settings.debconf ]; then
	. /etc/openoffice/settings.debconf
else
	prelink="false"
fi

#DEBUG="--verbose"

help() {
	echo "oooprelink -- Prelink OpenOffice.org's binaries"
	echo "-----------------------------------------------"
	echo "How this script can be called:"
	echo "oooprelink -- determines whether to do prelinking"
	echo "              from /etc/openoffice/settings.debconf"
	echo "oooprelink -u|--unprelink -- force unprelinking"
	echo "oooprelink -f|--force -- force prelinking"
	echo "oooprelink -h|--help -- show this help"
	exit 1
}

if [ "$1" = "-f" -o "$1" = "--force" ]; then
	prelink="true"
elif [ "$1" = "-u" -o "$1" = "--unprelink" ]; then
	prelink="false"
elif [ "$1" = "-h" -o "$1" = "--help" ]; then
	help
elif [ "$1" = "" ]; then
	sleep 1s
else
	help
fi

if [ "$prelink" = "true" ]; then
	echo -n "Prelinking OpenOffice.org binaries... "
	/usr/sbin/prelink $DEBUG \
		--ld-library-path=/usr/lib/openoffice/program \
		--conserve-memory --libs-only /usr/lib/openoffice/
 	/usr/sbin/prelink $DEBUG \
		--ld-library-path=/usr/lib/openoffice/program \
		--conserve-memory /usr/lib/openoffice/program/*.bin \
		/usr/lib/openoffice/program/ooovirg \
		/usr/lib/openoffice/program/regcomp \
		/usr/lib/openoffice/program/pagein \
		/usr/lib/openoffice/program/gnomeint
	if [ ! -d /var/state/openoffice ]; then
		mkdir -p /var/state/openoffice
	fi
	touch /var/state/openoffice/ooo_is_prelinked
	echo "done."
	# only i386 has an additional dep on libstdc++3
	ARCH=`dpkg --print-architecture`
	if [ "$ARCH" = "ia64" -o "$ARCH" = "alpha" ]; then
	LIBC="libc6.1"; else LIBC="libc6"; fi
	echo "If you update the libraries used by OpenOffice.org (listed in"
	echo "/usr/share/doc/openoffice.org-bin/used_libs) in the future,"
	echo "you need to prelink again."
	echo "Run /usr/sbin/oooprelink then."
elif [ "$prelink" = "false" ]; then
	if [ -f /var/state/openoffice/ooo_is_prelinked ]; then
		echo -n "Undoing prelinking of OpenOffice.org binaries... "
		/usr/sbin/prelink --undo $DEBUG \
			--ld-library-path=/usr/lib/openoffice/program \
			--conserve-memory /usr/lib/openoffice \
			2>/dev/null
		#/usr/sbin/prelink --undo $DEBUG \
		#	--ld-library-path=/usr/lib/openoffice/program \
		#	--conserve-memory \
		#	/usr/lib/openoffice/program/*.bin \
		#	/usr/lib/openoffice/program/regcomp \
		#	/usr/lib/openoffice/program/pagein \
		#	/usr/lib/openoffice/program/gnomeint
		rm -f /var/state/openoffice/ooo_is_prelinked
		echo "done."
	fi
else
	echo -n "Unknown value for prelink variable in "
	echo "/etc/openoffice/settings.debconf"
	exit 1
fi
	


