#!/bin/sh
# BLURB gpl
# 
#                            Coda File System
#                               Release 6
# 
#           Copyright (c) 1987-2003 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
# 
#*/

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

# exit if errors
set -e

#
# Configuration
#
vicedir=/vice
. "`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) and username (not root)"
echo "for a Coda System:Administrator member on this server,"
echo "(sort of a Coda super user)"
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 ]; then
 	    echo "Root cannot be a Coda administrator"
	    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 ]; then
 	    echo "Root cannot be a Coda administrator"
	    username=
	    continue
 	fi
done

if [ -f prot_users.cdb ] ; then
  echo "Going to rebuild 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

pdbtool source pdbsetup
rm pdbsetup

echo "$userid	changeme	admin user" > passwd.coda
initpw -k "drseuss " < passwd.coda > auth2.pw
rm passwd.coda

echo "An initial administrative user $username (id $userid)"
echo "with Coda password \"changeme\" now exists."

