#!/bin/bash

# Accepts one pair per line, separated by :, dan first, e.g.
# vej:vei

# Run with first argument "inflected" to also accept input with
# inflected forms (not just lemmas), at the cost of more false
# positives to check, e.g.
#
# $ echo 'vejene:veiene' | dev/add-words inflected > new-words.dix

if [[ $1 = inflected ]]; then
    script=add-words-inflected.awk
else
    script=add-words.awk
fi

make -q langs >&2
if [[ $? = 1 ]]; then
    cat 1>&2 <<EOF

                    # [1;31mUNCOMPILED DEPENDENCIES[0m
\$ make -j langs     # [31mDO THIS AND RERUN[0m

EOF
fi


tmpd=$(mktemp -dt apertium-add-words.XXXXXXXXXX)
echo "${tmpd}" >&2

cat >"${tmpd}/bi"

echo "${tmpd}/dan" >&2
cut -f1 -d: "${tmpd}"/bi |apertium-destxt -n |apertium -f none  -d ../apertium-dan dan-morph|apertium-retxt >"${tmpd}"/dan

echo "${tmpd}/nob" >&2
cut -f2 -d: "${tmpd}"/bi |apertium-destxt -n |apertium -f none  -d ../apertium-nob nob-morph|apertium-retxt >"${tmpd}"/nob
echo "${tmpd}/nno" >&2
cut -f2 -d: "${tmpd}"/bi |apertium-destxt -n |apertium -f none  -d ../apertium-nno nno-morph|apertium-retxt >"${tmpd}"/nno

echo "${tmpd}/dan-known" >&2
cut -f1 -d: "${tmpd}"/bi|apertium-destxt -n |apertium -f none -d . dan-nob-morph  |apertium-retxt |grep '^\^[^*+]*\$$' >"${tmpd}"/dan-known
echo "${tmpd}/nob-known" >&2
cut -f2 -d: "${tmpd}"/bi|apertium-destxt -n |apertium -f none -d . nob-dan-morph|apertium-retxt |grep '^\^[^*+]*\$$' >"${tmpd}"/nob-known
echo "${tmpd}/nno-known" >&2
cut -f2 -d: "${tmpd}"/bi|apertium-destxt -n |apertium -f none -d . nno-dan-morph|apertium-retxt |grep '^\^[^*+]*\$$' >"${tmpd}"/nno-known

awk -v tmpd="${tmpd}" -f "$(dirname "$0")"/"${script}" /dev/null
