#!/bin/bash
if ! type hfst-foma-wrapper.sh > /dev/null ; then
    echo "Cannot find hfst-foma-wrapper installed"
    exit 1
fi
getopt="getopts"
if type -p getopt > /dev/null ; then
    getopt=$(type -p getopt)
fi
function print_usage() {
    echo "$0 [-h | -V | -f format | -latin1] [-o OUTFILE] [ROOT [LEXICON...]]"
}

function print_help() {
    print_usage
    echo "Imitates lexc using foma and a wrapper script"
    echo
    echo "Xerox lexc options supported:"
    echo "  -h            print this help"
    echo "  -l(atin1)     convert input to UTF-8 for processing"
    echo "  -u(tf8)       use UTF-8 input (default)"
    echo
    echo "Hfst options:"
    echo "  -V          print version info"
    echo "  -f format   save result in format"
    echo "  -o          output file name"
    echo
    echo "If ROOT file is not given, the root lexicon is read from stdin"
    echo "before starting"
    echo "All unrecognised options are passed to foma verbatim"
}

function print_version() {
    echo "hfst-lexc 0.50.0 (foma 0.9.14)"
    echo "Copyright (c) 2010 University of Helsinki"
    echo "Licence GPLv3: GNU GPL version 3 <http://gnu.org/licenses/gpl.html>"
    echo "This is free software: you are free to change and redistribute it."
    echo "There is NO WARRANTY, to the extent permitted by law."
}

if test "x$getopt" = xgetopts ; then
    while getopts "hvVqf:l:o:u:" flag ; do
        case $flag in
        h) print_help; exit 0;;
        V) print_version; exit 0;;
        v) wrapopts="$wrapopts -v";;
        q) wrapopts="$wrapopts -q";;
        l) latin1=latin1;;
        f) wrapopts="$wrapopts -f $OPTARG";;
        u) utf8=utf8;;
        o) outfile="$OPTARG";;
        \?) echo "Unknown command line switch -$flag$OPTARG, passing on"; extraopts=$"extraopts -$flag$OPTARG";;
        esac
    done
    shift $(($OPTIND-1))
else
    args=$($getopt -o "hvVqf:o:l:u:" -l "help,verbose,version,quiet,format:,output:,latin1::,utf8::" -n hfst-lexc -- "$@")
    if test $? -ne 0 ; then
        print_usage
        exit 1
    fi
    eval set -- "$args"
    while true ; do
        case $1 in
        -h|--help) print_help; exit 0;;
        -V|--version) print_version; exit 0;;
        -v|--verbose) wrapopts="$wrapopts -v"; shift;;
        -q|--quiet) wrapopts="$wrapopts -q"; shift;;
        -l|--latin1) latin1="latin1"; shift;;
        -f|--format) wrapopts="$wrapopts -F $2"; shift 2;;
        -u|--utf8) utf8="utf8";;
        -o|--output) outfile="$2"; shift 2;;
        --) shift; break;;
        *) echo "Unknown command line switch -$flag$OPTARG, passing on"; extraopts=$"extraopts -$flag$OPTARG";;
        esac
    done
    infiles="$@"
fi
if test x$utf8 =  xutf8 ; then
    echo "UTF-8 is already default in $0"
fi
if test "x$latin1" = "xlatin1" ; then
    for f in $@ ; do
        if type recode ; then
            recode l1..u8 $f
        else
            echo "missing recode, cannot use latin1"
            exit 1
        fi
    done
fi

if test -z "$*" ; then
    if test -t 1 ; then
        echo "Reading all lexc files from stdin first"
    fi
fi
cat $@ > $outfile.tmp.lexc
if test -z "$outfile" ; then
    printf "read lexc $outfile.tmp.lexc\nsave stack $$outfile" > $outfile.tmp.lexcscript
else
    printf "read lexc $outfile.tmp.lexc\nsave stack $outfile" > $outfile.tmp.lexcscript
fi
if test -t 1 ; then
    if ! hfst-foma-wrapper.sh $wrapopts -X"-f $outfile.tmp.lexcscript $extraopts"; then
        rm -f "$outfile.tmp.lexcscript" "$outfile" "$outfile.tmp.lexc"
        echo "Foma wrapper failed"
        exit 1
    fi
else
    if ! hfst-foma-wrapper.sh $wrapopts -X"-f $outfile.tmp.lexcscript -p $extraopts" > /dev/null ; then
        rm -f "$outfile.tmp.lexcscript" "$outfile" "$outfile.tmp.lexc"
        echo "Foma wrapper failed"
        exit 1
    fi
fi
if test -z "$outfile" ; then
    cat $$outfile | hfst-name -t 40 -n "hfst-lexc='$*'"
    rm -f $$outfile
else
    cat $outfile | hfst-name -t 40 -n "hfst-lexc='$*'" > $outfile.named
    mv $outfile.named $outfile
fi
rm -f "$outfile.tmp.lexcscript" "$outfile.tmp.lexc"

