#!/bin/bash

case $# in
0|1|2) echo "u8myspell - converts MySpell dictionaries to UTF-8
usage: u8myspell source_name output_name source_charset"; exit 1;;
esac

i=$1
o=$2
charset=$3
localdir="$(dirname $0)"

paste <(sed 's#/.*##' $i.dic | recode ${charset}..u8) <(sed 's#^[^/]*##' $i.dic) |
sed "s#/`echo -e '\t'`##" | sed -f "$localdir"/l1_u8.sed >$o.dic

cat $i.aff | awk '
/^SET/{
print "SET UTF-8" >"u8.beg"
print "" >"u8.mid"
print "" >"u8.end"
next
}
/^(TRY|COMPOUNDSYLLABLE|ACCENT|REP|MAP|CHECKCOMPOUND)/{
print $1 >"u8.beg"
print $2,$3,$4,$5,$6 >"u8.mid"
print "" >"u8.end"
next
}
/^[SP]FX/&&(NF>4){print $1,$2 >"u8.beg";
print $3,$4,$5 >"u8.mid";print $6,$7 >"u8.end";next}
{print >"u8.beg";print"">"u8.mid";print"">"u8.end"}'
recode ${charset}..u8 u8.mid
paste u8.beg u8.mid u8.end | tr '\t' ' ' | sed -f "$localdir"/l1_u8.sed >$o.aff
rm u8.{beg,mid,end}
