#!/bin/sh

DICDIR=/var/lib/anthy
METADICDIR=/usr/share/anthy/dic
CANNADIC='gcanna.ctd gcannaf.ctd'
EXTRADIC="$METADICDIR/*.t"

MATRIXDIR=/var/lib/anthy/mkmatrix
METAMATRIXFILE=/usr/share/anthy/matrix/dep.sorted

DEPGRAPHDIR=/var/lib/anthy/depgraph
WORDDICDIR=/var/lib/anthy/mkworddic

CONFIG='/etc/anthy/diclist'
DICTCONFIG='/etc/anthy/dict.args'
TMPDICTCONFIG='/tmp/dict.args.XXXXXX'

OLDDIR=`pwd`
cd $METADICDIR

TMPDICTCONFIG=$(mktemp $TMPDICTCONFIG)
if [ $? != 0 ]; then
	echo "mktemp failed, aborting" 1>&2
	exit 1
fi

for file in $CANNADIC; do
	case $file in
	*.ctd)
		if test -f $file; then
			echo "read $METADICDIR/$file" >> $TMPDICTCONFIG
		fi
		;;
	esac
done

echo "read $METADICDIR/katakana.t" >> $TMPDICTCONFIG
echo "read $METADICDIR/adjust.t" >> $TMPDICTCONFIG
echo "read $METADICDIR/name.t" >> $TMPDICTCONFIG
echo "read $METADICDIR/compound.t" >> $TMPDICTCONFIG

addondics=$(sort -u $CONFIG| tr '\n' ' '| sed 's/\ $//')

for file in $addondics; do
	if test -f $file; then
		echo "read $METADICDIR/$file" >> $TMPDICTCONFIG
	fi
done

echo "read $METADICDIR/placename.t" >> $TMPDICTCONFIG
echo "build_reverse_dict" >> $TMPDICTCONFIG
echo "read_uc $METADICDIR/udict" >> $TMPDICTCONFIG
echo "write anthy.wdic" >> $TMPDICTCONFIG
echo "done" >> $TMPDICTCONFIG

echo -n 'Updating anthy.dic...'

rm -rf $MATRIXDIR; mkdir $MATRIXDIR
rm -rf $WORDDICDIR; mkdir $WORDDICDIR
rm -rf $DEPGRAPHDIR; mkdir $DEPGRAPHDIR

cp /etc/anthy/anthy-conf /var/lib/anthy

if test -f $DICDIR/anthy.dic; then
	rm -f $DICDIR/anthy.dic
fi

if test -f $MATRIXDIR/matrix; then
	rm -f $MATRIXDIR/matrix
fi

if test -f $WORDDICDIR/anthy.wdic; then
	rm -f $WORDDICDIR/anthy.wdic
fi

mv $TMPDICTCONFIG $DICTCONFIG

cd $MATRIXDIR
/usr/bin/mkmatrix $METAMATRIXFILE -o ./matrix

cd $WORDDICDIR
/usr/bin/mkworddic -f $DICTCONFIG > /dev/null 2>&1

cd $DEPGRAPHDIR
for i in /usr/share/anthy/depgraph/*.depword; do
	ln -s $i $DEPGRAPHDIR/`basename $i`
done
ln -s /usr/share/anthy/depgraph/indepword.txt 

ln -s /usr/share/anthy/depgraph/conjugate.table $DEPGRAPHDIR
/usr/bin/mkdepgraph 

cd $DICDIR
/usr/bin/mkfiledic -p ./

rm -f /var/lib/anthy/anthy-conf

cd $OLDDIR

echo 'done.'
