
#	ATT :#ident	"sadmin:admin/menu/usermgmt/moduser/chgloginid	2.3"

#ident	"@(#)sadmin/admin/menu/usermgmt/moduser:chgloginid	25.1"

#menu# menu of commands to modify a user's login
#menu# change a user's login ID
#help# 
#help#	This procedure allows you to change a user's login ID.
#help#	You cannot change the login IDs of the administrative and system
#help#	logins.


#test if yellow page is up
ps -e > /tmp/t1
if grep ypbind /tmp/t1 > /dev/null
then
# if so, is this the yp master server?
	host=`uname -n`					
	master=`ypwhich`
	if test $host = $master; then
		flag=`cat /tmp/t1`
	else
		echo "Must be YP master server to execute chgloginid\n"
		rm /tmp/t1
		exit 1
	fi
fi
rm /tmp/t1

loginlen=8	# Maximum login ID length
minid=100	# Minimum user and group ID

trap 'exit 0' 1 2 15
flags="-qq -k$$"

echo 0 > /tmp/t0
while  true
do
	loginid=`checkre ${flags} -H'
	This is the "name" that the computer uses to identify the user.
	It also is used to identify data that belongs to the user.' \
		-fe "Enter user's login ID [?, q]:  " \
		-R ':' '":" is an illegal character.' \
		-r '.' 'You must enter a value.'`
	uid=`sed -n "/^${loginid}:/s/[^:]*:[^:]*:\([0-9]*\):.*/\1/p" /etc/passwd`
	if [ -z "${uid}" ]
	then
		echo "\\tLogin ID '${loginid}' does not exist.  Choose another."
		continue
	elif [ "${uid}" -lt ${minid} ]
	then
		echo "\\tThis login has user ID ${uid}, which is less than ${minid}.
	This procedure will not change its login ID."
		continue
	fi
	while true
	do
		newloginid=`checkre ${flags} -H'
	This is the "name" that the computer uses to identify the user.
	It also is used to identify data that belongs to the user.
	The login ID may be any combination of numbers and letters not already
	used by someone else.  Typically, people choose their initials,
	first or last name, or nickname.' \
			-fe "Enter new login ID [?, q]:  " \
			'.' "You must enter a value, not more than ${loginlen} characters long." \
			'^[a-z0-9]*$' 'Answer contains an illegal character.
	Only numbers and lower case letters are permitted.' \
			"^.\{1,${loginlen}\}$" "Answer is too long.
	No more than ${loginlen} characters are permitted."`
		passmgmt -m -l ${newloginid} ${loginid} 2>/dev/null
		err=`echo $?`
		if [ $err -eq 9 ]
		then
			echo "\\tLogin ID '${newloginid}' already exists.  Choose another."
			continue
		elif [ $err -gt 0 ]
		then
			admerr $0 'Cannot edit /etc/passwd.'
			exit 1
		fi
		break
	done

	echo 1 > /tmp/t0
	checkyn ${flags} -f \
		'Do you want to change the login ID of another login?'  ||
		break
done


#test if yellow page is up
ps -e > /tmp/t1
if grep ypbind /tmp/t1 > /dev/null
then
	flag=`cat /tmp/t0`
	rm /tmp/t0
	if test $flag -gt 0; then
		make -eif /etc/yp/Makefile
	fi
fi
