#!/bin/sh
# BLURB gpl
#
#                            Coda File System
#                               Release 6
#
#           Copyright (c) 1987-2016 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
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=
while [ "$userid" = "" ]; do
 	echon "Enter the uid of this user: "
 	read -r userid
 	if [ "$userid" = "" ]; then
 	    echo "Nothing cannot be a Coda administrator"
	    continue
 	fi

        # test if $userid is an integer, a portable looking solution from
        # https://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash
        if [ "$userid" -eq "$userid" ] 2>/dev/null; then
            echo "nop" > /dev/null
        else
            echo "UID should be an integer value"
            userid=
            continue
        fi

 	if [ "$userid" = 0 ] || [ "$userid" = 1 ]; then
 	    echo "Choose a user ID other than 0 or 1"
	    userid=
	    continue
 	fi
done

username=
until [ "$username" != "" ]; do
 	echon "Enter the username of this user: "
 	read -r username
 	if [ "$username" = "" ]; then
 	    echo "Nothing cannot be a Coda administrator"
	    continue
 	fi

 	if [ "$username" = root ] || [ "$username" = system ]; 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 )
