#! /bin/sh

DICTDIR=/usr/share/anthy/dic
UPDATE=/usr/sbin/update-anthy-dics
DICTLIST="2ch.t udict zipcode.t utf8.t tankanji.t extra.t compound.t base.t \
	  adjust.t gtankan.ctd gcannaf.ctd gcanna.ctd"
EXTRADICTLIST=""
HASHLIST=/var/lib/anthy/hashlist

set -e

case "$1" in
    configure)
	. /usr/share/debconf/confmodule

	if [ -f /etc/anthy/diclist ]; then
		rm -f /etc/anthy/diclist
	fi
	db_get anthy/dictionaries

	touch /etc/anthy/diclist
	DICS="$RET"
	if [ "$RET" != "" ]; then
		DICS=`echo $RET | sed 's/,//g'`
		for i in $DICS; do
			echo $i >> /etc/anthy/diclist
		done
	fi

	if [ -x "$UPDATE" ] && [ -d "$DICTDIR" ]; then
		tmpdir=`mktemp --tmpdir -d anthy.XXXXXXXXXX`
		cd $tmpdir && temp_hashlist=`mktemp anthy-dict.XXXXXXXXXX`
                cd $DICTDIR && \
		for dict in $DICTLIST $EXTRADICTLIST; do md5sum $dict >> $tmpdir/$temp_hashlist; done
		if [ ! -f "$HASHLIST" ]; then
   		  $UPDATE
		  mv $tmpdir/$temp_hashlist $HASHLIST
		elif [ -z "`cmp -l $HASHLIST $tmpdir/$temp_hashlist 2 > /dev/null`" ]; then
		  $UPDATE
		  mv $tmpdir/$temp_hashlist $HASHLIST
		else
		  rm $tmpdir/$temp_hashlist
		fi
	fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
