#!/bin/sh 

set -e

. /usr/share/debconf/confmodule

METADICDIR=/usr/share/anthy/dic
#ADDONDICDIR=/var/lib/anthy/
DICLIST=/var/cache/anthy/diclist
DICFILE=''

if [ ! -d /var/cache/anthy ]; then
	mkdir -m 755 /var/cache/anthy
fi

if [ -f $DICLIST ]; then
	if [ -f "$DICLIST"-old ]; then
		rm -f "$DICLIST-old"
	fi
	mv "$DICLIST" "$DICLIST-old"
fi

cat << ENDOFLIST > $DICLIST
2ch.t
base.t
extra.t
ENDOFLIST

if [ -f "$DICLIST"-old ]; then
	if diff -q "${DICLIST}"-old "${DICLIST}" > /dev/null 2>&1; then
		# old file and new file are identical
		rm -f "$DICLIST-old"
	else
		db_get anthy/reconf
		if [ "$RET" = "true" ]; then
			db_fset anthy/dictionaries seen false
		fi
	fi
fi

DICS=$(sort -u "$DICLIST" | tr '\n' ' ' \
      | sed 's/\ /,\ /g' | sed 's/,\ $//g')

db_capb backup

db_subst anthy/dictionaries dictionaries "$DICS"

while [ "$GO" != "ok" ]; do
	db_input medium anthy/dictionaries || true
	if db_go; then
		GO=ok
	fi
done

db_input medium anthy/reconf || true
db_go || true
