#!/usr/bin/env bash
#
# addict - a script to add a dictionary to aspell:
#
#  * The first argument: add to *what language*'s
#    spellchecker (e.g., "sv" for Swedish, "en" for
#    English, and so on).
#
#  * The second argument: the *dictionary file* that should
#    be added. That file should look like this:
#
#       fool
#       draw
#       counter
#
#    - that is, one line for each word.
#
#   It'll look the best if you use a file without an
#   extension.
#
#   The file will be sorted (does that help?).
#
# This script has to be run with sudo as it operates in
# /usr/lib/aspell (not in $HOME).
#
# This script can be useful to add specialized
# dictionaries, e.g., a list of computer lingo, or
# whatever stuff that is often not found in
# dictionaries.
#
# For a small amount of words, just append a list to
# ~/.aspell.sv.pws instead - to compress just a few
# words is not worth the effort.
#
# Tested on Debian sid Tue Oct 2012 (30/10) 00:34
#
# Update 1: Tested Fri, May 13 (17/5) 23:54.
# Note:     For this script to work, cd to the
#           directory of the new word list that you
#           wish to insert.
#
# Update 2, 17/07/2014:
#
# On Debian, the Swedish dictionary for aspell is
# called aspell-sv. This is 164 k. There is also an
# ispell list, called iswedish, which is - hold tight -
# 1 252 k, with 120 000 words. iswedish also suggests
# the package wswedish which is 1 336 k and (also) of
# 120 000 words (?). Be that as it may, it/they seems
# like more data than aspell-sv... (ispell is also much
# faster than aspell in Emacs.)
#
# Check out:
#
#   http://user.it.uu.se/~embe8573/conf/emacs-init/new-spell.el
#
# for how to make it work in Emacs.

LANGUAGE=$1
LIST=$2

# sort the list of new words
SORTED=sorted_${LIST}
sort "${LIST}" >"${SORTED}"

# compress the sorted list
ASPELL_DICT=${LIST}.rws
aspell -l "${LANGUAGE}" create master "${ASPELL_DICT}" <"${SORTED}"

# add the compressed dictionary to the language's dictionary
ASPELL_PATH=/usr/lib/aspell/
echo "add ${ASPELL_DICT}" >>"${ASPELL_PATH}${LANGUAGE}".multi

rm "${SORTED}"
