# /etc/Rmusr shell script
# Copyright 1986 L/F Technologies, Carson City, Nevada
# Author: Rodger Lakey
# File Creation Date: Dec 10, 1986
# Last Revision Date: Apr 15, 1987
#
# Modification History:
# 	1.0 - Dec 10, 1986 by RJL - File created
#	1.1 - Apr 15, 1987 by ACW - Prevented unsuspecting super-user from 
#		removing root, and other "privileged" users.
#
echo "Remove User Program"
echo "Version 1.1"
echo 

while :
do
	echo "Enter login name of user to be deleted from the system: \c"
	read LOGNAME
	if [ "$LOGNAME" = "" ]
	then
		continue
	fi
	ID=`grep "^$LOGNAME:" /etc/passwd | awk -F: '{ printf("%s ",$3) }'`
	if [ "$ID" -lt "100" ]
	then
		echo "That name either does not exist, or is a privileged user."
		echo "Please try another."
		echo
	else
		echo
		break
	fi
done

HOMEDIR=`grep "^$LOGNAME:" /etc/passwd  | awk -F: '{ printf("%s",$6) }'`
OWNER=`/bin/ls -do $HOMEDIR | awk '{ printf("%s",$3) }'`
if [ "$OWNER" != "$LOGNAME" ]
then
	echo "User $LOGNAME is NOT the owner of home directory $HOMEDIR"
	echo "This script will not remove $HOMEDIR"
else
	while :
	do
		echo "User $LOGNAME has the home directory $HOMEDIR"
		echo "If you wish, this directory can be deleted. Doing so"
		echo "will permanently remove all the user's files."
		echo "Do you wish to delete this directory? (y or n) \c"
		read ANS
		if [ "$ANS" = "y" ]
		then
			rm -rf $HOMEDIR
			break
		fi
		if [ "$ANS" = "n" ]
		then
			break
		fi
	done
fi

if mkdir /etc/ptmp	# fails if passwd command running
then
	cp /etc/passwd /etc/opasswd
	chmod 444 /etc/opasswd
	LOGNAME='^'$LOGNAME
	grep -v $LOGNAME /etc/passwd > /tmp/TMp$$
	mv -f /tmp/TMp$$ /etc/passwd
	chmod 444 /etc/passwd
	rmdir /etc/ptmp 
else
	echo "/etc/passwd file in use try again later"
	exit 1
fi
echo "Done."
