#!/bin/sh 

#
# 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."
	echo -n "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!

#
# 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
pro.db
servers
vice.pdb
vice.pcf
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
#

vice-setup-ports


#
# 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
   echo -n '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=${hn%%.*}
# 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
   echo -n '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
    echo -n "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."






