#!/bin/bash
#
# Copyright (C) 2012-2015 SUSE Linux GmbH
#
# Author:
# Frank Sundermeyer <fsundermeyer at opensuse dot org>
#
function print_help {
    cat <<EOF_helptext
$ME [options] FILELIST

Spell checks DocBook XML files with aspell. By default uses the standard
American English dictionary plus an additional dictionary with SUSE terminology.

$ME supports two modes:
  * by default runs the interactive spell checker
  * if called with --list, dumps a sorted list of misspelled words to
    standard output

Options:
  --dict=DICTIONARY               Specify an additional (custom) dictionary
                                  (absolute path). By default the English
                                  SUSE Terminology dictionary
                                  (/usr/share/daps/lib/suse_aspell.rws) is used.
                                  Specify --dict="" if you do not want to use
                                  any additional dictionary.
  --help, -h                      Display this help
  --lang=LANGUAGE, -l LANGUAGE    Language to use. It uses the same format as
                                  the LANG environmental variable: It consists
                                  of  the two letter ISO 639 language code
                                  (e.g. 'en' for English) and an optional two
                                  letter ISO 3166 country code after an
                                  underscore (e.g. en_GB for Britsh English).
                                  Make sure the respective dictionary for
                                  aspell is installed.
                                  Default: en_US
  --list                          dumps a sorted list of misspelled words to
                                  standard output instead of starting the
                                  interactive spellchecker
EOF_helptext
}

#----------------------------------------------------------------------
# Variables
#

# cleanup
unset LIST

# default values
LANGUAGE=en_US
ARCH=$(getconf LONG_BIT)
if [[ 64 = "$ARCH" ]]; then
    LIBDIR=/usr/lib64
else
    LIBDIR=/usr/lib
fi
EXTRA_DICT="$LIBDIR/aspell-0.60/suse_aspell.rws"

# ignore text within these tags
declare -a SKIPTAGS=(
    --add-sgml-skip=author
    --add-sgml-skip=command
    --add-sgml-skip=email
    --add-sgml-skip=envar
    --add-sgml-skip=filename
    --add-sgml-skip=firstname
    --add-sgml-skip=guimenu
    --add-sgml-skip=keycap
    --add-sgml-skip=literal
    --add-sgml-skip=option
    --add-sgml-skip=remark
    --add-sgml-skip=screen
    --add-sgml-skip=surname
    --add-sgml-skip=systemitem
    --add-sgml-skip=ulink
    --add-sgml-skip=varname
    --add-sgml-skip=xref
)

#----------------------------------------------------------------------
# printing help / catching errors
#
ME=$(basename "$0")

ARGS=$(getopt -o hl: -l dict:,help,lang:,list -n "$ME" -- "$@")
eval set -- "$ARGS"
while true ; do
    case "$1" in
        --dict)
            if [[ -z $2 ]]; then
                unset EXTRA_DICT
            elif [[ -f $2 ]]; then
                EXTRA_DICT="$2"
            else
                ccecho "error" "Extra dictionary $2 doe not exist" >&2
                exit 1
            fi
            shift 2
            ;;
        -h|--help)
            print_help
            exit
            ;;
        -l|--lang)
            LANGUAGE=$2
            shift 2
            ;;
        --list)
            LIST=1
            shift
            ;;
        --)
            shift
            break
            ;;
    esac
done

if [[ -z $1 ]]; then
    ccecho "error" "Error: Please specify a valid filename" >&2
    exit 1
fi

for FILE in "$@"; do
    test -f "$FILE" || ccecho "warn" "File $FILE does not exist"
    if [[ ! $LIST ]]; then
        echo "Checking $FILE..."
        /usr/bin/aspell --mode=sgml "${SKIPTAGS[@]}" --encoding=utf-8 \
            --lang="$LANGUAGE" --extra-dicts="$EXTRA_DICT" -c "$FILE"
    else
        /usr/bin/aspell --mode=sgml "${SKIPTAGS[@]}" --encoding=utf-8 \
            --lang="$LANGUAGE" --extra-dicts="$EXTRA_DICT" list < $"FILE" | sort -u
    fi
done
