Your IP : 216.73.216.155


Current Path : /usr/sbin/
Upload File :
Current File : //usr/sbin/update-ispell-dictionary

#!/bin/sh
# $Id: update-ispell-dictionary,v 1.9 2003/06/17 14:27:17 david Exp $
#
# 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.

# note we use "/bin/echo -e" because -e is not standard (e.g.
# ash builting echo doesn't support it).

set -e

if [ $(id -u) != 0 ]; then
  echo $0: You must run this as root.
  exit 1
fi

NEWERSCRIPT=/usr/sbin/select-default-ispell

# If $NEWERSCRIPT (provided by the dictionaries-common
# package) is there, use that instead; the previous methods (the
# rest of this file) are obsolete.

if [ -x $NEWERSCRIPT ] ; then
    echo $0 is now obsolete, and will eventually
    echo be removed.  I am running its replacement,
    echo $NEWERSCRIPT, for you...
    sh -c $NEWERSCRIPT
    exit $?
fi

# Do nothing if running in noninteractive mode
DEBIAN_FRONTEND=`echo "$DEBIAN_FRONTEND" | tr A-Z a-z`
if [ "$DEBIAN_FRONTEND" = "noninteractive" ]; then
    echo "$0: Running in noninteractive mode.  Not doing anything."
    exit 0
fi

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

get_dictionaries() {
 dictionaries=`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=`/bin/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=`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 ()
{
 /bin/echo
 /bin/echo -e $dictionaries

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

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

 if [ -z $selected ]
 then
   /bin/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."
}

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

/bin/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=`/bin/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 -- you should install one."
 exit
fi