#!/bin/sh

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

MATRIXDIR=/opt/maemocjk/var/lib/anthy/mkmatrix
DEPGRAPHDIR=/opt/maemocjk/var/lib/anthy/depgraph
WORDDICDIR=/opt/maemocjk/var/lib/anthy/mkworddic

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

OLDDIR=`pwd`
cd $METADICDIR

mktemp $TMPDICTCONFIG > /dev/null 2>&1

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 /opt/maemocjk/etc/anthy/anthy-conf /opt/maemocjk/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
/opt/maemocjk/usr/bin/mkmatrix /opt/maemocjk/usr/share/anthy/matrix/parsed.txt -o ./matrix

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

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

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

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

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

cd $OLDDIR

echo 'done.'
