#! /bin/bash
#
# The System Manager
# An Administration Tool For UNIX
#
# Copyright (c) 1994 Stuart Herbert
# Released under the GNU GPL v2
#
# Bugs/comments/contributions to S.Herbert@Sheffield.ac.uk

# $1 = Username to nail
function DeleteThatUser()
{
  UserDetails="`grep ^$1: /etc/passwd`"
  HomeDir="`echo $UserDetails | cut -d : -f 6`"
  
  if YesOrNo "Do you really want to delete $1?" ; then
    if YesOrNo "Archive $1's files (in $HomeDir)?" ; then
      InfoBox "Archiving $1's files ... please wait"
      tar cfP $HomeDir.tar $HomeDir 2> /dev/null
      gzip -9 $HomeDir.tar
    fi
    if YesOrNo "Delete $1's files (in $HomeDir)?" ; then
      InfoBox "Removing $1's files ... please wait"
      rm -rf $HomeDir
    fi
    if YesOrNo "Locate and remove $1's files (in /)?" ; then
      InfoBox "Lauching background find & rm ... please wait"
      find / -user "$1" -exec rm -f {} \; &
    fi
    InfoBox "Removing $1 ... please wait"
    userdel $1
    MsgBox "$1 is history."
  else
    Abort "$1 will not be deleted."
  fi
}

function main()
{
  SelectUser "ManyOfAll" "Please select a user(s) to delete." "$SysmanFile1" ""
  if [ ! -s $SysmanFile1 ]; then
    return
  fi
  
  MenuSelection="`cat $SysmanFile1`"
  for x in $MenuSelection ; do
    DeleteThatUser "$x"
  done
}

# main code
if [ -n "$1" ]; then
  case "$1" in
    --SysmanInstall)
      echo "Y : Y : DELETE : Delete A User (Or Two) :$0"
      exit
      ;;
    --SysmanAuthor)
      echo "DELETE : Delete A User From The System"
      echo "Copyright (c) Stuart Herbert"
      exit
      ;;
  esac
else
  main
fi

# clean up here
for x in $SysmanFile1 $SysmanFile2 $SysmanFile3 ; do
  if [ -f $x ]; then rm -f $x ; fi
done

# that's it, folks
