#! /usr/bin/env bash

function print_usage_and_exit {
  >&2 echo "USAGE : (tsv | txt) expPath [arguments]"
  exit 1
}

MODE=$1
EXPPATH=$2

if [ -z "$MODE" ];
then
  >&2 echo "ERROR : missing argument 1 (mode)"
  print_usage_and_exit
fi

if [ -z "$EXPPATH" ];
then
  >&2 echo "ERROR : missing argument 2 (expPath)"
  print_usage_and_exit
fi

shift
shift

if [ ! -d "$EXPPATH" ]; then
  >&2 echo "ERROR : directory $EXPPATH doesn't exist"
  print_usage_and_exit
fi

TRAIN=$EXPPATH"/data/train.conllu"
TRAINRAW=$EXPPATH"/data/train.txt"
DEV=$EXPPATH"/data/dev.conllu"
DEVRAW=$EXPPATH"/data/dev.txt"
TEST=$EXPPATH"/data/test.conllu"
TESTRAW=$EXPPATH"/data/test.txt"

REF=$TEST
REFRAW=$TESTRAW

if test ! -f $REF;
then
  >&2 echo "ERROR : no ref file found in" $CORPUS
  >&2 echo "$REF"
  print_usage_and_exit
fi
if test ! -f $REFRAW;
then
  >&2 echo "ERROR : no ref file found in" $CORPUS
  >&2 echo "$REFRAW"
  print_usage_and_exit
fi

EVALCONLL="../scripts/conll18_ud_eval.py"
OUTPUT=$EXPPATH"/predicted_eval.tsv"

if [ "$MODE" = "tsv" ]; then
macaon decode --model $EXPPATH --inputTSV $REF $@ > $OUTPUT && $EVALCONLL $REF $OUTPUT || exit 1
exit 0
fi

if [ "$MODE" = "txt" ]; then
macaon decode --model $EXPPATH --inputTXT $REFRAW $@ > $OUTPUT && $EVALCONLL $REF $OUTPUT || exit 1
exit 0
fi

print_usage_and_exit

