#!/bin/sh


if [ -e /vice/srv.conf ]; then 
   grep '^\-rvm ' /vice/srv.conf
   if [ $? = 0 ]; then
       echo "You have already got rvm parameters in srv.conf."
       echo "Remove these up first."
   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
   echo -n "Are you ready to set up RVM? [yes/no] "
   read yesno
   if [  x$yesno = xyes  -o  x$yesno = xy ]; then
       echo ""
   else
       echo "Run vice-rvmsetup when you are ready!"
       exit 
   fi
done

#
# we need a size and partition for the log.
#
until [ x$log != x ]; do
   echo -n "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."
   echo -n "What is your log size? (enter as e.g. '12M') "
   read logsize
    echo
done

until [ x$data != x ]; do
   echo -n "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 330M only on very fast machines)"
   echo  "Pick one of the defaults, otherwise I will bail out"
    echo
   echo -n "What is the size of you data partition (or file) [22M,44M, 90M, 130M, 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
echo -n "Proceed, and wipe out old data? [y/N] "
answer=n
read answer 
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 ;;
        FreeBSD\ 2.2* )
                rvmstart=0x50000000 ;;
        *)
                rvmstart=0x20000000 ;;
esac

case $datasize in 
    22M )
       dsparm=20480000
       heapsize=0x1200000
       ;;
     44M )
       dsparm=39690240
       heapsize=0x2400000
        ;;
     90M )
       dsparm=79380480
       heapsize=0x4a00000
        ;;
     130M )
       dsparm=119070700
       heapsize=0x7000000
        ;;
     315M )
       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 
