#! /bin/sh
#
#  $ADM/adduser -- add a user, creating directories and stuff
#
#  usage: adduser name [options]
#	options are:
#	-p password (not encrypted if it starts with '*')
#	-u userid (numeric or '-')
#	-g groupid (numeric or symbolic)
#	-n real name (gecos field)
#	-d home directory (checked iff it exists)
#	-s login shell (must exist and be executable)
#
#  Written by Steven Robbins
#
ADM=/usr/adm
MAIL_DIR=/usr/spool/mail

#  Set up the defaults
#
PW_USER=$1
PW_PASS=$PW_USER
PW_UID="-"
PW_GID="user"
PW_GCOS=$PW_USER
PW_DIR="/home/"$PW_USER
PW_SHELL="/bin/sh"

#  Deal with overriding options
#
shift
badcase=0
while getopts "p:u:g:n:d:s:" OPT; do
	case $OPT in
		p) PW_PASS=$OPTARG;;
		u) PW_UID=$OPTARG;;
		g) PW_GID=$OPTARG;;
		n) PW_GCOS=$OPTARG;;
		d) PW_DIR=$OPTARG;;
		s) PW_SHELL=$OPTARG;;
		?) badcase=1
		esac
	done

if [ $badcase -eq 1 ]; then exit 1; fi

#  Now, put the entry into /etc/passwd, and do all the other admin chores
#
PWENT="$PW_USER $PW_PASS $PW_UID $PW_GID \"$PW_GCOS\" $PW_DIR $PW_SHELL"
MAIL_FILE=$MAIL_DIR/$PW_USER

if eval $ADM/mkpwent $PWENT; then
#
#  Setup the home directory, and related things
#
	mkdir $PW_DIR 
	mkdir $PW_DIR/bin 

	touch $MAIL_FILE
	chmod 600 $MAIL_FILE

	cp $ADM/dot/bashrc       $PW_DIR/.bashrc 
	cp $ADM/dot/bash_logout  $PW_DIR/.bash_logout 

	chown -R $PW_USER:$PW_GID $PW_DIR $MAIL_FILE

	cat <<-!G!R!O!K! | mail $PW_USER
Welcome, $PW_GCOS!

You have been given a default password.  
Please change it now using the command 'passwd' from the shell prompt.

The Management
!G!R!O!K!
	fi
