#!/bin/csh -f

#   Setupacct:

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

#   If ALIAS_LOCATION is set and not NULL, determine the value of
#   symlink_to_alias from it. Otherwise, assume the value of the variable
#   is /usr/aw/alias.

if ($?ALIAS_LOCATION) then

#   Testing the number of elements of an enviroment variable doesn't seem
#   to work, so we use a shell variable instead.

    set alocn = $ALIAS_LOCATION
    if ($#alocn) then
        set symlink_to_alias = $ALIAS_LOCATION
    endif
endif

if (! $?symlink_to_alias) then
    set symlink_to_alias = /usr/aw/alias
endif

set CDALIAS = Products/Alias3D/alias
set SKELDIR = etc/acct

#   If we are installing/updating a demo account from the Alias|Wavefront
#   Alias Studio CD-ROM, the account skeleton area is located in
#   /CDROM/Products/Alias3D/alias/etc/acct/skel.
#
#   If we are installing/updating a demo account from the Alias Studio software,
#   as installed on disk,  the account skeleton area is located in
#   /usr/aw/alias/etc/acct/skel.
#
#   The same applies to the location of the AcctBldr and awupdate scripts.

if ($?on_cd) then
    set ABLDR = $ALIAS_CDLOCN/$CDALIAS/$SKELDIR/AcctBldr
    set AUPDATE = $ALIAS_CDLOCN/$CDALIAS/$SKELDIR/awupdate
else
    set ABLDR = $symlink_to_alias/$SKELDIR/AcctBldr
    set AUPDATE = $symlink_to_alias/$SKELDIR/awupdate
endif

if (-d /var/tmp && -w /var/tmp) then
    set TMPDIR = /var/tmp
else if (-d /usr/tmp && -w /usr/tmp) then
    set TMPDIR = /usr/tmp
else        
    set TMPDIR = /tmp
endif

#    Get the userid of the current user. If the current user is the
#    superuser (userid is zero), ask the user which account should
#    be created/updated.
#
#    If the current user is not the superuser, update that user's
#    account.

set ruid = `id | sed "s/uid=\([0-9]*\)(.*/\1/"`
if ($ruid == 0) then
    set superuser
endif

set OUT = $TMPDIR/out.$$

if ($?superuser) then

#    If the superuser wants to setup a demo account, ask them the preferred
#    username and check for it's existence. Verify that the specified username
#    is not more than 8 characters long.

try_again:

    echo ""
    while (1)
        echo -n "What is the username of the account? (i.e.: awdemo): "
        set pname = $<
        set usrname = ($pname)
        if ($#usrname == 1) then
            set first8 = `echo $usrname | cut -c1-8`
            if ("$first8" != "$usrname") then
                echo ""
                echo "The username $usrname is too long."
                echo ""
                echo "It must be a maximum of 8 characters.  Please try again."
                echo ""
            else
                break
            endif
        endif
    end

#    Test for existence of the demo account by echoing the pathname of the
#    demo user's home directory. This will fail for one reason:
#
#        - There is no such user
#
#    The echo has to be run in a sub-shell to split stdout from stderr.
#
#    We assume that if the echo writes something to stderr, the account does
#    not exist and attempt to create it using AcctBldr. It adds a password
#    entry to the local password file.
#
#    AcctBldr doesn't exit with a non-zero status if the account already exists,
#    so we have to check for a local password entry before and after to
#    determine if an entry is added. If a password entry is added to the local
#    password file, execute the IRIX passwd command to set it's password.

    echo ""
    echo "Checking for existence of the $usrname account ..."
    (echo ~$usrname > /dev/null) >& $OUT
    if (! -z $OUT) then
        grep -s "^${usrname}:" /etc/passwd >& /dev/null
        if ($status != 0) then
            set nopwentery
        endif
        echo ""
        echo "The $usrname account does not appear to exist ... Creating ..."
        echo ""
        $ABLDR -d $usrname
        grep -s "^${usrname}:" /etc/passwd >& /dev/null
        if ($status == 0 && $?nopwentery) then
            echo ""
            echo "A password entry for the $usrname account has been added to the local"
            echo "password file."
            echo ""
            echo "Please specify a password for the $usrname account."
            passwd $usrname
            echo ""
            echo "If your site uses NIS to administer user accounts, you may wish to add the"
            echo "password entry for the $usrname account to the NIS master password file and"
            echo "remove it from the local password file."
            echo ""
        endif
        set create
    endif
else
    set usrname = `id | sed "s/.*(\(.*\)) .*/\1/"`
endif

if (! $?create) then

#    If the demo account appears to exist, run the update script to update
#    its dotfiles so that it can run the Alias software.
#
#    If the update script exits with a non-zero exit status, it means that
#    the home directory of the demo account does exist and the account can't
#    be customized to use the Alias software. If this happens, ask the
#    installer if they wish to try setting up another demo account. If they
#    say `yes', try again, else continue.
#
#    If the update of the demo account succeeds, continue.

    echo ""
    echo -n "Do you wish to update this account to use the Alias software? (yes/no): "
    while (1)
        set ans = $<
        if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
            echo -n "Please specify yes or no: "
        else
            break 
        endif
    end
    if ($ans =~ [Yy][Ee][Ss]) then
        echo ""
        echo ""
        echo "Updating existing $usrname account to use the Alias|Wavefront software ..."

#    When executing the update script, we make the assumption that the script
#    will copy the appropriate files to the home directory of the user. We
#    would rather run the update script without the `-' flag of the su command
#    as the flag causes the user's existing dotfiles to be interpreted. This
#    can cause errors.

        su $usrname -c $AUPDATE
        if ($status != 0) then
            echo ""
            echo -n "Do you wish to try setting up a demo account again? (yes/no): "
            while (1)
                set ans = $<
                if ("$ans" !~ [yY][eE][sS] && "$ans" !~ [nN][oO]) then
                    echo -n "Please specify yes or no: "
                else
                    break 
                endif
            end
            if ($ans =~ [Yy][Ee][Ss]) goto try_again
        else
            set update
        endif
    endif
endif

rm -f $OUT

if ($?create || $?update) then
    echo ""
    echo -n "The $usrname account has been "
    if ($?create) then
        echo -n "created"
    else
        echo -n "updated"
    endif
    echo " to use the Alias|Wavefront software."
endif

echo ""
echo -n "Press <ENTER> to continue: "
set a = $<
exit 0
