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

fullname() {
    ( cd `dirname $1` ; echo `pwd`/`basename $1` )
}

# exit if errors
set -e

# do the unlimit in case needed
if ulimit -d unlimited ; then
  echon
fi

#
# Configuration
#

# defaults, will be overridden by settings in the server.conf file
vicedir=/vice

conf="`codaconfedit server.conf`"
if [ "x${conf}" != x/dev/null ] ; then
  . ${conf}
  haveconf=1
else
  haveconf=0
  numservers=1
fi

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

echo
echo A server needs a small log disk partition, preferrably on a disk by
echo itself. It also needs 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

if [ $numservers -gt 1 ]; then
  echo For multiple servers on the same machine, you will need to specify a log
  echo partition and a metadata partition for each server you plan to run.
fi

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-setup-rvm when you are ready!"
    exit 1
fi

#
# Do the following for each server ...
#

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

    if [ $numservers -gt 1 ]; then
        conf="`codaconfedit server_$n.conf`"
	if [ "x$conf" != x/dev/null ]; then
	    . $conf
	fi
	if [ x$hostname != x ]; then
            echo "Configuring for server $n ($hostname)."
	else
	    echo "No initial server_$n.conf found, something went wrong with vice-setup?"
	    exit 1

#	    echo "Configuring for server $n."
#	    hostname=""
#	    while [ x$hostname = x ]; do
#	        echon "What is the hostname for server $n? "
#		read hostname
#            done
#	    echo > $conf << EOF
#hostname="$hostname"
##rvm_log=""
##rvm_data=""
##rvm_data_length=""
##mapprivate=""
#EOF
	fi
    fi

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

    logsize=""
    until [ "x$logsize" != x ]; do
        echo
	echo "The log size must be smaller than you log partition.  We"
	echo "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

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

    datasize=""
    until [ x$datasize != x ]; do
	echo
	echo  "The data size must be approx 4% of you server file space.  We"
	echo  "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  "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 

	case $datasize in 
	    22[mM] | 44[mM] | 90[mM] | 130[mM] | 200[mM] | 315[mM] )
	        ;;
	    500[mM] | 1[gG] )
	        echo 
		echo "!!!!!!!!!!!!!!"
		echo Your size is an experimental size.  Be warned!
		echo You may want to run with private mapping for RVM.
		echo 
		;;
	    * )
		echo "You have selected an unsupported size and this script"
		echo "will not setup your RVM with this size.  Are you sure"
		echon "you want this size?  (yes/no) "
		yesno=
 		read yesno
		if [ x$yesno != xyes ]; then
		    datasize=""
		fi
		;; 
	esac
    done

    log=`fullname $log`
    data=`fullname $data`

    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
    answer=""
    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
    /usr/sbin/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 "$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
	    ;;
	500[mM] )
	    dsparm=524288000
	    heapsize=0x1e000000
	    ;;
	1[gG] )
	    dsparm=1073741824
	    heapsize=0x3FF00000
	    ;;
	* )
	    echo "You have chosen a data size that is not supported by this"
	    echo "coda server setup procedure. Read rdsinit(8) and rdsinit" 
	    echo "to configure your data area.  After configuring the RVM data"
 	    echo "area you also need to create the configuraton files:"
	    echo "    a file ${vicedir}/srv.conf containing:"
	    echo
	    echo "    -rvm $log $data <size of rvm data in bytes>"
	    echo
	    echo "or add the proper entries in the server conf file $conf."
	    echo
	    echo "Press return to continue"
	    read ok
	    exit 1
	    ;; 
    esac

    rvmstart=0x50000000
    staticsize=0x100000
    nlists=80
    chunk=32

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

    if [ $haveconf = 1 ] ; then
	conffile="`basename $conf`"
	codaconfedit $conffile rvm_log $log
	codaconfedit $conffile rvm_data $data
	codaconfedit $conffile rvm_data_length $dsparm
    else
	echo "-rvm $log $data $dsparm" >> ${vicedir}/srv.conf
    fi

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

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

    n=`expr $n + 1`
    echo
done

echo RVM setup is done!
echo 
