#!/bin/bash

rm -f words.$1*

shopt -s nullglob

max_level=`src/get-level max`

if [ $1 -ne $max_level ]
then

  add_affixes="`src/get-all-below $1 add-affixes`"

  cat `src/get-all-below $1 supplement` $add_affixes l/include/$1/* \
    | src/filter | sort | uniq > working/words.$1.tmp

  if [ -n "$add_affixes" ] && [ $1 -le `cat l/add-affixes/max-level` ]
  then
    echo "Adding affixes"
    cat $add_affixes | src/add-affixes >> working/words.$1.tmp
  fi

  src/add-possessive working/words.$1.tmp

  cat working/words.$1.tmp    \
    | src/filter              \
    | src/add-other-spellings \
    | src/add-other-forms     \
    | src/add-other-spellings \
    | sort -u                 \
    | comm -12  - working/words.`src/get-level next $1` \
    > working/words.$1

  rm working/words.$1.tmp

else

  cat working/all.lst           \
    | src/add-possessive        \
    | src/add-other-spellings   \
    | sort -u                   \
    > working/words.$1

fi


