#!	/bin/csh -f
#
# Copyright 1993, 1994, 1995 by the Regents of the University of California.
# see the file "Copyright" in the distribution for conditions of use.
#
#
#	select a default editor
#
#		1 - get editor from the environment & display intro
#
set ed = ( $EDITOR )
#		try to keep the following under 24 lines ...
cat << EOT

This menu item allows you to specify the editor you want to use
with Pine, Usenet news, editing files, setting up a plan, etc.

If you do not know an editor, Pico is the easiest to learn, and is
the default for new UCLink accounts.

The other available editors are vi, jove, and emacs.  All are much
harder to learn than Pico.

This account is currently set up to use the $ed editor.
EOT
#
#=====================================================================
#		2 - get user's choice of editor
#
reread1:
echo ' '
echo Enter your choice for your default editor: pico, vi, jove, or emacs.
echo -n "Press Return for no change: "

set x = $<
set y = ( $x )
if( $#y == 0 ) exit
if( $#y > 1 ) x = "ERROR"
if( $x != "pico" && $x != "jove" && $x != "emacs" && $x != "vi" ) then
	echo 'You must enter pico, jove, vi, or emacs or Return.'
	goto reread1
endif
#	user entered a valid editor name
set ed = $x
#
#=====================================================================
#		3 - replace EDITOR in .login and .cshrc
#
set_env_var EDITOR $ed
if( $status ) exit 1

cat << EOT

$ed is now saved as your default editor.

EOT
#=====================================================================
#		4 - No pre-existing .pinerc
#
if( ! -e ~/.pinerc ) then
	if( $ed == "pico" ) goto done
	echo Apparently you have not yet used Pine.  If you want to specify $ed
	echo as the alternate editor for Pine, you need to invoke this
	echo menu entry again, after you have used Pine.
	goto done
endif
#=====================================================================
#		5 - Get pine alternate editor name from .pinerc

set pine_ed = `awk -F= 'BEGIN { ed = "NONE" } { if( $1 == "editor" && NF > 1 && $2 != "" ) ed = $2; } END { print ed }' < ~/.pinerc`

set tempp = `mktemp -c -p.pinerc -d ~`
set temp = `mktemp -c`

if( $ed != "pico" ) goto pine_other

#=====================================================================
#		6 - if want pico, get rid of alternate editor in .pinerc

if( $pine_ed == "NONE" ) goto done

#	replace "editor=..." by "editor="
sed -e 's/^editor=.*/editor=/' < ~/.pinerc > $temp
/bin/cp $temp $tempp
if( $status ) goto quota_err
/bin/mv $tempp ~/.pinerc
echo Since Pico is the built in editor for Pine, your previous specification
echo of an alternate editor for Pine has been deleted from the Pine
echo control file.
goto done

#=====================================================================
#		7 - specify jove/emacs/vi as alternate editor for Pine

pine_other:
if( $pine_ed == $ed ) then
	echo Your Pine control file already indicates $ed as the alternate
	echo editor so no further change is necessary.
	goto done
endif

sed -e "s/^editor=.*/editor=$ed/" < ~/.pinerc > $temp
/bin/cp $temp $tempp
if( $status ) goto quota_err
/bin/mv $tempp ~/.pinerc

cat << EOT
$ed is now specified as the alternate editor for Pine.
Invoke it by typing control-underscore  while in the "Message Text"
area of the Pine screen.

To show that you have selected an alternate editor, Pine replaces
"^W Where" by "^_ Alt Ed" in the menu at the bottom of the screen.
You can still use "^W" even though it isn't displayed in the menu.

EOT
goto done

quota_err:
cat << "EOT"

*** The update could not be done, probably due to quota errors.

"EOT"


done:
if( $?temp ) /bin/rm -f $temp
if( $?tempp ) /bin/rm -f $tempp
