#!/bin/bash
#
# This scripts complements ohmasteruser by destroying accounts and doing
# some cleanup...


user=$1			# username passed to this script
DHOME="/hamster/home" 	# place for home dirs
BASE="/hamster"		# place before SYS volume
SNAME="nw-admin"	# supervisor name
SHOME="supervis"	# Supervisor home dir

#Get user name
if [ "$user" = "" ]
then 
  echo -n "What user should be cast into the pit: "
  read user
fi

# Check existing
if ! grep "^$user:" /etc/passwd > /dev/null
then
  echo "Removing Nonexisting users lead to time paradoxes, I quit!"
  exit
else
  echo -n "Are you sure? (Type yes) "
  read yn
  if [ "$yn" != "yes" ]
  then
    echo "Chicken!"
    exit
  fi
fi

#11th commandment: thou should make backups
cp /etc/passwd /etc/passwd.notmp
cp /etc/group /etc/group.notmp
 
# Remove user from passwd file
echo -n "Removing user from password file..."
cat /etc/passwd | sed "/^$user:/d" > /etc/passwd.lock
mv -f /etc/passwd /etc/passwd-
mv -f /etc/passwd.lock /etc/passwd
echo "done..."

# Remove user from shadow passwd file
echo -n "Removing user from shadow password file...."
mv /etc/shadow /etc/shadow-
cat /etc/shadow- | sed "/^$user:/d" > /etc/shadow
echo "done..."

# Remove user from group file
echo -n "Removing user from group file.... "
cat /etc/group | sed "/^$user:/d" > /etc/group.lock
mv -f /etc/group /etc/group-
mv -f /etc/group.lock /etc/group
cat /etc/group | sed "s/:$user,/:/" > /etc/group.l1
cat /etc/group.l1 | sed "s/,$user,/,/" > /etc/group.l2
cat /etc/group.l2 | sed "s/,$user$//" > /etc/group.l3
mv -f /etc/group /etc/group-
mv -f /etc/group.l3 /etc/group
rm /etc/group.l1 /etc/group.l2
echo "done..."

# Remove user from expiry file
echo -n "Removing user from account expiry file... "
cp /etc/expiry /etc/expiry-
cat /etc/expiry- | sed "/^$user:/d" > /etc/expiry
echo "Done..."
 
# Remove user from birthdays file
echo -n "Remove user from birthdays file.... "
cp /etc/birthdays /etc/birthdays-
cat /etc/birthdays | sed "/^$user:/d" > /etc/birthdays
echo "Done...." 

echo -n "Removing user from mail delivery notification file.... "
cp /etc/ohmaillist /etc/ohmaillist-
cat /etc/ohmaillist- | sed "/^$user$/d" > /etc/ohmaillist
echo "Done..."

# Remove user's mail box
echo -n "Removing user's mailbox..."
rm /var/spool/mail/$user
rm /tmp/mail/$user
echo "done..."

# Remove user's files
echo -n "Should I remove all $user's data as well? (Type yes) "
read yn
if [ "$yn" = "yes" ]
then
  echo "Removing ..."
  rm -r $DHOME/$user
  rm -r $DHOME/httpd/html/$user

#  here needs to be some code to delete the novell mailbox as well

   NOVMAIL="`ls -l $BASE/SYS/mail | grep  " $user " | colrm 1 55`"
   if [ "$NOVMAIL" != "" ] ; then 
      echo -n "Removing Novell mailbox $NOVMAIL ... " 
      rm -r $BASE/SYS/mail/$NOVMAIL  
      echo "done..."
   fi
fi

echo -n "Updating myusers.html... "
$DHOME/httpd/cgi-bin/nph-users > /$DHOME/httpd/html/myusers.html
chmod 755 $DHOME/httpd/html/myusers.html
echo "done..."

echo -n "Removing user from Novell binderies... "
echo -e "#DELETE $user^\r\n" >> $DHOME/$SHOME/users.usr
chown $SNAME.$SNAME $DHOME/$SHOME/users.usr
echo -n "done..."

# The following removes stuff from the dummy group and passwd file for ftp
echo -n "Updating passwd clone in ..base../home/etc... "
cat /etc/passwd | cut -f 1,3,4,5,6,7,8,9,10 -d : - | sed s/:/:*:/ > $DHOME/etc/passwd
cp /etc/group $DHOME/etc/group
echo "done..."


echo "Supervisor, please just logon to marsserver to remove this user automagically."
echo ""
echo "Password linecount: bottom number should be one less"
wc /etc/passwd.notmp
wc /etc/passwd
echo "Groupfile linecount: bottom number should be one less"
wc /etc/group.notmp
wc /etc/group
echo "All done, please inform user $user of his non-existance"

#DONE...