#!/bin/sh
# Add a new user to the system
# Heavily modified by: greEd
# Inspired by a script found at alge.anart.no

thiscmd=`basename $0`
thisuser=`/usr/xpg4/bin/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 "Please enter the users real name: "
read real_name
echo "Please enter the users Group: "
read group_name
echo "Please enter the users ID: "
read user_id
echo "Please select a login shell (enter for /bin/sh): "
def_shell=/bin/sh
read u_shell
if [ "$u_shell" = "" ]; then
   # No shell entered - default to /bin/sh
   echo "Using /bin/sh"
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/sh"
   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:    /users/$1"
echo "User ID:           $user_id"
echo "User Group:        $group_name" 
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 "\n\nAdding user $1 [$real_name] to system - DO NOT ABORT NOW!!"
useradd -g $group_name -u $user_id -d /users/$1 -s $def_shell -c "$real_name" $1
cp -r /etc/skel /users/$1
chown -R $1:$group_name /users/$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


