#!/bin/sh
#
# Primitive attempt to provide a facilty to erase users from /etc/passwd etc.
# Christoph Lameter, 24. September 1996
#
# This script was changed on 3-19-2000. 
# It now deletes the user from the /etc/shadow and /etc/group files also.

if [ "$1" = "" ]; then
        echo
	echo "Remove a USER from /etc/passwd and or /etc/shadow." 
	echo -e "Usage: deluser <username>\n"
	echo -e "# deluser jerry  <For example, remove user jerry>\n"
	exit 1
fi

if [ ! "$UID" = "0" ]; then
echo "[$LOGNAME] You need to run this script as 'root'."
exit 1
fi

	if ! grep -q "^$1:" /etc/passwd; then
	echo "User $1 not in /etc/passwd file."
        exit 1
        else
	cp /etc/passwd /etc/passwd~
	sed -e "/^$1:/d" </etc/passwd~ >/etc/passwd
	rm -f /etc/passwd~ 
        echo "User $1 removed from /etc/passwd file."
fi
                
if grep -q "^$1:" /etc/shadow 2>/dev/null ; then
cp /etc/shadow /etc/shadow~
sed -e "/^$1:/d" </etc/shadow~ >/etc/shadow
rm -f /etc/shadow~
echo "User $1 removed from /etc/shadow file."
else
echo "User $1 not in /etc/shadow file, so not removed."
fi

if grep -q "^$1:" /etc/group 2>/dev/null ; then
cp /etc/group /etc/group~
sed -e "/^$1:/d" </etc/group~ >/etc/group
rm -f /etc/group~
echo "User $1 removed from /etc/group file."
else
echo "User $1 not in /etc/group file, so not removed."
fi

if [ -d /home/$1 ]; then 
echo -n "Do you want to remove the home directory of $1? (N/y) : " 
read ans
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
rm -rf /home/$1 
echo "User /home/$1 directory removed."
fi
else
echo "No /home/$1 directory found, so not removed."
fi
