#!/bin/sh

# To generate localization information, run:
#   xgettext -o - -L Shell select-editor

PATH=$PATH:/usr/sbin

# Ensure that $HOME/.selected_editor is writeable
touch $HOME/.selected_editor || exit 1

editors=`update-alternatives --list editor | wc -l`
if [ $editors -gt 1 ]; then
	echo
	echo "`gettext 'Select an editor.  To change later, run'`" "'select-editor'."
	i=0
	editors=`update-alternatives --list editor`
	for e in $editors; do
		i=`expr $i + 1`
		desc=
		if [ $e = "/bin/nano" ]; then
			desc="        <---- ` gettext 'easiest'`"
			simple=$i
		fi
		echo "  $i. $e$desc"
	done
	echo ""
	selected=x
	while /bin/true; do
		if [ -z "$selected" -a ! -z "$simple" ]; then
			selected="$simple"
		elif ! test $selected -gt 0 2>/dev/null; then
			read -p "`gettext 'Choose'` 1-$i [$simple]: " -r selected
		elif ! test $selected -le $i 2>/dev/null; then
			read -p "`gettext 'Choose'` 1-$i [$simple]: " -r selected
		else
			break
		fi
	done
	i=0
	for e in $editors; do
		i=`expr $i + 1`
		if [ $i -eq $selected ]; then
			echo "# Generated by /usr/bin/select-editor" > $HOME/.selected_editor
			echo "SELECTED_EDITOR=\"$e\"" >> $HOME/.selected_editor && exit 0
		fi
	done
fi
exit 1
