#! /bin/sh
#
#
# adduser - add users to mac os x local directory
#           works on 10.5 and 10.6  and 10.10, not tested on anything else
#           copyright 2015-04-05 by Brett Wynkoop wynkoop@wynn.com
#
# There is plenty of room for enhancement, like choice of shell
# or setting specific UID and GID instead of taking the defaults
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin

MYNAME=`basename $0`

export PATH MYNAME


while [ x$LOGIN = x ]
do

    echo
    /bin/echo -n "Username: "
    read LOGIN
done

# get the user's real name 
while [ x"$NAME" = x ]
do
      echo
      /bin/echo -n "Full Name: "
      read NAME
done
#
# ask to confirm the username and real name before we do any real
# work -BEW-
#
while [ x$CONT =  x ]
do
    echo
    /bin/echo -n "Create account $LOGIN with real name $NAME? [y/n] "
    read CONT
    echo
    if [ x$CONT = xn ]
    then 
         echo
         echo "$MYNAME: nothing done, exiting"
         exit 0
    fi
    if [ x$CONT != xy ]
    then
        CONT=""
    fi

done


#Create a new entry in the local domain
dscl . -create /Users/$LOGIN

#Create and set the shell property to bash.
dscl . -create /Users/$LOGIN UserShell /bin/bash

#Create and set the users full name.
dscl . -create /Users/$LOGIN RealName "${NAME}"

#
# get last UID and increment
#
LID=`dscl . -list /Users UniqueID | sort -n -k 2 | tail -1 | awk '{ print $2 }'`
NEWID=`expr $LID + 1`

#Create and set the user ID.

dscl . -create /Users/$LOGIN UniqueID $NEWID

#Create and set the users group ID property.
# default group for OS X users is 20, so just use
# that as the primary group for now.  Need to improve this -BEW-
#
dscl . -create /Users/$LOGIN PrimaryGroupID 20

#Set the password.
dscl . -passwd /Users/$LOGIN

#
# set home dir
#
dscl . -create /Users/$LOGIN NFSHomeDirectory /Users/$LOGIN

#Create and set the user local home directory.
createhomedir -u $LOGIN  -c