#!/bin/sh -e

SDICT_DIR=/opt/sdict

f="${1%%.bz2}" # strip trailing .bz2 if needed
f="${f%%.dct}" # strip trailing .dct if needed

if [ -e $f.dct.bz2 ]; then
    echo "Decompressing $f.dct.bz2"
    bzip2 -d -k $1.dct.bz2
fi

if [ ! -e $f.dct ]; then
    echo "File '$f.dct' not found in working directory!"
    exit 1
fi

if [ ! -e $f.txt ]; then
    # decompile DCT file to plain text
    $SDICT_DIR/bin/sdict-tools.plx --decompile --input-file=$f.dct --output-file=$f.txt
fi

title=`head $f.txt | grep 'title = ' | colrm 1 8`

echo "Removing nul characters"
tr -d '\000' < $f.txt > $f-nonul.txt

echo "Getting rid of ___ and <p>"
sed -e 's/^\(.*\)___/:\1:/' -e 's/<p>/ /g' < $f-nonul.txt > $f-txt.dict

#echo "Manual cleanup"
#vi $f-txt.dict

echo "Preparing .dict file and index"
dictfmt -j -s "$title" --headword-separator , --utf8 --allchars $f < $f-txt.dict

echo "Compressing with dictzip"
dictzip $f.dict

echo "Done!  $f.dict.dz and $f.index should be ready."
echo "Use 'sudo install -m 644 $f.dict.dz $f.index /usr/share/dictd/'to install."
echo "Run 'sudo dictdconfig -w; sudo /etc/init.d/dictd restart' afterwards."
