#!/bin/bash

# Check the spelling in the POD content.
#
# For each file run pod2text and then run ispell over it. We use a per-file
# personal dictionary. Because we've used pod2text, you also need to correct
# the spelling in the original file.
#
# When cheking spelling, if you i(nsert) an unknown spelling rather than
# a(ccept) it, then the next time you run this script there should word
# rather than there should be no misspellings found.
#
# In additional to making a patch in the package to fix the misspellings,
# when there are enough corrections they should be submitted to Best
# Practical as a merge request.

if [ ! -f debian/rules ]
then
    print Run this from the base directory of the package
    exit 1
fi

function check_spelling {
    file=$1

    dir=$(dirname $file)

    basefile=$(basename $file)
    pod2text $file > $file.txt

    # ispell won't use the personal dictionary if it doesn't exist.
    mkdir -p debian/maint/ispell-dicts/$dir
    touch debian/maint/ispell-dicts/$dir/$basefile
    ispell -p debian/maint/ispell-dicts/$dir/$basefile -x $file.txt
    rm $file.txt
}

for dir in bin sbin
do
    for file in $dir/*.in
    do
        check_spelling $file
    done
done

for file in $(find devel -type f)
do
    check_spelling $file
done

for file in $(find lib -type f)
do
    check_spelling $file
done

for file in $(find docs -type f \( -name "*.pod" -or -name "UPGRADING*" \))
do
    check_spelling $file
done
