#!/bin/sh
# BLURB gpl
# 
#                            Coda File System
#                               Release 6
# 
#           Copyright (c) 1987-2008 Carnegie Mellon University
#                   Additional copyrights listed below
# 
# This  code  is  distributed "AS IS" without warranty of any kind under
# the terms of the GNU General Public Licence Version 2, as shown in the
# file  LICENSE.  The  technical and financial  contributors to Coda are
# listed in the file CREDITS.
# 
#                         Additional copyrights
#                            none currently
# 
#*/

prefix=/usr/pkg
exec_prefix=${prefix}

echon() {
    if [ "`echo -n`" = "-n" ] ; then
        echo "$@"'\c'
    else
        echo -n "$@"
    fi
}

# exit if errors
set -e

#
# Configuration
#
vicedir=/vice
. "`${exec_prefix}/sbin/codaconfedit server.conf`"

# Set up password and group files for Coda

cd ${vicedir}/db

echo "Setting up users and groups for Coda"
echo
echo "You need to give me a uid (not 0 or 1) and username (not root)"
echo "for a Coda System:Administrator member on this server,"
echo "(sort of a Coda super user)"
echo
echo "I will create the initial administrative user with Coda password"
echo "\"changeme\". This user/password is only for authenticating with"
echo "Coda and not for logging into your system (i.e. we don't use"
echo "/etc/passwd authentication for Coda)"
echo

 
userid=
until [ "x$userid" != x ]; do
 	echon "Enter the uid of this user: "
 	read userid
 	if [ "x$userid" = x ]; then
 	    echo "Nothing cannot be a Coda administrator"
	    continue
 	fi
  
 	if [ "x$userid" = x0 -o "x$userid" = x1 ]; then
 	    echo "Choose a user ID other than 0 or 1"
	    userid=
	    continue
 	fi
done

username=
until [ "x$username" != x ]; do
 	echon "Enter the username of this user: "
 	read username
 	if [ "x$username" = x ]; then
 	    echo "Nothing cannot be a Coda administrator"
	    continue
 	fi
 
 	if [ "x$username" = xroot -o "x$username" = xsystem ]; then
 	    echo "Choose an administrator username other than root or system"
	    username=
	    continue
 	fi
done

if [ -f prot_users.cdb ] ; then
  echo "Going to reinitialize the protection databases"
  echo "moving ${vicedir}/db/prot_users.cdb to ${vicedir}/db/prot_users.cdb.old"
  mv prot_users.cdb prot_users.cdb.old
fi

cat > pdbsetup <<EOF
nui System 1
nui $username $userid
ng System:Administrators $userid
ng System:AnyUser System
EOF

${exec_prefix}/sbin/pdbtool source pdbsetup
rm pdbsetup

# make sure we don't make the auth2.pw file world-readable
( umask 077
echo "$userid	changeme	admin user" > passwd.coda
${exec_prefix}/sbin/initpw -k "drseuss " < passwd.coda > auth2.pw
rm passwd.coda )

