#!/bin/sh

CONFIGFILE=/etc/openoffice/settings.debconf

set -e

. /usr/share/debconf/confmodule

db_get openoffice.org-bin/prelink && prelink=$RET || prelink="false"
if [ -f $CONFIGFILE ]; then
	. $CONFIGFILE || true

	TMP=`mktemp -q`
        cp -a -f $CONFIGFILE $TMP
        if grep -q "^prelink=.*" $CONFIGFILE; then
		sed -e "s/^ *prelink=.*/prelink=\"$prelink\"/" \
            	< $CONFIGFILE > $TMP
	else
		echo "prelink=\"$prelink\"" >> $CONFIGFILE
	fi
        mv -f $TMP $CONFIGFILE
else
	echo "prelink=\"$prelink\"" > $CONFIGFILE
fi

if [ "$1" = "openoffice.org-bin" -a "$2" = "configure" ]; then
	if [ -x /usr/sbin/prelink ]; then
		# the check if we should do it is actually done in this script
		# prelink can hang asking for input so redirect stdin
		# (BTS #233139)
		oooprelink < /dev/null
	fi
fi

if [ "$1" = "openoffice.org-bin" -a "$2" = "purge" ]; then
	rm -f /var/state/openoffice/ooo_is_prelinked
	if [ -d /var/state/openoffice ]; then
		if ! ls /var/state/openoffice/*; then
			rm -rf /var/state/openoffice
		fi
	fi
fi

