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

# test on Solaris doesn't have a -e flag :-(
# if [ -e /vice/srv.conf ]; then
if [ -f /vice/srv.conf ]; then  
   grep '^\-rvm ' /vice/srv.conf
   if [ $? = 0 ]; then
       echo "You already have rvm parameters in srv.conf."
       echo "Remove these first."
       exit 1
   fi
fi


echo
echo You need a small log disk partition, preferrably on a disk by itself.
echo You need a metadata partition of approx 4% of you filespace.
echo 
echo For trial purposes you may give oridnary files instead of raw
echo partitions. Keep all size small if you do this. 
echo Production servers will want partitions for speed.
echo
echo     -------------------------------------------------------
echo     WARNING: you are going to play with your partitions now. 
echo     verify all answers you give.
echo     -------------------------------------------------------
echo
echo WARNING: these choices are not easy to change once you are up and running.
echo

yesno=""
until [ "x$yesno" != x ]; do
   echon "Are you ready to set up RVM? [yes/no] "
   read yesno
done
if [ "x$yesno" = xyes -o "x$yesno" = xy -o "x$yesno" = xy ]; then
    echo ""
else
    echo "Run vice-rvmsetup when you are ready!"
    exit 
fi

#
# we need a size and partition for the log.
#
until [ "x$log" != x ]; do
   echon "What is your log partition? "
   read log
done

until [ "x$logsize" != x ]; do
    echo
    echo  "The log size must be smaller than you log partition."
    echo  "We recommend not more than 30M log size, and 2M is a good choice."
    echon "What is your log size? (enter as e.g. '2M') "
    read logsize
    echo
done

until [ "x$data" != x ]; do
   echon "What is your data partition (or file)? "
   read data
done

until [ "x$datasize" != x ]; do
    echo
    echo  "The data size must be approx 4% of you server file space."
    echo  "We have templates for servers of approx: 500M, 1G, 2.2G, 3.3G, 8G"
    echo  "(you can store less, but not more on such servers)."
    echo  "The corresponding data sizes are  22M, 44M, 90M, 130M, 315M." 
    echo  "(use 315M only on very fast machines)"
    echo  "Pick one of the defaults, otherwise I will bail out"
    echo
    echo "What is the size of you data partition (or file)"
    echon "[22M,44M, 90M, 130M, 200M, 315M]: "
    read datasize 
done


echo 
echo --------------------------------------------------------
echo WARNING: DATA and LOG  partitions are about to be wiped.
echo --------------------------------------------------------
echo
echo "  --- log area: $log, size $logsize."
echo "  --- data area: $data, size $datasize."
echo
until [ "x$answer" != x ]; do
    echon "Proceed, and wipe out old data? [y/n] "
    answer=n
    read answer 
done
if [ "x$answer" = xn -o "x$answer" = xno -o "x$answer" = xN -o "x$answer" = xNo ]; then
    exit 1    
fi

echo
rvmutl > /dev/null  << EOF
i $log $logsize
q
EOF

echo
if [ $? != 0 ]; then
    echo Error in rvmutl. Exiting.
    exit 1
fi

echo LOG file has been initialized!

echo


case `uname -sr` in
        "NetBSD 1.2" )
                rvmstart=0x20000000 ;;
        NetBSD\ 1.3* )
                rvmstart=0x50000000 ;;
        NetBSD\ 1.4* )
                rvmstart=0x50000000 ;;
        FreeBSD\ * )
                rvmstart=0x50000000 ;;
        *)
                rvmstart=0x20000000 ;;
esac

case "$datasize" in 
    22[mM] )
 	dsparm=23068672 # was 20480000
 	heapsize=0x1200000
 	;;
    44[mM] )
 	dsparm=46137344 # was 39690240
 	heapsize=0x2400000
 	;;
    90[mM] )
 	dsparm=94371840 # was 79380480
 	heapsize=0x4a00000
 	;;
    130[mM] )
 	dsparm=136314880 # was 119070700
 	heapsize=0x7000000
 	;;
    200[mM] )
 	dsparm=209715200
 	heapsize=0xb000000
 	;;
    315[mM] )
 	dsparm=330301440
 	heapsize=0x13000000
 	;;
    * )
	echo 'Read rdsinit(8) use rdsinit to configure your data area.'
	exit 
        ;; 
esac

staticsize=0x100000
nlists=80
chunk=32

parms="$dsparm $rvmstart $heapsize $staticsize $nlists $chunk"

echo "-rvm $log $data $dsparm" >> /vice/srv.conf

echo
echo Rdsinit will initialize data and log.
echo This takes a while. 
rdsinit -f $log $data $parms

if [ $? != 0 ]; then
    echo Error in rdsinit. Exiting.
    exit 1
fi

echo RVM setup is done!
echo 
