#!/bin/bash -e
#
# postinst configuration file for Debian witalian package.
# $Id: postinst,v 1.8 1998/04/09 15:38:39 salve Exp $
#
# Copyright  Davide G. M. Salvetti <salve@debian.org>, 1997, 1998.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to: The Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#
# On Debian GNU/Linux System you can find a copy of the GNU General Public
# License in /usr/doc/copyright/GPL.

set -o posix

LANGUAGE=italian
dict=/etc/dictionary
words=/usr/dict/words

case "$1" in
    configure)
	# Test for $dict to exist.
	if [ ! -e "$dict" ]; then
	    # Test for not existing $dict to be a dangling symlink.
	    if [ -L "$dict" ]; then
		mv -bf $dict $dict.old
		echo "\`$dict' was a dangling symlink: renamed as \`$dict.old'."
	    fi
	    echo "There is no default dictionary selected."
	    unset YesNo || true
	    while [ "$YesNo" != "y" -a "$YesNo" != "n" ]; do
		read -e -p "Would you like $LANGUAGE to be the default dictionary (y/N)? " YesNo
		if [ -z "$YesNo" ]; then
		    # Default value.
		    YesNo=n
		fi
		# Get the first character, translated to lowercase.
		YesNo=$(echo ${YesNo:0:1} | tr YN yn)
	    done
	    if [ "$YesNo" == "y" ]; then
		ln -snf /usr/dict/$LANGUAGE $dict
		echo "Made $LANGUAGE the default dictionary.  (You can change this by"
		echo "adjusting the symbolic link \`$dict' --- see \`man ln'.)";
	    fi
	# Test for existing $dict to be a symlink.
	elif [ ! -L "$dict" ]; then
	    echo "The file \`$dict' should be a symbolic link to an existing"
	    echo "dictionary in \`/usr/dict/'.  This is its current state instead:"
	    echo "$(ls --format=long $dict)"
	    echo "You should probably change it --- see \`man ln'."
	fi

	# Explain how to change the default if $LANGUAGE is not the default itself.
	if [ "$dict" != \
		"$(find $dict -lname "/usr/dict/$LANGUAGE" 2>/dev/null)" ]; then
	    echo "If you would like to use \`$LANGUAGE' for the default dictionary, please do:"
	    echo -e "\tln -snbf /usr/dict/$LANGUAGE $dict"
	fi

	# Test for $words to be a symbolic link to $dict.
	if [ "$words" != \
		"$(find $words -lname "$dict" 2>/dev/null)" ]; then
	    # Test for $words to exist.
	    if [ ! -e "$words" ]; then
		# Test for not existing $words to be a dangling symlink.
		if [ -L "$words" ]; then
		    mv -bf $words $words.old
		    echo "\`$words' was a dangling symlink: renamed as \`$words.old'."
		fi
		ln -snf $dict $words
		echo "Made $words a symbolic link pointing to $dict."
	    else
		echo "The file \`$words' should be a symbolic link to \`$dict'."
		echo -e "This is its current state:\n$(ls --format=long $words)"
		unset YesNo || true
		while [ "$YesNo" != "y" -a "$YesNo" != "n" ]; do
		    read -e -p "Do you allow me to fix it (Y/n)? " YesNo
		    if [ -z "$YesNo" ]; then
			# Default value.
			YesNo=y
		    fi
		    # Get the first character, translated to lowercase.
		    YesNo=$(echo ${YesNo:0:1} | tr YN yn)
		done
		if [ "$YesNo" == "y" ]; then
		    mv -bf $words $words.old
		    echo "The file \`$words' was renamed as \`$words.old'."
		    ln -snf $dict $words
		    echo "Made \`$words' a symbolic link pointing to \`$dict'."
		fi
	    fi
	fi
    ;;
    abort-upgrade|abort-deconfigure|abort-remove)
	:
    ;;
    *)
	echo "${0##*/}: Called with unknown argument \`$1'." >&2
    ;;
esac

exit 0

### Local Variables:
### mode: shell-script
### End:
