#! /bin/sh

# Convert a binary OpenFST file into an efsm textual file.
me=$(basename "$0")
medir=$(mktemp -d "/tmp/$me.XXXXXX") || exit 1

# We are going to read the input several times.  Be sure to save it,
# in case it were stdin.
input=$medir/a.fst
cat "$@" >$input

isyms=$medir/isymbols.txt
osyms=$medir/osymbols.txt
trans=$medir/transitions.fsm

# Is this an acceptor?
#
# fstprint features an "acceptor" y/n field.  Unfortunately it merely
# checks whether the input and output symbols are always equal (so
# it's unrelated to the --acceptor flag of fstcompile), and this check
# is performed on the symbol numbers only, not even checking if the
# symbol tables actually match (i.e., "(a,x)*" will actually be
# considered an "acceptor" if a and x are both numbered 1).
#
# Therefore check that transitions have equal symbol numbers and that
# symbol tables are equal.
fstprint --save_isymbols=$isyms \
         --save_osymbols=$osyms $input > $trans
if fstinfo $input | grep -q '^acceptor  *y$' && cmp $isyms $osyms; then
  acceptor=true
  fstprint --acceptor \
           --save_isymbols=$isyms $input > $trans
else
  acceptor=false
fi

# Use $(sort...) to sort symbols by number, not by value.
if $acceptor; then
  cat <<EOF
#! /bin/sh

me=\$(basename "\$0")
medir=\$(mktemp -d "/tmp/\$me.XXXXXX") || exit 1

cat >\$medir/symbols.txt <<\EOFSM
$(sort -n -k2 $isyms)
EOFSM

cat >\$medir/transitions.fsm <<\EOFSM
$(cat $trans)
EOFSM

fstcompile --acceptor \\
  --keep_isymbols --isymbols=\$medir/symbols.txt \\
  --keep_osymbols --osymbols=\$medir/symbols.txt \\
  \$medir/transitions.fsm "\$@"
sta=\$?

rm -rf \$medir
exit \$sta
EOF
else
  cat <<EOF
#! /bin/sh

me=\$(basename "\$0")
medir=\$(mktemp -d "/tmp/\$me.XXXXXX") || exit 1

cat >\$medir/isymbols.txt <<\EOFSM
$(sort -n -k2 $isyms)
EOFSM

cat >\$medir/osymbols.txt <<\EOFSM
$(sort -n -k2 $osyms)
EOFSM

cat >\$medir/transitions.fsm <<\EOFSM
$(cat $trans)
EOFSM

fstcompile \\
  --keep_isymbols --isymbols=\$medir/isymbols.txt \\
  --keep_osymbols --osymbols=\$medir/osymbols.txt \\
  \$medir/transitions.fsm "\$@"
sta=\$?

rm -rf \$medir
exit \$sta
EOF
fi

rm -rf $medir
