#!/bin/sh
# @vasm : vuseradd
# @level: root
# @description: quick add a user, all defaults
# 
# (c) Kocil, 2003
# (c) Vector Linux, 2003
#
# Released under GNU GPL

if [ ! "$VASM_FUNCTIONS" ]; then
  . /sbin/vasm-functions
fi

# Path to files
pfile=/etc/passwd
gfile=/etc/group
sfile=/etc/shells

# Paths to binaries
VPASSWD=/sbin/vpasswd
CHMOD=/bin/chmod
USERADD=/usr/sbin/useradd

# Defaults
defhome=/home
defshell=/bin/bash
defskel=/etc/skel
defchmod=711 

# Function to display the account info
function confirm_user () {
  DIMENSION="15 60"
  TITLE="ADD USER"
  TEXT="\n
We are going to add this user \n
  Login Name..: $LOGIN\n
  Real Name...: $U_NAME\n
  User ID.....: $U_ID\n
  User Group..: $G_ID\n
  Home........: $U_HOME\n
  Shell.......: $U_SHELL\n\n
Say YES if you are really sure:"

  $DCMD --backtitle "$BACKTITLE" --title "$TITLE" --yesno "$TEXT" $DIMENSION 2> $freply
  if [ $? = 1 -o $? = 255 ]; then
    exit 1
  fi
}

######################################################################################
## MAIN

# get login name from param
LOGIN="$1"

# get LOGIN
while [ 1 ]; do
  while [ -z "$LOGIN" ]; do 
    inputbox "Login name for new user (lower case and digit)" "ADD USER"
    LOGIN=`cat $freply`
  done

  #echo $LOGIN  
  if [ "`echo $LOGIN | grep -e '[A-Z]'`" ]; then
    msgbox "User '$LOGIN' contains illegal characters (CAPITAL); please choose another"
  elif [ "`echo $LOGIN | grep -e '[^a-z0-9_-]'`" ]; then
    msgbox "User '$LOGIN' contains illegal characters; please try again"
  elif [ "`grep -e "^${LOGIN}:" $pfile`" ]; then
    msgbox "User '$LOGIN' already exists; please choose another"
  else
    break
  fi
  unset LOGIN
done

# Ask long name
U_NAME="$2"
while [ -z "$U_NAME" ]; do
   inputbox "Enter real name (alphanumeric)" "ADD USER"
   U_NAME=`cat $freply`
   if [ "`echo $U_NAME | grep -e '[^A-Za-z0-9 .;,_-]'`" ]; then
      msgbox "Name '$U_NAME' contains illegal characters. Please type it again."
   else
      break
   fi
   unset U_NAME
done
#: Get the UID for the user & ensure it's not already in use :#
#
# Whilst we _can_ allow users with identical UIDs, it's not a 'good thing' because
# when you change password for the uid, it finds the first match in /etc/passwd 
# which isn't necessarily the correct user

U_ID=`awk -F: 'BEGIN {id=1000} { if ($3 >= id) id=$3+1 } END { print id}' $pfile`
while [ 1 ]; do
  inputbox "User ID (integer 1000-60000. Empty to use default)" "ADD USER" "$U_ID"
  U_ID="`cat $freply`"
  if [ -z "$U_ID" ]; then
    break;
  elif [ "`echo $U_ID | grep -e [^0-9]`" ]; then
    msgbox "UIDs are numerics only"
  elif [ "`grep -e "^.*:.*:$U_ID:.*" $pfile`" ]; then
    msgbox "That UID is already in use; please choose another" 
  else
    break
  fi
done

# other default additional entry
G_ID="users"
U_HOME="${defhome}/${LOGIN}"
U_SHELL="${defshell}"

confirm_user

[ "$U_ID" ] && U_ID="-u $U_ID"

infobox "Creating user ....."
# Add the account to the system
if [ "$U_NAME" ]; then
  CMD="$USERADD -d $U_HOME -m -k $defskel $U_ID -g $G_ID -s $U_SHELL -c \"$U_NAME\" $LOGIN"
  $USERADD -d $U_HOME -m -k $defskel $U_ID -g $G_ID -s $U_SHELL -c "$U_NAME" $LOGIN
else
  CMD="$USERADD -d $U_HOME -m -k $defskel -g $G_ID $U_ID $LOGIN"
  $USERADD -d $U_HOME -m -k $defskel -g $G_ID $U_ID $LOGIN
fi

if [ $? = 0 ]; then
  # Set home permissions 
  if [ -d "$U_HOME" ]; then
    $CHMOD $defchmod "$U_HOME"
  fi

  clean_tmp
  # Set a password
  . $VPASSWD "$LOGIN"

else
  msgbox "Can not create user.\nThe command was :\n$CMD"
  clean_exit 1
fi
