#
# Copyright (c) 1990 Universities Research Association, Inc.        
#       All Rights Reserved                                                    
#
#
# This material resulted from work developed under a Government Contract and   
# is subject to the following license:  The Government retains a paid-up,      
# nonexclusive, irrevocable worldwide license to reproduce, prepare derivative 
# works, perform publicly and display publicly by or for the Government,       
# including the right to distribute to other Government contractors.  Neither  
# the United States nor the United States Department of Energy, nor any of     
# their employees, makes any warrenty, express or implied, or assumes any      
# legal liability or responsibility for the accuracy, completeness, or         
# usefulness of any information, apparatus, product, or process disclosed, or  
# represents that its use would not infringe privately owned rights.

# @(#) shell 1.5 Delta: 94/06/09 16:41:22 Extraction 97/07/16 11:19:23 @(#)
. ./sys.env
#
# Uncomment PASSWD and YPDIR for testing of base shell script alone
#
#PASSWD=Xpasswd
#YPDIR=""
F=`$BASENAME $0`
C=`/bin/pwd`
SHELLFILE=shells
INTERACTIVE=0
NONINTERACTIVE=1
trap '/bin/rm -fr /tmp/$$.*' 0 1 2 3 4 5 6 7 8 10 12 13 15
umask 077
if [ "$PASSWD" = "NULL" ]
then
	$ECHO "$F: system not allowed to modify passwd file"
	exit 1
fi
$CMDBIN_DIR/bin/getlock $$ $SYSTOOLS_DIR/locks moduser
if [ "$?" -ne 0 ]
then
	$ECHO "$F: moduser lock is not available, wait and try again"
	exit 1
fi
if [ "$CMDNAME" = "root" ]
then
	if [ $# -ne 1 -a $# -ne 2 ]
	then
		$ECHO "$F: usage: cmd $F loginname [ shell ]"
		exit 1
	fi
	login="$1"
	if [ -z "$login" -o `$EXPR $login : '[a-zA-Z_][a-zA-Z0-9_-]*$'` -eq 0 ]
	then
		$ECHO "$F: loginname, $login, must be leading alpha alphanumeric"
		exit 1
	fi

	if [ $# = 2 ]
	then
		mode=$NONINTERACTIVE
		shell=$2
	else
		mode=$INTERACTIVE
	fi
else
	if [ $# -ne 0 -a $# -ne 1 ]
	then
		$ECHO "$F: usage: cmd $F [ shell ]"
		exit 1
	fi
	login="$CMDNAME"
	if [ -z "$login" -o `$EXPR $login : '[a-zA-Z_][a-zA-Z0-9_-]*$'` -eq 0 ]
	then
		$ECHO "$F: loginname, $login, must be leading alpha alphanumeric"
		exit 1
	fi

	if [ $# = 1 ]
	then
		mode=$NONINTERACTIVE
		shell=$1
	else
		mode=$INTERACTIVE
	fi
fi

nuid=`$EGREP -c "^$login:" $PASSWD`
if [ $nuid -le 0 ]
then
	$ECHO "$F: Sorry, there is no $login login on the system"
	exit 1
fi
if [ $nuid -gt 1 ]
then
	$ECHO "$F: Sorry, there are more than one $login login on the system"
	$ECHO "$F: This situation needs to brought to the attention of the" \
		"system manager."
	exit 1
else
	$EGREP "^$login:" $PASSWD >/tmp/$$.login
fi
uid=`$CUT -d: -f3 /tmp/$$.login`
uid=`$EXPR $uid + 0`
currentshell=`$CUT -d: -f7 /tmp/$$.login`
# Do not allow a user to change someone elses login shell unless he/she logged
# in as root or master
if [ "$CMDNAME" != "$login" -a "$CMDNAME" != "root" ]
then
	$ECHO "$F: Sorry, you don't have permission to do that"
	exit 1
fi
if [ ! -r $SHELLFILE ]
then
	$ECHO "$F: Sorry, can't read file $SHELLFILE"
	exit 1
fi
$ECHO "Current shell is \"$currentshell\""
touch /tmp/$$.shells
$SORT -u $SHELLFILE | while read shell
do
	if [  -z "$shell" ]
	then
		continue
	fi
	if [ `$EXPR "$shell" : '\(.\).*'` = '#' ]
	then
		continue
	fi
	if [ -r "$shell" -a -x "$shell" ]
	then
		$ECHO "$shell" >>/tmp/$$.shells
	else
		$ECHO "$F: Yeeks, bogus shell, $shell, in shells list." \
			"Notify the system manager."
	fi
done
if [ "$mode" = "$NONINTERACTIVE" ]
then
	#
	# NONINTERACTIVE version
	#
	if [ "$currentshell" = "$shell" ]
	then
		$ECHO "$F: No change of shell needed"
		exit 0
	fi
	if [ `$EGREP -c "^$shell\$" /tmp/$$.shells` -gt 0 ]
	then
		/bin/ed -s $PASSWD <<!EOF! > /dev/null
/^$login:[^:]*:$uid:/s-\\([^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\).*-\\1${shell}
w
q
!EOF!
		$ECHO "$F: Login shell changed to $shell for $login"
	else
		$ECHO "$F: $shell: Invalid Shell"
		exit 1
	fi
else
	#
	# INTERACTIVE version
	#
	$ECHO
	count=0
	$SORT -u /tmp/$$.shells | while read i
	do
		count=`$EXPR $count + 1`
		$ECHO $count $i
	done
	$ECHO
	$ECHO "Enter number of desired login shell-> \c"
	read number
	if [ `$EXPR "$number" : '[0-9][0-9]*$'` -eq 0 ]
	then
		$ECHO "$F: answer must be numeric integer"
		exit 1
	fi
	$ECHO
	if [ "$number" -lt "1" -o \
	     "$number" -gt `$SORT -u /tmp/$$.shells | wc -l` ]
	then
		$ECHO "$F: No change made"
		exit 1
	fi
	count=0
	$SORT -u /tmp/$$.shells | while read shell
	do
		count=`$EXPR $count + 1`
		if [ "$count" = "$number" ]
		then
			if [ "$currentshell" = "$shell" ]
			then
				$ECHO "$F: No change of shell needed"
				exit 0
			fi
			# Thanks to Marc Mengel
			/bin/ed -s $PASSWD <<!EOF! > /dev/null
/^$login:[^:]*:$uid:/s-\\([^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\).*-\\1${shell}
w
q
!EOF!
			$ECHO "$F: Login shell changed to $shell for $login"
			break
		fi
	done
fi

#
# Common close and setup
#
if [ "x$YPDIR" != "x" ]
then
	cd $YPDIR
	$YPMAKE passwd 2> /dev/null
fi
exit  0
