#!/bin/sh
# @vasm : vuserdel
# @level: root
# @description: delete a user
# 
# (c) Eko M. Budi, 2003
# (c) Vector Linux, 2003
#
# Released under GNU GPL

. /sbin/vasm-functions

pfile="/etc/passwd"

################################################
confirm_user () {
  DIMENSION="12 50"
  TITLE="DELETE USER"
  TEXT="\n
Last chance to to keep this user\n
  Login Name..: $LOGIN\n
  Real Name...: $U_NAME\n
  User ID.....: $U_ID\n\n
Say YES to delete user $LOGIN."

  $DCMD --backtitle "$BACKTITLE" --title "$TITLE" --yesno "$TEXT" $DIMENSION 2> $freply
  if [ ! $? = 0 ]; then
     clean_exit
  fi
}

################################################
# Main 1 
LOGIN=$1
if [ "$LOGIN" ]; then
  LINE=`grep -e "^${LOGIN}:" $pfile`;
  if [ "$LINE" ]; then
    U_NAME=`echo $LINE | cut -d ':' -f 5`
    U_ID=`echo $LINE | cut -d ':' -f 3`
    confirm_user
#    infobox "Deleteting user $LOGIN ...."
    userdel $LOGIN
    if [ $? = 0 ]; then
       rm -rf /home/$LOGIN
       sleep 2
       infobox "Deleting user $LOGIN .... DONE"
       clean_exit
    else
       msgbox "Deleting user $LOGIN FAILED"
       clean_exit 1
    fi
  fi   
  msgbox "User '$LOGIN' does not exists; please choose another"
fi

##################################################
# Need to ask username
function list_user () {
  DIMENSION="24 54 10"
  TITLE="DELETE USER"
  TEXT="\nThis is the list of users you may delete.\n
However, it contains only the first 100 users\n
with UID>=500.\n
If the user is not in the list, select EDIT\n\n
Select the user you want to delete then press OK"

  echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \' > $fmenu
  echo '"EDIT" "Enter name manually" \' >> $fmenu
  ## List the user, but only for the first 100
  awk -F: '{ if ($3>=500) {print $0; num++} if (num>=100) exit 0}' $pfile | sort | while read LINE; do
     N1=`echo $LINE | cut -d ':' -f 1`
     N2=`echo $LINE | cut -d ':' -f 5 | cut -d ',' -f 1`
     echo "$N1" "\"$N2 \" \\" >> $fmenu
  done
  echo '2> $freply' >> $fmenu

  #cat $fmenu
  source $fmenu
  if [ ! $? = 0 ]; then
    exit 1
  fi
}

###############################################################
# MAIN 2
while [ 1 ]; do
  list_user
  LOGIN="`cat $freply`"
  [ "$LOGIN" = "EDIT" ] && LOGIN=""
  while [ -z "$LOGIN" ]; do
     inputbox "Enter user name" "DELETE USER"
     LOGIN="`cat $freply`"
  done
  LINE="`grep -e "^${LOGIN}:" $pfile`";
  if [ -z "$LINE" ]; then
     msgbox "User '$LOGIN' does not exists; please choose another"
     continue
  fi  
  $0 $LOGIN
done

