#!/bin/bash

# Accepts one pair per line, separated by :, swe first, e.g.
# bustad:bolig

# 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 'bustadene:boligene' | 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
if [[ $? = 1 ]]; then
    echo "make -j langs # do this first" 1>&2
fi


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

cat >"${tmpd}/bi"

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

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

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

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