#!/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`" ] ; then
        echo "$@"\c
    else
        echo -n "$@"
    fi
}

#
# Configuration directories:
# 


echo 'Setting up config files (under /vice).'


if [ ! -d /vice ]; then
    mkdir /vice
fi

cd /vice

for i in backup db srv vol bin auth2 ; do
    if [ ! -d $i ]; then
       mkdir $i
    fi
done

if [ ! -d vol/remote ]; then
    mkdir vol/remote
fi

echo Directories under /vice are set up.
echo

#
# tokens
#
echo Setting up tokens for authentication.

cd /vice/db

for which in auth2 volutil; do
    token=""
    until [ x$token != x ]; do
	echo "The following tokens must be identical on all servers."
	echon "Enter a random token for $which authentication : "
	read token
	if [ x$token != x ]; then
            rm -f $which.tk
	    touch $which.tk
	    chmod 600 $which.tk
	    echo $token >> $which.tk
        fi
    done
done
echo tokens done!

touch auth2.lock

#
# files file for update
#

echo
echo Setting up the file list for update
cat > /vice/db/files <<EOF
ROOTVOLUME
VLDB
auth2.pw
auth2.tk
auth2.tk.BAK
auth2.lock
servers
prot_users.db
prot_index.db
volutil.tk
VRDB
files
VSGDB
dumplist
EOF

echo filelist for update ready. 

#
# populating /vice/vol
#

echo
echo Populating /vice/vol...
touch fs.lock
touch volutil.lock
echo lockfiles created.

#
# adding stuff to /etc/services if needed
#

case x`uname`  in
  xCYGWIN32_NT)
    ;;
  xCYGWIN_NT-4.0)
    ;;
  x*)
    vice-setup-ports
    ;;
esac

#
# startup scripts
#
CODABASE=
case x`uname` in
  xNetBSD)
	CODABASE=/usr/pkg
	;;
  xFreeBSD)
	CODABASE=/usr/local
	;;
esac

if [ x$CODABASE != x ]; then
yesno=""
until [ x$yesno != x ]; do
   echon 'Do you want to start the server at boot time? (y/n) '
   read yesno
   if [ x$yesno = xy  -o  x$yesno = xyes -o x$yesno = xY ]; then
       touch /vice/srv/STARTFROMBOOT
       grep "${CODABASE}/etc/rc.vice" /etc/rc.local > /dev/null 2>1
       if [ $? != 0 ]; then 
         echo "if [ -x ${CODABASE}/etc/rc.vice ]; then ${CODABASE}/etc/rc.vice start ; fi" >> /etc/rc.local 
       fi
       echo "Startup scripts now installed."
   fi
done
fi

#
# /vice/hostname 
# 
hn=`hostname`
bn=`echo $hn | cut -f 1 -d .`
# to prevent killing the hostname if it wasn't fully qualified.
if [ x$bn = x ]; then
    bn=$hn
fi

echo $bn > /vice/hostname

#
# end of common setup area, now specialize to scm or non scm
# 

isscm=N
yesno=""
until [ x$yesno != x ]; do
   echon 'Is this the master server, aka the SCM machine? (y/n) '
   read yesno
   if [ x$yesno = xy  -o  x$yesno = xyes ]; then
      isscm=Y
      echo  "Now installing files specific to the SCM..."
   else
      echo  "Now installing things specific to non-SCM machines..."
   fi
done


if [ $isscm = Y ]; then
    vice-setup-scm
    vice-setup-user
    vice-setup-rvm
    vice-setup-srvdir
    RV=`cat /vice/db/ROOTVOLUME`
    DP=`cat /vice/db/vicetab | awk '{print $2}' | head -n 1`
    echo "Congratulations: your configuration is ready...and now"
    echo "to get going do the following:"
    echo " - start the auth2 server as: auth2"
    echo " - start rpc2portmap as: rpc2portmap"
    echo " - start updateclnt as:  updateclnt -h $hn -q coda_udpsrv"
    echo " - start updatesrv as: updatesrv -p /vice/db"
    echo " - start the fileserver: startserver &"
    echo " - wait until the server is up: tail -f /vice/srv/SrvLog"
    echo " - create your root volume: createvol_rep $RV E0000100 $DP"
    echo " - setup a client: venus-setup $hn 20000"
    echo " - start venus: venus"
    echo " - enjoy Coda."

    exit 0 
fi

#
# specifics for non SCM servers only 
# 

until [ x$scmhost != x ]; do
    echon "Enter the hostname of the SCM machine : "
    read scmhost
    if [ x$scmhost != x ]; then
       echo $scmhost > /vice/db/scm
    fi
done

vice-setup-rvm
vice-setup-srvdir

echo "You have set up " `cat /vice/hostname`
echo "Your scm is " `cat /vice/db/scm`
echo "Other config files will be fetched from the SCM by updateclnt."
