#!/bin/sh

set -e

if [ "$1" = "openoffice.org" -o "$1" = "openoffice.org-debian-files" ] && [ "$2" = "configure" ]; then
	# This directory should be in openoffice.org - workaround:
	mkdir -p /usr/lib/openoffice/share/dict

	cd /usr/lib/openoffice/share/dict
	# when "ooo" is a link, we do not need to do anything here
	if [ ! -L ooo ]; then
		# do we have files in that directory?
		TMP=`mktemp -q`
		ls -l1 ooo/* > $TMP 2> /dev/null || /bin/true
		# if yes, move them...
		if [ -s $TMP ]; then

			if [ -L /usr/lib/openoffice/share/dict/ooo/dicts ]; then
				# remove link created by bug in earlier versions
				rm /usr/lib/openoffice/share/dict/ooo/dicts
			fi

			echo -n "Moving user myspell dictionaries to /usr/share/myspell/dicts... "
			if [ ! -d /usr/share/myspell/dicts ]; then
				mkdir -p /usr/share/myspell/dicts
			fi
			mv -v ooo/* /usr/share/myspell/dicts
			rmdir ooo
			echo "done."
		fi
		rm -f $TMP

		if [ ! -e /usr/lib/openoffice/share/dict/ooo ]; then
			echo "Creating symlink /usr/lib/openoffice/share/dict/ooo -> /usr/share/myspell/dicts..."
			ln -s /usr/share/myspell/dicts /usr/lib/openoffice/share/dict/ooo
		else
			echo "Maintainer script error: /usr/lib/openoffice/share/dict/ooo exists"
			ls -l /usr/lib/openoffice/share/dict
			exit 1
		fi
	fi
fi
