#! /bin/bash

source config

function has_space {
  [[ "$1" != "${1%[[:space:]]*}" ]] && return 0 || return 1
}

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

MCD=data/conllu.mcd
MODE=$1
KEYWORD=$2
TEMPLATENAME=$3
EXPNAME=$4

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

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

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

shift
shift
shift
shift

if [ "$KEYWORD" = "." ]
then
  KEYWORD=""
fi

TRAIN=$(find $UD_ROOT*$KEYWORD -type f -name '*train*.conllu')
TRAINRAW=$(find $UD_ROOT*$KEYWORD -type f -name '*train*.txt')
DEV=$(find $UD_ROOT*$KEYWORD -type f -name '*dev*.conllu')
DEVRAW=$(find $UD_ROOT*$KEYWORD -type f -name '*dev*.txt')
TEST=$(find $UD_ROOT*$KEYWORD -type f -name '*test*.conllu')
TESTRAW=$(find $UD_ROOT*$KEYWORD -type f -name '*test*.txt')

if has_space "$TRAIN" || has_space "$DEV" || has_space "$TEST";
then
  >&2 echo "ERROR : more than 1 match with keyword" $KEYWORD
  >&2 echo "TRAIN : " $TRAIN
  >&2 echo "DEV : " $DEV
  >&2 echo "TEST : " $TEST
  print_usage_and_exit
fi

if test ! -f $TRAIN;
then
  >&2 echo "ERROR : no train file found with keyword" $KEYWORD
  >&2 echo "$TRAIN"
  print_usage_and_exit
fi

mkdir -p bin

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

rm -rf bin/$EXPNAME
cp -r $TEMPLATENAME bin/$EXPNAME

EVALCONLL="../scripts/conll18_ud_eval.py"


if [ "$MODE" = "tsv" ]; then
macaon train --model bin/$EXPNAME --mcd $MCD --trainTSV $TRAIN --devTSV $DEV $@ || exit 1
exit 0
fi

if [ "$MODE" = "txt" ]; then
macaon train --model bin/$EXPNAME --mcd $MCD --trainTSV $TRAIN --trainTXT $TRAINRAW --devTSV $DEV --devTXT $DEVRAW $@ || exit 1
exit 0
fi

print_usage_and_exit

