#
#  adduser     (4/2/88)
#-----------------------------------------------------------------------------
#  Adduser: will add new users to the password file.
#
#  NOTE:  This script will create password file entries that are one line
#    long.  This format is compatible with the related shell scripts:
#    chnguser and deluser. 
#
#  Start by making sure everything is set up right
#-----------------------------------------------------------------------------
: ${PASSWD:=\\etc\\passwd}                       # make sure $PASSWD is set
if [ ! -f $PASSWD ]                              # look for password file
   then                                          # handle errors if not found
   echo "$PASSWD not found."
   exit 1
fi

clear                                            # clear the screen
echo "ADD A NEW USER TO THE PASSWORD FILE:
"
case "$1" in                                     # look for name as argument
   ?*) USER=$1                                   # if present
       Echo "Adding $USER as a new user.";;      # set USER from it
    *) echo "Enter the name of the user to add (no entry to quit)"
       echo -n "        : "                      # otherwise ask for user name
       read USER;;                               # and read USER from terminal
esac

case "$USER" in                                  # check for null entry
   "") echo "No user name entered, processing terminated."
       exit 1;;                                  # otherwise quit
esac
#-----------------------------------------------------------------------------
#  Do not set continue if the requested user is already in the password file
#-----------------------------------------------------------------------------
grep "^${USER}:" $PASSWD > nul:                  # look for existing user
case $? in
   0) echo "$USER already exists as a user in $PASSWD, processing terminated."
      exit 1;;                                   # handle error if found
   2) echo "Cannot search $PASSWD for $USER, processing terminated."
      exit 1;;                                   # handle error from grep
esac                                             # now we can set up new user
#-----------------------------------------------------------------------------
#  This is the main loop of the script.  Continue through the loop until
#  you have varified that the data is correct.  Remember that you can always
#  abort the script with <DEL> if you find yourself caught and just want to
#  get out.
#-----------------------------------------------------------------------------
while true                                       # loop until input is okay
   do
   echo "Enter a comment to identify $USER"
   echo -n "    : "                              # ask for comment
   read COMMENT
   echo "Enter the home directory for $USER"
   echo -n "    : "                              # ask for home dir
   read HOMEDIR
   while [ ! -d "$HOMEDIR" ]                     # while it's a bad directory
     do
     echo "Not a valid directory, please try again"
     echo -n "  : "                              # ask for it again
     read HOMEDIR
   done
   SUFFIX=
   echo "Enter any path suffix(es) to add to the normal path"
   echo -n "    : "                              # ask for path suffix
   read SUFFIX
   while [ \( ! -d "$SUFFIX" \) -a \( -n "$SUFFIX" \) ]
     do
     SUFFIX=
     echo "Not a valid directory, please try again"
     echo -n "  : "                              # ask for it again
     read SUFFIX
   done
   echo "Enter any initial command to execute upon login (eg. sh)"
   echo -n "    : "                              # ask for first command
   read FIRSTCMD
                                                 # show all of the entries
   echo "
        User login name:      $USER
        Descriptive comment:  $COMMENT
        Home directory:       $HOMEDIR
        Path string suffix:   $SUFFIX
        First command:        $FIRSTCMD
"
#-----------------------------------------------------------------------------
#  Everything checks out, now use the yesno script to see if it meets the
#  user's needs.  If done, give the user the option of using the passwd
#  program to set a password on the new user entry.  Note that we will not
#  remove the old password file, but will leave it on the disk as passwd.old
#-----------------------------------------------------------------------------
   if sh yesno "Is this correct"                 # ask if they are okay
     then                                        # ok, create new passwd line
     NEWLINE="${USER}::${COMMENT}:${HOMEDIR}:${SUFFIX}:${FIRSTCMD}"
     mv -f $PASSWD ${PASSWD}.old                 # save current password file
     cat ${PASSWD}.old > $PASSWD                 # make new one (w/o ^Z)
     echo "$NEWLINE" >> $PASSWD                  # add it to password file
     echo "$USER is now set up."                 # report add to the screen
                                                 # ask about a password
     if sh yesno "Do you want to protect $USER with a password"
       then                                      # if desired
       passwd $USER                              # set the password
     fi
     exit 0                                      # then end
   else                                          # if input is not okay
     clear                                       # clear screen again
     echo "Please try again.  NOTE: you may press the DEL key to quit.
"                                                # print error message
   fi
done                                             # and try again
