#!/bin/sh
# deluser - Older alternative to "userdel".
# Revision: 060320

#---------------------------------------------------------------------

# This script  removes a user account.  It's based on the Peanut Linux
# 9.1 version of "deluser" produced by "Linuxkid" (Jay Klepacs).

#---------------------------------------------------------------------

if [ ! "$UID" = "0" ]; then
echo
echo "[$LOGNAME] You need to run this script as root."
echo "Try one of these to run it as root: # sudo deluser"
echo "                                    # su -c deluser"
echo
exit 
fi

if ! type -all dialog >/dev/null 2>&1 ; then
echo "Can't find 'dialog', I can't run without 'dialog' on your system."
exit 
fi

TMP=/tmp/1.$$

cat << EOF > $TMP
Deletes a user from the system
EOF
dialog --title "Deluser" --yesno "`cat $TMP`" 6 55  

if [ $? = 1 ]; then
rm -f $TMP
echo
exit
fi

while [ 0 ]
do
cat << EOF > $TMP
What's the name of the user you want to remove from the system?
EOF

dialog --title "Remove User" --inputbox "`cat $TMP`" 8 70 2> $TMP
 
if [ $? = 1 ]; then
rm -f $TMP
echo
exit
fi

USER=`cat $TMP`

if [ -z "$USER" ]; then
continue
elif ! grep -q "^$USER:" /etc/passwd; then
cat << EOF > $TMP
User $USER not in /etc/passwd file.
EOF
dialog --title "Not Found!" --msgbox "`cat $TMP`" 6 55
continue
else
cp /etc/passwd /etc/passwd~
sed -e "/^$USER:/d" </etc/passwd~ >/etc/passwd
rm -f /etc/passwd~ 
cat << EOF > $TMP
User $USER removed from /etc/passwd file.
EOF
fi

if grep -q "^$USER:" /etc/shadow 2>/dev/null ; then
cp /etc/shadow /etc/shadow~
sed -e "/^$USER:/d" </etc/shadow~ >/etc/shadow
rm -f /etc/shadow~
cat << EOF >> $TMP
User $USER removed from /etc/shadow file.
EOF
fi

if grep -q "^$USER:" /etc/group 2>/dev/null ; then
cp /etc/group /etc/group~
sed -e "/^$USER:/d" </etc/group~ >/etc/group
rm -f /etc/group~
cat << EOF >> $TMP
User $USER removed from /etc/group file.
EOF
fi

cat $TMP >> $TMP >/dev/null
dialog --title "Removed" --msgbox "`cat $TMP`" 7 55

if [ -d /home/$USER ]; then 

cat << EOF > $TMP
Do you want to remove the home directory of $USER? 
EOF

dialog --title "Remove Home" --yesno "`cat $TMP`" 6 65

if [ $? = 0 ]; then
rm -rf /home/$USER
cat << EOF > $TMP
User /home/$USER directory removed.
EOF
dialog --title "Removed" --msgbox "`cat $TMP`" 6 55
fi
fi
continue
done
