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

VDIR=$(dirname $0)
. $VDIR/vasm-functions

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

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

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


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

# get login name from param
LOGIN="$1"
U_NAME="$2"
U_ID=""
G_ID="users"
G_EXTRA="audio,video,cdrom,games"
U_HOME="${defhome}/${LOGIN}"
U_SHELL="${defshell}"

# ASK Login Name
menuA()
{
while [ 1 ]; do
  TITLE="ADD USER"
  TEXT="Login name for the new user (lower case and digit):"

  inputbox "$TEXT" "$TITLE" "$LOGIN"
  STATUS=$?
  [ $STATUS != 0 ] && return $STATUS

  LOGIN=$(cat $freply)

  if [ -z "$LOGIN" ]; then
    continue    
  elif [ "`echo $LOGIN | grep -e '[A-Z]'`" ]; then
    retrybox "User '$LOGIN' contains illegal characters (CAPITAL)"
  elif [ "`echo $LOGIN | grep -e '[^a-z0-9_-]'`" ]; then
    retrybox "User '$LOGIN' contains illegal characters"
  elif [ "`grep -e "^${LOGIN}:" $pfile`" ]; then
    retrybox "User '$LOGIN' is already exist"
  else
    return 0
  fi
done
}

menuB() {

if [ -z $U_NAME ]; then
  U_NAME=`echo $LOGIN | awk '{ print toupper(substr($1,1,1)) substr($1,2)}'`
fi

# Ask long name
while [ 1 ]; do
  TITLE="ADD USER"
  TEXT="Enter real name (alphanumeric):"

  inputwizard "$TEXT" "$TITLE" "$U_NAME"
  STATUS=$?
  [ $STATUS != 0 ] && return $STATUS

  U_NAME=$(cat $freply)

  if [ "`echo $U_NAME | grep -e '[^A-Za-z0-9 .;,_-]'`" ]; then
     retrybox "Name '$U_NAME' contains illegal characters"
  else
     return 0
  fi
done
}

menuC() {

U_ID=`awk -F: 'BEGIN {id=1000} { if ($3 >= id) id=$3+1 } END { print id}' $pfile`
while [ 1 ]; do
  TITLE="ADD USER"
  TEXT="Enter user ID (integer 1000-60000. Empty for next ID)"
  inputwizard "$TEXT" "$TITLE" "$U_ID"
  STATUS=$?
  [ $STATUS != 0 ] && return $STATUS

  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
}

menuD() {

  # Get default
  G_ID="users"
  U_HOME="${defhome}/${LOGIN}"
  U_SHELL="${defshell}"

  DIMENSION="18 60 2"
  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
Say YES if you are really sure:"

  $WCMD $LEFT_ALIGN --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
  "YES" "Create the user then set the password" \
  "NO"  "Cancel and quit" \
  2> $freply


  STATUS=$?
  [ $STATUS != 0 ] && return $STATUS
  
  reply=$(cat $freply)
  
  if [ "$reply" != "YES" ]; then
     clean_exit
  fi

}

menuE() {

  infobox "Creating user ....."
  # Add the account to the system
  CMD_UID=${U_ID:+"-u $U_ID"}
  dbug "$USERADD -m -s $U_SHELL $CMD_UID $CMD_NAME $LOGIN"

  if [ "$U_NAME" ]; then
    $USERADD -m -s $U_SHELL $CMD_UID -c "$U_NAME" -G $G_EXTRA $LOGIN 2> $freply
  else
    $USERADD -m -s $U_SHELL $CMD_UID -G $G_EXTRA $LOGIN 2> $freply
  fi
  if [ $? = 0 ]; then
    dbug "User has been created"
    # Set home permissions 
    if [ -d "$U_HOME" ]; then
      # Copy skeletons to make sure 
      cp -a /etc/skel/* $U_HOME
      chmod $defchmod $U_HOME
      chown -R $LOGIN $U_HOME
    fi
    clean_tmp
    exec $VDIR/vpasswd "$LOGIN"
  else
    errorbox "\n$(cat $freply)"
    clean_exit 1
  fi
}


##################################
# testing
#LOGIN=aaaa
#U_NAME=
#U_ID=
#G_ID="users"
#U_HOME="${defhome}/${LOGIN}"
#U_SHELL="${defshell}"
#menuE
#userdel aaaa

############################################
# Main Program

wizard menuA menuB menuC menuD menuE

