#!/bin/csh -f
#
# This script can be used to add a user to the password file on
# a public workstation.
#

alias passwdcrypt /afs/athena/user/q/qjb/${hosttype}bin/passwdcrypt mu
alias namesearch "grep -c '^'\!:1':.*' /etc/passwd"
alias uidsearch "grep -c '^[^:]*:[^:]*:'\!:1':.*' /etc/passwd"

if ($uid != 0) then
   echo -n "You must be root to use this script.  Use 'su' to"
   echo "become root."
   exit 1
endif

access_on

echo "Please enter username and password at the following prompts."
echo "The password will not be echoed when you type it.  You will be"
echo "prompted for it twice to verify that you have typed it in correctly."
echo " "

set found = 1
while ($found)
   echo -n "Username: "
   set name = $<
   set found = `namesearch $name`
   if ($found == 0) then
      hesinfo $name passwd >& /dev/null
      if ($status == 0) then
         set found = 1
      endif
   endif
   if ($found) then
      echo "User $name already exists.  Pick another username."
   endif
end

set pw1 = 0
set pw2 = 1
while ($pw1 != $pw2) 
   echo -n "Enter password: "
   set pw1 = `passwdcrypt`
   set pw1 = $pw1[2]
   echo " "
   echo -n "Enter it again for verification: "
   set pw2 = `passwdcrypt`
   set pw2 = $pw2[2]
   echo " "
   if ($pw1 != $pw2) then
      echo "Mismatch - try again."
   endif
end

set found = 1
set id = 32000
while ($found) 
   @ id ++
   set found = `uidsearch $id`
end

@ n = $id - 32000

set pwstr = "${name}:${pw1}:${id}:101:Temporary User ${n}:/tmp/${name}:/bin/csh"

mkdir /tmp/$name
if ($status) then
   echo "Failure creating home directory -- try another username."
   exit 2
endif

cat << EOF >! /tmp/$name/.login
stty dec
echo "Enter your terminal type.  Default appears in parentheses."
set noglob; eval \`tset -s -I -Q \?vt100\`
set ignoreeof
alias logout exit
EOF

cat << EOF >! /tmp/$name/.cshrc
set history = 100
set lineedit
set prompt = "athena% "
EOF

echo $pwstr >> /etc/passwd

/etc/chown $name /tmp/$name/.{login,cshrc}

echo "User $name added."

exit 0

