#ident	"@(#)/usr/admin/menu/usermgmt/deluser.sl 1.4 3.2 09/01/87 31066 AT&T-SF"
#menu# delete a user from the system
#help# 
#help#	Deluser allows you to remove users from the computer.  The
#help#	deleted user's files are removed from the file systems and their
#help#	logins are removed from the /etc/passwd file.

minuid=100	# minimum user ID number that is deletable

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

echo '
This function COMPLETELY REMOVES THE USER, their mail file, home directory
and all files below their home directory from the machine.
Once this is done, there is no way guaranteed to get them all back.
		BE SURE THIS IS WHAT YOU WANT TO DO!
'
while true
do
	loginid=`checkre ${flags} -fe 'Enter login ID you wish to remove [q]:' \
		'.' 'You must enter at least one character.'`
	idline=`grep "^${loginid}:" /etc/passwd`
	if [ -n "${idline}" ]
	then
		eval `echo "${idline}"  |
			sed 's/[^:]*:[^:]*:\([^:]*\):[^:]*:\([^:]*\):\([^:]*\).*/uid="\1" text="\2" home="\3"/'`
		echo "\\n\\tUser ID: '${loginid}' belongs to '${text}'
	whose home directory is ${home}."
                echo
		if [ "${uid}" -lt ${minuid} ]
		then
			echo "\\tThat login has user ID ${uid} which is less than ${minuid}.
This procedure will not delete that login ID."
		elif [ -n "`who  |  grep \"^${loginid} \"" ]
		then
			echo "\\tThat user is currently logged on.  This procedure will not delete it."
		elif  checkyn ${flags} -H"
	If you answer  y  all files under ${home} will be removed." \
			-f "Do you want to remove login ID '${loginid}'?"
		then
			passmgmt -d ${loginid} 2>/dev/null 1>&2
			if [ $? -ne 0 ]
			then
				admerr $0 'Failure attempting to delete user password entry'
				exit 1
			fi
# remove newgrp entries in /etc/group for loginid
			ed - /etc/group <<- !
				H
				,g/:${loginid}$/s//:/
				,g/:${loginid},/s//:/
				,g/,${loginid}$/s///
				,g/,${loginid},/s//,/
				w
				q
			!
			if [ ! -d ${home} ]
			then
				echo "\\t${home} does not exist."
			else
				/bin/rm -rf ${home}
				echo "\\n\\tDirectory ${home} and all files under it have been removed."
			fi
			/bin/rm -f /usr/mail/${loginid}
			echo "\\n\\tUser ID: '${loginid}' has been completely removed.\\n"
		fi

	else
		echo "\\n'${loginid}' is not an existing login. Current users are:\\n"
		/usr/local/listusers | egrep -v "=|User|^$"
                echo
	fi
done
