#!/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
exec_prefix=${prefix}

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

#
# Configuration
#
vicedir=/vice
. "$("${exec_prefix}/sbin/codaconfedit" server.conf)"

if [ "$hn" = "" ] ; then
  if [ "$(uname -s)" != Linux ] ; then
    domain=""
    while [ "$domain" = "" ] ; do
      echon "What is the domain name for your servers? "
      read -r domain
    done
    hn=$(hostname -s).$domain
  else
    hn=$(hostname -f)
  fi
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

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

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

# Create the directory if needed.  (Symlink is OK.)

if [ -d "$srvdir" ] || [ -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=""
while [ "$yesno" = "" ]; do
    echon "Shall I set up a vicetab entry for $srvdir (y/n) "
    read -r yesno

    case "$(echo "$yesno" | tr '[:upper:]' '[:lower:]')" in
    y|yes) vicetab=Y ;;
    n|no)  vicetab=N ;;
    *) yesno="" ; echo "Please answer yes or no." ;;
    esac
done
if [ "$vicetab" = Y ] ; 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 [ "$size" != "" ]; do
        echo "Select the maximum number of files for the server."
        echo "[256K, 1M, 2M, 16M]: "
        read -r 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 -r
fi

echo
