#!/bin/sh
#######################################################
#
# remuser.sh -- deletes a user completely from system
#
#######################################################
# VARIABLES
TPWD=/tmp/tpwd.$$
PWFILE=/etc/passwd

chk_del() {

ID=`echo "$PWDLINE" | awk -F: '{print $3}'`
GID=`echo "$PWDLINE" | awk -F: '{print $4}'`
NAME=`echo "$PWDLINE" | awk -F: '{print $5}'`
HOMEDIR=`echo "$PWDLINE" |awk -F: '{print $6}'`
SH=`echo "$PWDLINE" | awk -F: '{print $7}'`

cat << EOF

      Login name	$USERID
      UID		$ID
      Group ID		$GID
      Description 	$NAME
      Home Directory	$HOMEDIR
      Shell Command	$SH

Last Chance! You are about remove the password 
entry and all files owned by "${USERID}".

EOF

echo "Is it OK to proceed ? [(y/n) n] \c" 
read ANSWER
case $ANSWER in
  y*|Y*) echo "\nProceeding..."  
         ;;
  *) echo "\nExiting now..."  
         sleep 2  
         exit 1
         ;;
esac  

# Remove files and then directories 
find / -user $USERID -type f -print -exec rm {} \;
find / -user $USERID -type d -print -exec rmdir {} \;

# delete entry from password file
grep -v "^$USERID:" $PWFILE > $TPWD       
cp $TPWD $PWFILE        
chmod 444 $PWFILE
rm $TPWD      
}

# mainline 

clear 

echo "\nPlease enter user to delete below..."     
echo "USERID: \c"
read USERID
if [ -z $USERID ]
then 
  echo "\nNo user entered. Exiting now!"
  sleep 2
elif grep "^$USERID:" $PWFILE > /dev/null
then
  # Get passwd entry and call function to
  # check if OK to delete and delete user
  PWDLINE=`grep "^$USERID:" $PWFILE`
  chk_del  
else
  echo "\n${USERID} not in $PWFILE. Exiting now..."
  sleep 2
fi 
   
