#!/bin/sh

#
# This script creates a new dictionary by expanding the original,
# Mozilla's, and the upstream dictionary to remove affix flags and
# then doing the wordlist equivalent of diff3 to create a new
# dictionary.
#
# The files 2-mozilla-add and 2-mozilla-rem contain words added and
# removed, receptively in the Mozilla dictionary.  The final
# dictionary will be in hunspell-en_US-mozilla.zip.
#

set -e

export LANG=C
export LC_ALL=C
export LC_CTYPE=C
export LC_COLLATE=C

WKDIR="`pwd`"

export SCOWL="$WKDIR/scowl/"

ORIG="$WKDIR/orig/"
SPELLER="$SCOWL/speller"

expand() {
  grep -v '^[0-9]\+$' | $SPELLER/munch-list expand $1 | sort -u
}

cd $SPELLER
./make-hunspell-dict -all > ./make-hunspell-dict.log
cd $WKDIR

expand $SPELLER/en.aff < $SPELLER/en.dic.supp > 0-special

expand $ORIG/en_US.aff < $ORIG/en_US.dic > 1-base.txt

expand ../en-US.aff < ../en-US.dic > 2-mozilla.txt

expand $SPELLER/en.aff < $SPELLER/en_US.dic   > 3-upstream.txt

comm -23 1-base.txt 2-mozilla.txt > 2-mozilla-rem
comm -13 1-base.txt 2-mozilla.txt > 2-mozilla-add
comm -23 3-upstream.txt 2-mozilla-rem | cat - 2-mozilla-add | sort -u > 4-patched.txt

cat 4-patched.txt | comm -23 - 0-special | $SPELLER/make-hunspell-dict -one en_US-mozilla /dev/null

# sanity check should yield identical results
#comm -23 1-base.txt 3-upstream.txt > 3-upstream-rem
#comm -13 1-base.txt 3-upstream.txt > 3-upstream-add
#comm -23 2-mozilla.txt 3-upstream-rem | cat - 3-upstream-add | sort -u > 4-patched-v2.txt

expand ../en-US.aff < mozilla-specific.txt > 5-mozilla-specific

comm -12 3-upstream.txt 2-mozilla-rem > 5-mozilla-removed
comm -13 3-upstream.txt 2-mozilla-add > 5-mozilla-added
