#!/bin/bash
##############################################################################
#                               sed replacement list
#                                       srl
#                        create list of replacement for sed
##############################################################################

if test -z "$1"
then
    echo "Usage: $0 [-l lang_code] <file>
    -l lang_code        language code. Same format of the LANG environmental variable
             on most systems (see aspell for further information)
    -m <min>        set minimal occurrence of a mistake to <min>
    Creates a replacement table to be used with sed.
    $0 will prompt for each mistake and ask for a replacement. It then
    writes the replacements to the file 'repl.txt' to be used with sed (sed -f repl.txt <file>)

"
exit
fi

LANGUAGE=${LANG%.*}

while getopts ":l:m:" Option
do
    case $Option in
    l)    #language
        LANGUAGE="$OPTARG"
        echo $LANGUAGE
    ;;
    m) # minimum occurrence
        MIN=$OPTARG
;;
    esac
done

shift $(($OPTIND - 1))

#select I in  quit `cat $1|aspell list|sort|uniq`
echo -e "The number behind the misspelled word is the occurrence in the text.
Language is $LANGUAGE
(Ignore highlighted text within words. Only whole words will be replaced later!\n"

for I in  `cat "$1"|aspell list --lang="$LANGUAGE"|sort|uniq`
do
    IMIN=$(grep -w "$I" "$1"|wc -l)
    [[ "$IMIN" -lt "$MIN" ]] && continue        # mininmal occurence
    echo "$I" [$IMIN]
        echo "-------------------"
        A=`grep -n -w "$I" "$1"| sed 's|'"$I"'|\\\033\[7m'"&"'\\\033\[27m'"|g"`
        echo -e  "$A"
        echo "-------------------"
        echo "enter replacement [enter for next]: "
        read A
        if test -z "$A"
            then
            continue
            else
            echo "s|$I|$A|g" >> repl.txt
        fi   
  
done

echo -e "\nUse sed -f repl.txt <file> to apply the replacements"