# Change a User's Information
#
# Created:	 24-Sep-87
# Modified Date: 24-Sep-87
#
# Created By:	 Sandy Marshall
# Modified By:	 Sandy Marshall
#

while :
do
	echo "\nEnter login name of user that information is to be changed for: \c"
	read LOGNAME
	if [ -n "$LOGNAME" ]
	then	EXISTS=`grep -c "^$LOGNAME:" /etc/passwd`
		if [ "$EXISTS" = "0" ]
		then	echo "\nThat name, $LOGNAME, does not exist"
		else	USRENTRY=`awk -F: '{ if ( $1 == x ) printf("%s\n",$0) }' x=$LOGNAME /etc/passwd`
			break
		fi
	else	exit 1
	fi
done

while :
do
	echo "\nEnter new groupid number (default current group): \c"
	read GID
	if [ -n "$GID" ]
	then	EXISTS=`awk -F: '{ if ( $3 == x ) printf("%s ",$1) }' x=$GID /etc/group`
		if [ "$EXISTS" = "" ]
		then	echo "\nThat group does not exist"
		else	break	
		fi
	else	GID=`echo $USRENTRY | cut -d: -f4`
		break
	fi
done

echo "\nEnter user profile (full name, phone ext.)"
echo "(default is current information): \c"
read FULLNAME
if [ -z "$FULLNAME" ]
then	FULLNAME=`echo $USRENTRY | cut -d: -f5`
fi

while :
do
	echo "\nEnter the shell program you wish to assign to this user"
	echo "(default is current shell): \c"
	read LSHELL
	if [ -z "$LSHELL" ]
	then	LSHELL=`echo $USRENTRY | cut -d: -f7`
		break
	else	if [ -x "$LSHELL" ]
		then	break
		else	echo "\nThat program does not exist"
		fi
	fi
done

PASS=`echo $USRENTRY | cut -d: -f2`
UID=`echo $USRENTRY | cut -d: -f3`
HOMEDIR=`echo $USRENTRY | cut -d: -f6`

while :
do
	if mkdir /etc/ptmp		# fails if passwd file is in use
	then	cp /etc/passwd /etc/opasswd
		chmod 444 /etc/opasswd
		chmod 600 /etc/passwd
		sed "/$LOGNAME/d" /etc/passwd > /usr/tmp/pass$$
		echo $LOGNAME:$PASS:$UID:$GID:$FULLNAME:$HOMEDIR:$LSHELL |
			sort -t: +2n /usr/tmp/pass$$ - > /etc/passwd
		chmod 444 /etc/passwd
		rm /usr/tmp/pass$$
		rmdir /etc/ptmp
		break
	else	echo "/etc/passwd is currently in use"
		echo "If you wish to try to change the user information again,"
		echo "please type \"yes\""
		read ANS
		if [ "$ANS" = "yes" ]
		then	continue
		else	echo "\nThe user information has not been changed"
			exit 1
		fi
	fi
done
