#!/bin/sh
# BLURB gpl
# 
#                            Coda File System
#                               Release 5
# 
#           Copyright (c) 1987-1999 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.db ] ; then
  echo "Going to rebuild the protection databases"
  echo "moving ${vicedir}/db/prot_users.db to ${vicedir}/db/prot_users.db.old"
  mv prot_users.db prot_users.db.old
elif [ -f prot_users.dir ] ; then
  echo "Going to rebuild the protection databases"
  echo "moving ${vicedir}/db/prot_users.[dir|pag] to ${vicedir}/db/prot_users.[dir|pag].old"
  mv prot_users.dir prot_users.dir.old
  mv prot_users.pag prot_users.pag.old
fi
if [ -f prot_index.db ] ; then
  echo "moving ${vicedir}/db/prot_index.db to ${vicedir}/db/prot_index.db.old"
  mv prot_index.db prot_index.db.old
elif [ -f prot_index.dir ] ; then
  echo "moving ${vicedir}/db/prot_index.[dir|pag] to ${vicedir}/db/prot_index.[dir|pag].old"
  mv prot_index.dir prot_index.dir.old
  mv prot_index.pag prot_index.pag.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."

