#!/bin/csh -f

#
#*
#*+***********************************************************************
#*
#*  Module:
#*      AcctBldr                                            26OCT95
#*
#*  Purpose:
#*      This module is used to build a simple functional user account
#*  Composer.
#*
#*  Date: 29 Sept 1995
#*
#*       1995 Alias|Wavefront, Inc.
#*      All rights reserved.
#*
#*      Alias|Wavefront, Inc.
#*      110 Richmond Street East
#*      Toronto, Ontario M5C 1P1  Canada
#*      Tel: (416) 362-9181
#*      Fax: (416) 362-0630
#*
#*
#*
#*-***********************************************************************
#*

#
# 
#
#

set noglob

# defaults
set LOG = awuser
set UID = 1000
set GID = 20
set SHL = /bin/csh
set HMP = /usr/people
set PSW

set me=$0
set mydir=$me:h
set me=$me:t
set x=0

set path = (/usr/sbin /usr/bsd /sbin /usr/bin /bin /etc /usr/etc /usr/bin/X11)

if ($mydir == $me) set mydir=$cwd
set SKL = $mydir/skel

alias Exit 'set x=\!$ ; goto EXIT'

if ($#argv < 1) goto tag_usage

while ($#argv > 1)

    if ($#argv < 2) goto tag_usage

    switch ($1)
	case -w:
		set WIN_STAT ; shift ; breaksw
	case -a:
		set ASK ; shift ; breaksw
	case -d:
		set PSW = disabled ; shift ; breaksw
	case -u:
		set UID = $2 ; shift ; shift ; breaksw
	case -g:
		set GID = $2 ; shift ; shift ; breaksw
	case -t:
		set SKL = $2 ; shift ; shift ; breaksw
	case -s:
		set SHL = $2 ; shift ; shift ; breaksw
	case -h:
		set HOM = $2 ; shift ; shift ; breaksw
	case -l:
		set LIK = $2 ; shift ; shift ; breaksw
	default:
		goto tag_usage
    endsw

end

if ($?WIN_STAT) then

	alias Echo 'echo \ \ \ \  \!* >> /tmp/.ab$$'

	if ! (-x /usr/bin/X11/xconfirm) echo 'cat << EOF' > /tmp/.ab$$
	Echo ""
	Echo ""
	Echo "Alias|Wavefront Account Creation Program"
	Echo ""
	Echo ""
	
else
	alias Echo echo
endif


set LOG = $1

if ($?ASK) then
    $mydir/ulaunch -geometry 225x126+507+414 -h : -m "Enter username to create" -c $LOG >& /tmp/.aa-$$
    set LOG = `/bin/awk '{print $1}' /tmp/.aa-$$`
    /bin/rm -f /tmp/.aa-$$
endif

if ("$LOG" == "") exit

# if home not given on command line, setup default
if !($?HOM) set HOM = $HMP/$LOG

# check for root privledges
if !(-w /etc/passwd) then
	Echo "ERROR, Must have SuperUser priviledges"
	Exit 1
endif

# check for read priviledges in template acct
(cd $SKL; pwd) >& /dev/null
if ($status) then
	Echo "ERROR, Cannot read contents of template directory."
	Exit 1
endif

# check if yp running
chkconfig yp
if ! ($status) then
	/bin/ypwhich -m | grep passwd >& /dev/null
	if !($status) set yp
endif

# check for account name already used
if ($?yp) then
    (/bin/ypcat passwd; cat /etc/passwd) | grep "^${LOG}:" >& /dev/null
    set s=$status
else
    (cat /etc/passwd) | grep "^${LOG}:" >& /dev/null
    set s=$status
endif

if !($s) then
	Echo "INFORMATION, '$LOG' already defined in passwd file, no action taken."
	Exit 0
endif

# if using -l, get existing account info
if ($?LIK) then

    # check for existing account name 
    # (/bin/ypcat passwd; cat /etc/passwd) | grep "^${LIK}:" >& /dev/null

    if ($?yp) then
	(/bin/ypcat passwd; cat /etc/passwd) | grep "^${LIK}:" >& /dev/null
	set s=$status
    else
	(cat /etc/passwd) | grep "^${LIK}:" >& /dev/null
	set s=$status
    endif


    if !($s) then
	
	# account found, now break out passwd line
	# set info = (`(/bin/ypcat passwd; cat /etc/passwd) | grep "^${LIK}:" | awk -F: '{printf "%s _%s %s %s %s %s", $1, $2, $3, $4, $6, $7}'`)

	if ($?yp) then
	    set info = (`(/bin/ypcat passwd; cat /etc/passwd) | grep "^${LIK}:" | awk -F: '{printf "%s _%s %s %s %s %s", $1, $2, $3, $4, $6, $7}'`)
	else
	    set info = (`(cat /etc/passwd) | grep "^${LIK}:" | awk -F: '{printf "%s _%s %s %s %s %s", $1, $2, $3, $4, $6, $7}'`)
	endif


	set PSW = "`echo $info[2]|sed s/_//`"
	set UID = $info[3]
	set GID = $info[4]
	set HOM = $info[5]:h/$LOG
	set SHL = $info[6]

    else

	# acct not found, unset -l opton
	unset LIK

    endif

endif

# if not using -l, check for uid already used
while ! ($?LIK)

    if ($?yp) then
	(/bin/ypcat passwd; cat /etc/passwd) | awk -F: '{print $3}' | grep ${UID} >& /dev/null
	set s=$status
    else
	(cat /etc/passwd) | awk -F: '{print $3}' | grep ${UID} >& /dev/null
	set s=$status
    endif

    if !($s) then
	# this uid in use, increment uid and check again
	Echo "INFORMATION, uid $UID in use, let's try another..."
	@ UID++ 
    else
	break
    endif
end

Echo "INFORMATION, user id '$UID' will be used for user '$LOG'."

set PSF = "${LOG}:${PSW}:${UID}:${GID}:Alias|Wavefront User:${HOM}:${SHL}"

Echo "INFORMATION, Adding user '$LOG' to /etc/passwd file."
echo $PSF >> /etc/passwd

# create home directory and place files for default environment

if (-d $HOM) then

    Echo "INFORMATION, '$HOM' directory exists, preserving contents."

else

    Echo "INFORMATION, Creating '$HOM' home directory."
    mkdir -m 755 -p $HOM

    Echo "INFORMATION, Installing default files:"
    Echo ""
    (cd $SKL; /bin/tar cf - .) | (cd $HOM; /bin/tar xpf -)

    chown -R $UID.$GID $HOM

endif

switch ("$PSW")

    case  disabled:
	set PSW = "<disabled>"
	breaksw

    case  "":
	set PSW = "<none>"
	breaksw

    default:
	set PSW = "<encrypted> (same as '$LIK' account)"
	breaksw

endsw

Echo ""
Echo "INFORMATION, Account successfully created"
Echo ""
Echo "New Account Summary:"
Echo ""
Echo "	User:     $LOG"
Echo "	Password: $PSW"
Echo "	User ID:  $UID"
Echo "	Group ID: $GID"
Echo "	Home:     $HOM"
Echo "	Shell:    $SHL"
Echo ""
Echo "NOTE: Use UNIX command 'passwd' to assign a password."

Exit 0


tag_usage:

cat << EOU

This program is used to quickly add a user to your
system. You must have superuser priviledges. When
executed without options, the default values are
used to create an account for <login_name>. To
override the default values, you must use the
appropriate options. See usage below.


USAGE: ${argv[0]} [options] <login_name>

	options:

	-u <uid>			(default: '1000')
	-g <gid>			(default: '20')
	-t <path_to_template_acct>	(default: 'skel')
	-s <shell>			(default: '/bin/csh')
	-h <home_directory>		(default: '/usr/people/<login_name>')
	-l <like_existing_acct>		(default: <none>)
	-a 				(confirm username)
	-d      			(create disabled password)
	-w				(output info in a window)

EOU

Exit 1

EXIT:


if ($?WIN_STAT) then

    Echo ""
    Echo ""
    if ! (-x /usr/bin/X11/xconfirm) then
	Echo "Press <Enter> to continue."
	echo 'EOF' >> /tmp/.ab$$
	echo 'line' >> /tmp/.ab$$
	/bin/chmod 755 /tmp/.ab$$

	/usr/sbin/xwsh -fn "-*-screen-bold-r-normal--16-*-*-*-m-90-iso8859-1" -bg lightgrey -fg black -cursorbg lightgrey -geometry 80x24+278+317 -name "Account Builder" -e sh /tmp/.ab$$
    else
	/usr/bin/X11/xconfirm -font "-*-screen-medium-r-normal--15-*-*-*-m-80-iso8859-1" \
			      -geometry 800x500+250+300 -icon info -useslider -file /tmp/.ab$$ >& /dev/null
    endif

    /bin/rm -f /tmp/.ab$$

endif

exit $x
