#!/bin/sh

# ADDUSER - add a new user to the system, including building their
#           home directory, copying in default config data, etc.
#           For a standard aLinux/Unix/Linux system, not Mac OS X.

pwfile="/etc/passwd"    shadowfile="/etc/shadow"
gfile="/etc/group"
hdir="/home/users"
mdir="/var/spool/mail"
skel="/etc/skel/."
shell="/bin/bash"

if [ "$(whoami)" != "root" ] ; then
  echo "Error: You must be root to run this command." >&2
  exit 1
fi

echo "Add new user account to $(hostname)"
echo -n "login: "     ; read login

# Adjust '5000' to match the top end of your user account namespace
# because some system accounts have uid's like 65535 and similar.

uid="$(awk -F: '{ if (big < $3 && $3 < 5000) big=$3 } END { print big + 1 }' $pwfile)"
homedir=$hdir/$login

# We are keeping all users in group users, so gid=users not gid=uid
gid=$uid

echo -n "full name: " ; read fullname

echo "Setting up account $login for $fullname..."

echo ${login}:x:${uid}:${gid}:${fullname}:${homedir}:$shell >> $pwfile
echo ${login}:*:11647:0:99999:7::: >> $shadowfile

echo "${login}:x:${gid}:$login" >> $gfile

mkdir -m 700 $homedir
cp -R $skel $homedir
chown -R $login:users $homedir
touch $mdir/$login; chown $login:root $mdir/$login; chmod 660 $mdir/$login

# Setting an initial password
passwd $login

exit 0