#!/bin/bash

if [ $# -lt 2 ]
then
    echo "Usage:"
    echo "        compdic-fst-to-bin <fstautomaton> <binautomaton>"
    echo "where"
    echo "    <fstautomaton> - file containig automaton in openfst format"
    echo "    <binautomaton> - a file to which the compiled binary automaton (lem/cor/kor format) shoul be written"
    exit 0
fi	

tempdir=`mktemp -d /tmp/compdic.XXXXXX`

alphabet=`tempfile -d $tempdir`

cat <<EOF > $alphabet
<eps> 0
a 1
A 2
 3
 4
 5
b 6
B 7
c 8
C 9
 10
 11
d 12
D 13
e 14
E 15
 16
 17
 18
f 19
F 20
g 21
G 22
h 23
H 24
i 25
I 26
j 27
J 28
k 29
K 30
l 31
L 32
 33
 34
m 35
M 36
n 37
N 38
 39
 40
o 41
O 42
 43
 44
 45
p 46
P 47
q 48
Q 49
r 50
R 51
s 52
S 53
 54
 55
t 56
T 57
u 58
U 59
 60
v 61
V 62
w 63
W 64
x 65
X 66
y 67
Y 68
z 69
Z 70
 71
 72
 73
 74
0 75
1 76
2 77
3 78
4 79
5 80
6 81
7 82
8 83
9 84
_ 85
- 86
? 87
! 88
~ 89
; 90
, 91
/ 92
* 93
+ 94
EOF

cat $1 | fstrmepsilon | fstdeterminize | fstminimize | fstprint --acceptor --isymbols=$alphabet | fsm2aut | aut2fsa > $2
rm -r $tempdir

#echo generating cats file ...

#cat $1 | cut -d ',' -f 2 | sort -u $2.cats
