#!	/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.
#
#
#	Replace all instances of setting first arg. environment variable
#	by setting it to second arg in last line of .cshrc
#
#	samples:
#
#		set_env_var  NAME  'Jerry Berkman'
#		set_env_var  NEWS_READER  pico
#
#	delete all 'setenv $1' from .login and .cshrc
#	watch for both blanks and tabs
#	add new line at end of .cshrc
#
#	special case: for EDITOR, also delete VISUAL
#
#	make files in /tmp and then copy to home dir & then mv to
#		be able to detect home directory quota problems
#
if( $#argv != 2 ) then
	echo "*** invalid call to $0 ***"
	exit -1
endif
cd
#
set temp  = `mktemp -c`
set templ = `mktemp -c -p.login -d ~`
set tempc = `mktemp -c -p.cshrc -d ~`

set ll = ~/.login
if( ! -e $ll ) set ll = /usr/skel/DOT/.login
if( $1 == EDITOR ) then
	egrep -v "^[ 	]*setenv[ 	][ 	]*(EDITOR|VISUAL)[ 	]" < $ll >! $temp
else
	egrep -v "^[ 	]*setenv[ 	][ 	]*$1[ 	]" < $ll >! $temp
endif
/bin/cp $temp $templ
if( $status ) goto quota_err

set cc = ~/.cshrc
if( ! -e $cc ) set cc = /usr/skel/DOT/.cshrc
if( $1 == EDITOR ) then
	egrep -v "^[ 	]*setenv[ 	][ 	]*(EDITOR|VISUAL)[ 	]" < $cc >! $temp
else
	egrep -v "^[ 	]*setenv[ 	][ 	]*$1[ 	]" < $cc >! $temp
endif
echo "setenv $1 '$2'" >> $temp
/bin/cp $temp $tempc
if( $status ) goto quota_err

/bin/mv -f $templ ~/.login
/bin/mv -f $tempc ~/.cshrc
/bin/rm $temp
exit 0

#======================================================

quota_err:

cat << EOT

The requested operation could not be completed, probably due
to disk quota problems.

EOT
/bin/rm -f $temp $templ $tempc
exit 1
