#!/bin/sh
# BLURB gpl
# 
#                            Coda File System
#                               Release 6
# 
#           Copyright (c) 1987-2008 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/pkg
exec_prefix=${prefix}

echon() {
    if [ "`echo -n`" = "-n" ] ; then
        echo "$@"'\c'
    else
        echo -n "$@"
    fi
}

#
# Configuration
#
vicedir=/vice
numservers=1
. "`${exec_prefix}/sbin/codaconfedit server.conf`"

if [ x$hn = x ] ; then
  if [ `uname -s` != Linux ] ; then
    domain=""
    while [ x$domain = x ] ; do
      echo -n "What is the domain name for your servers? "
      read domain
    done
    hn=`hostname -s`.$domain
  else
    hn=`hostname -f`
  fi
fi

n=1
while [ $n -le $numservers ]; do

    # server information prefix
    if [ $numservers -gt 1 ]; then
	srvprefix=${vicedir}/server_$n
	. "`${exec_prefix}/sbin/codaconfedit server_$n.conf`"
	hn=$hostname
    else
	srvprefix=${vicedir}
    fi

    #
    # Setting up the server directory
    #

    echo
    echo Directories on the server will be used to store container files
    echo that hold the actual data of files stored in Coda. Directory
    echo contents as well as metadata will be stored in the RVM segment
    echo that we already configured earlier.
    echo
    echo You should only have one container file hierarchy for each disk
    echo partition, otherwise the server will generate incorrect
    echo estimates about the actual amount of exportable disk space.
    echo
    
    if [ $numservers -gt 1 ]; then
	echon "Where shall we store your file data for server $n ($hn) [/vicepa]? "
    else
	echon 'Where shall we store your file data [/vicepa]? '
    fi
    srvdir=
    read srvdir

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

    # Create the directory if needed.  (Symlink is OK.)
    
    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"
    
    yesno=""
    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 "$hn   $srvdir   ftree   width=64,depth=3" \
			 >> ${vicedir}/db/vicetab
		    ;;
		1[mM] )
		    echo "$hn   $srvdir   ftree   width=32,depth=4" \
			 >> ${vicedir}/db/vicetab
		    ;;
		2[mM] )
		    echo "$hn   $srvdir   ftree   width=128,depth=3" \
			>> ${vicedir}/db/vicetab
		    ;;
		16[mM] )
		    echo "$hn   $srvdir   ftree   width=256,depth=3" \
			>> ${vicedir}/db/vicetab
		    ;;
		* )
		    echo "Choose from one of the listed values."
		    size=""
		    ;;
	    esac
	done	      
	echo
	echo "Server directory $srvdir is set up!"
    else
	echo
	echo  "Read vicetab(5) for set up info."
	echo
	echo  "Press return to continue"
	read ok
    fi

    echo

    n=`expr $n + 1`
done
