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

## This script need overhaul
## Still using the OLD confusing flow

. /sbin/vasm-functions

check_root

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 "Deleting 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

###############################################################
# MAIN 2
edit_user()
{
LOGIN=""
while [ -z $LOGIN ]; do
  inputbox "Enter user name" "DELETE USER"
  [ $? != 0 ] && return 1
  LOGIN="`cat $freply`"
done
return 0
}

##################################################
# MAIN 2
while [ 0 ]; do
  DIMENSION="20 50 10"
  TITLE="DELETE USER"
  TEXT="\n
This is the list of users you may delete.
Please select the one you you want to delete.
If the user is not in the list, select EDIT:"

  echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \' > $fmenu
  echo '"EDIT" "Enter name manually" \' >> $fmenu
  ## List the users, 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
    clean_exit 1
  fi

  LOGIN="`cat $freply`"

  if [ "$LOGIN" = "EDIT" ]; then
     edit_user  
  fi
  
  if [ $LOGIN ]; then
      LINE="`grep -e "^${LOGIN}:" $pfile`";
      if [ "$LINE" ]; then
         $0 $LOGIN
      else
         retrybox "User '$LOGIN' does not exists; please choose another"
      fi
  fi
done

