#!/bin/sh
#< Add a new user to the system
# anu - Add New User
# nice wrapper for the useradd command
# inspired by a script found at alge.anart.no
# and totally rewritten for my site
# Kevin Waldron  11/12/2003

# Added support for selecting different shell - KW 12/12/2003

thiscmd=`basename $0`
thisuser=`id -u`

# first things first - are we root?
case $thisuser in
  0 ) ;;	# root - fine
  * ) echo "You must be root to run $thiscmd" && exit 1
      ;;
esac

if [ "$#" -ne 1 ]; then
  echo "USAGE: $thiscmd username" && exit 1
fi


# does username already exist?
grep ^$1: /etc/passwd > /dev/null 2>&1
if [ "$?" -eq 0 ]; then
  echo "User $1 already exists. Please re-run with a different username!"
  exit 1
fi
clear
echo "Adding New User [$1] to `hostname`"
# Assumptions
# -----------
# By default, we know that "useradd" will assign the next available UID
# Also, under SuSE, the "users" group is GID 100

echo -n "Please enter the users real name: "
read real_name
echo -n "Please select a login shell (enter for /bin/bash): "
def_shell=/bin/bash
read u_shell
if [ "$u_shell" = "" ]; then
   # No shell entered - default to /bin/bash
   echo "Using /bin/bash"
else
   grep $u_shell /etc/shells > /dev/null 2>&1
   if [ "$?" -eq 0 ]; then
      echo "Using shell $u_shell"
      def_shell=$u_shell
   else
      echo "Shell $u_shell not found in /etc/shells"
      echo "Defaulting to /bin/bash"
   fi
fi

echo -e "\n\n"
echo "About to add user with the following details:"
echo "User name:         $1"
echo "Real name:         $real_name"
echo "Shell:             $def_shell"
echo "Home Directory:    /home/$1"
echo "User will be assigned next available UID, and will be a member"
echo "of the users group"
echo "Type [y] to confirm, or anything else to quit"
read response
case $response in
   [yY]* ) ;; # fine
   *     ) echo "Aborting user creation process"
           exit 1 ;;
esac

# Add the user proper!
echo -e "\n\nAdding user $1 [$real_name] to system - DO NOT ABORT NOW!!"
useradd -g users -d /home/$1 -s $def_shell -c "$real_name" $1
cp -r /etc/skel /home/$1
chown -R $1:users /home/$1
passwd $1
echo "User $1 successfully added to system!"
echo "User $1 [$real_name]: added to `hostname` on `date`" >> /var/log/newusers


exit 0

# Generated with sh2html - freely available from http://www.zazzybob.com/sh2html.html