#!/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
}

#
# Configuration
#
prefix=/usr
conf=/etc/coda/server.conf
if [ -f ${conf} ] ; then
  . ${conf}
fi

if [ x${vicedir} = x ] ; then
  vicedir=/vice
fi

#
# Setting up the server directory
#

echo 
echo Your server directories will hold the files \(not directories\). 
echo You can currently only have one directory per disk partition.
echo

echon 'Where shall we store your data [/vicepa]? '
srvdir=/vicepa
read srvdir

if [ "x$srvdir" = x ]; then
    srvdir=/vicepa
fi

# set up a symlink if needed

if [ -d "$srvdir" -o -h "$srvdir" ]; then
    if [ -f "$srvdir/FTREEDB" ]; then
      echo "An FTREEDB exists in $srvdir.  Clean up first and rerun $0"
      echo "You may also want to clean up ${vicedir}/db/vicetab"
      exit 1
    fi
else
    mkdir "$srvdir"
    if [ $? != 0 ]; then
   	 echo Could not make $srvdir as a directory. Exiting.
  	 echo Run vice-setup-srvdir to repeat this step.
  	 exit 1
    fi
fi
touch "$srvdir/FTREEDB"

hn=`hostname`
bn=`echo $hn | cut -f 1 -d .`
if [ x$bn = x ]; then
    bn=$hn
fi

until [ "x$yesno" != x ]; do
   echon "Shall I set up a vicetab entry for $srvdir (y/n) "
   read yesno
done
if [ "x$yesno" = xy -o "x$yesno" = xyes -o "x$yesno" = xY ]; then
    grep $srvdir ${vicedir}/db/vicetab >/dev/null 2>&1
    if [ $? = 0 ]; then
	echo "$srvdir already in ${vicedir}/db/vicetab. Please clean up first."
	exit 1
    fi

    size=""
    until [ "x$size" != x ]; do
        echo "Select the maximum number of files for the server."
	echo "[256K, 1M, 2M, 16M]: "
	read size
	case "$size" in
	  256[kK] )
    	      echo "$bn   $srvdir   ftree   width=64,depth=3" >> ${vicedir}/db/vicetab
	      ;;
 	  1[mM] )
    	      echo "$bn   $srvdir   ftree   width=32,depth=4" >> ${vicedir}/db/vicetab
	      ;;
 	  2[mM] )
    	      echo "$bn   $srvdir   ftree   width=128,depth=3" >> ${vicedir}/db/vicetab
	      ;;
 	  16[mM] )
    	      echo "$bn   $srvdir   ftree   width=256,depth=3" >> ${vicedir}/db/vicetab
	      ;;
          * )
	      echo "Choose from one of the listed values."
	      size=""
	      ;;
	esac
    done	      

#   echo "Now initializing the partition. This takes a while...."
#   makeftree ${vicedir}/db/vicetab $srvdir   --  commented out by phil.
#    echo 'done.'
    echo
    echo 'Server directory is set up!'
else
    echo
    echo  "Read vicetab(5) and makeftree(8) for set up info."
    echo
    echo  "Press return to continue"
    read ok
fi

echo
