#!/bin/bash
#
# Bash script to select a new ispell default dictionary.
# Included as part of the Debian/GNU Linux ispell package.
#
# Kenneth MacDonald <K.MacDonald@ed.ac.uk> September 1995
#
# This script makes extensive use of 'update-alternatives' from the
# dpkg suite of programs.  Priority information for each of the
# alternatives is stored, read and acted upon by 'update-alternatives'.
# IMPORTANT: All ispell dictionary packages should install themselves
# with priority 10.  This script will then assign priority 999 to the
# chosen default, and re-run update-alternatives.

set -e

# Find the current dictionaries on the system, and format into a menu.

get_dictionaries() {
 dictionaries=`/usr/sbin/update-alternatives --display ispell-dictionary.hash \
	| grep priority \
	| sort -r -n -k 4 \
	| sed 's+/usr/lib/ispell/++' \
	| sed 's/\.hash//' \
	| awk '{printf ("\\\t[%d] %s\\\n", NR, $1)}'`

 if [ ! -z "$dictionaries" ]
 then 
  num_dictionaries=`echo -e $dictionaries | grep -c '\[.*\]'`
 else
  num_dictionaries='None'
 fi

}
# ----------------------------------------------------------------------

# Find the current default dictionary, set to None if none found.

get_default () {
 current_default=`/usr/sbin/update-alternatives \
	--display ispell-dictionary.hash \
	| grep 999 \
	| sed 's+/usr/lib/ispell/++' \
	| sed 's/\.hash//' \
	| awk '{print $1}'`

 if [ -z $current_default ]
 then
  current_default='None'
 fi
}

# ----------------------------------------------------------------------

# Keep prompting for default until valid choice made.

choose_default ()
{
 echo -e "\n$dictionaries"

 echo -n "Select the number of the default dictionary [1] "
 read num
 selected_num=${num:-1}

 selected=`echo -e $dictionaries | grep "\[$selected_num\]" | awk '{print $2}'`

 if [ -z $selected ]
 then
   echo -e "\nInvalid choice - try again!\n"
   choose_default
 fi
}

# ----------------------------------------------------------------------

# Promote the selected dictionary to be the default.

make_default ()
{
 echo -n "Making $selected the default ispell dictionary..."

 update-alternatives --quiet --install /usr/lib/ispell/default.hash \
	ispell-dictionary.hash /usr/lib/ispell/$selected.hash 999 \
	--slave /usr/lib/ispell/default.aff ispell-dictionary.aff \
	/usr/lib/ispell/$selected.aff > /dev/null

 echo "done."
}

# ----------------------------------------------------------------------

# Demote the old default dictionary.

demote_default ()
{
 echo -n "Demoting $current_default (old default)..."

 update-alternatives --quiet --install /usr/lib/ispell/default.hash \
	ispell-dictionary.hash /usr/lib/ispell/$current_default.hash 10 \
	--slave /usr/lib/ispell/default.aff ispell-dictionary.aff \
	/usr/lib/ispell/$current_default.aff > /dev/null

 echo "done."
}

# ----------------------------------------------------------------------

echo -e "Please wait while I search for ispell dictionaries..."
get_dictionaries

if [ $num_dictionaries != "None" ]
then

 get_default

 if [ $num_dictionaries != "1" ]
 then
  choose_default
 else
   selected=`echo -e $dictionaries | grep '\[.*\]' | awk '{print $2}'`
   echo There is only one installed dictionary - $selected.
 fi

 if ([ $current_default != 'None' ] && [ $current_default != $selected ])
 then
  demote_default
 fi

 if [ $selected != $current_default ]
 then
  make_default
 else
  echo No change - $selected is already the default.
 fi

else
 echo "WARNING: No ispell dictionaries found - recommended to install one."
 exit
fi

