#!/bin/sh
#
# Coda client installation script
# Made for Linux coda-client rpm distribution 
# Peter J. Braam, Dec 1996
# included suggestions from Elliot Lee, may 1997.
#
# Usage: venus-setup default-realm-name cachesize-in-kb
#
# the script will verify that the necessary directories and /etc/services
# are present.
#
# BUGS: need to substitute /etc/coda with ${prefix}/etc/coda and have configure
#	splice in the right location of the venus.conf file.
prefix=/usr/pkg
exec_prefix=${prefix}

OS=`uname -s | cut -c1-5`

if [ $OS = CYGWI ] ; then
    if [ $# != 3 ]; then
	echo "Usage: $0 default-realm-name cachesize-in-kb drive-letter"
	exit 1
    fi
else
    if [ $# != 1 -a $# != 2 ]; then
	echo "Usage: $0 default-realm-name [cachesize-in-kb]"
	exit 1
    fi
fi

REALM=$1
if [ x$2 != x ] ; then
    CACHESIZE=$2
else
    CACHESIZE=100000
fi
if [ x$3 != x ] ; then
    MOUNTPOINT=`echo $3 | cut -c1`
    if [ $MOUNTPOINT \< A -o $MOUNTPOINT \> z -o \
         \( $MOUNTPOINT \> Z -a $MOUNTPOINT \< a \) ] ; then
	echo "Drive letter must be a letter!"
	exit 1;
    fi
fi

#
# Allowing the user to edit venus.conf before creating all the various
# directories might be nice, but breaks the existing `half-automatic'
# installation
#
#restart=0
#[ ! -f /etc/coda/venus.conf ] && restart=1

[ "$REALM" ] && ${exec_prefix}/sbin/codaconfedit venus.conf realm $REALM
${exec_prefix}/sbin/codaconfedit venus.conf cacheblocks $CACHESIZE

conf="`${exec_prefix}/sbin/codaconfedit venus.conf`"

#if [ $restart = 1 ] ; then
#    echo "We just created an initial configuration file /etc/coda/venus.conf"
#    echo "You might want to look it over and modify some of the default paths"
#    echo "Rerun 'venus-setup $SERVERS $CACHESIZE' if you're happy"
#    exit
#fi

# default paths
errorlog=/usr/coda/etc/console
cachedir=/usr/coda/venus.cache
checkpointdir=/usr/coda/spool
logfile=/usr/coda/etc/venus.log
marinersocket=/usr/coda/spool/mariner
pid_file=/usr/coda/venus.cache/pid
run_control_file=/usr/coda/venus.cache/VENUS_CTRL
rvm_log=/usr/coda/LOG
rvm_data=/usr/coda/DATA
mountpoint=/coda

# override with user defined paths
if [ x${conf} != x/dev/null ] ; then
    . ${conf}
fi

#
# we need to make sure we have the following directories so that venus can
# create the files that live in them.
#
errlogdir=`dirname $errorlog`
logfiledir=`dirname $logfile`
marinersocketdir=`dirname $marinersocket`
pid_filedir=`dirname $pid_file`
run_control_file_dir=`dirname $run_control_file`
rvm_logdir=`dirname $rvm_log`
rvm_datadir=`dirname $rvm_data`

# Darn, mkdir -p -m 700 /path/to/new/dir only sets the mode of the last
# created directory correctly. The following function sets the mode of all
# newly created directories.
makedir () {
  [ "$2" = "" ] && return
  while [ ! -d $2 ] ; do
    dir=$2
    # find first existing parent
    while [ "$dir" != "" -a ! -d $dir ] ; do
	newdir=`basename $dir`
	dir=`dirname $dir`
    done
    echo "=> Creating $dir/$newdir, mode $1"
    if ! mkdir $dir/$newdir ; then
        echo "  Could not make \"$dir/$newdir\".  Aborting...."
        exit 1
    fi
    if [ $dir = / ] ; then
        dir=
    fi
    chmod $1 $dir/$newdir
  done
}

makedir 755 $checkpointdir
makedir 755 $marinersocketdir
makedir 700 $cachedir
makedir 700 $errorlogdir
makedir 700 $logfiledir
makedir 700 $pid_filedir
makedir 700 $run_control_file_dir
makedir 700 $rvm_logdir
makedir 700 $rvm_datadir

contains () {
	grep "$2" "$1" > /dev/null 2>&1
}

# tell the kernel about the new module 
if [ X$OS = XLinux ] ; then
    # make sure the module dependencies are current
    /sbin/depmod -a
    # udev managed /dev
    if [ -d /dev/.udevdb -o -f /dev/.udev.tdb ] ; then
	# udev relies on modules being autoloaded because their hardware got
	# detected. But since there is no hardware for the Coda kernel module
	# we have to make sure it is loaded automatically at boot

	# FC3
	if contains /etc/rc.sysinit /etc/rc.modules ; then
	    if [ ! -f /etc/rc.modules ] ; then
		echo "#!/bin/sh" > /etc/rc.modules
		chmod 755 /etc/rc.modules
	    fi
	    if ! contains /etc/rc.modules coda ; then
		echo "/sbin/modprobe coda" >> /etc/rc.modules
	    fi

	# debian
	elif ! contains /etc/modules coda ; then
	    echo "=> Adding 'coda' to /etc/modules so that the kernel module is"
	    echo "   loaded automatically whenever the system boots"
	    echo coda >> /etc/modules
	fi
	# since we aren't booting this time, make sure the module is loaded
	echo "== Loading coda kernel module."
	/sbin/modprobe coda && sleep 5
    fi
    # devfs managed /dev
    if [ -d /etc/devfs ] ; then
	if ! contains /etc/devfs/devices cfs0 ; then
	    echo "=> Adding 'cfs0' to /etc/devfs/devices so that devfs creates"
	    echo "   an entry for /dev/cfs0 so that the Coda kernel module"
	    echo "   can be autoloaded when the /dev/cfs0 or /dev/coda/0"
	    echo "   device is accessed"
	    echo "cfs0 c 67 0 root root 0200" >> /etc/devfs/devices
	    /etc/init.d/devfsd restart
	fi
    fi
    # static /dev
    if /sbin/modinfo -V | grep modutils > /dev/null || [ -z $(/sbin/modinfo -F alias coda) ] ; then
	# kernel module does not encode an alias for us
	if [ -f /etc/modules.conf ] ; then
	    MODULES=/etc/modules.conf
	else
	    MODULES=/etc/conf.modules
	fi
	if ! contains $MODULES coda ; then
	    echo "=> Adding aliases to $MODULES so that modprobe can"
	    echo "   handle autoloading of the Coda kernel module when"
	    echo "   /dev/cfs0 is accessed"
	    echo >> $MODULES
	    echo 'alias char-major-67 coda' >> $MODULES
	    echo 'alias /dev/coda coda' >> $MODULES
	fi
    fi
fi

if [ $OS = CYGWI ]; then

  # setup the drive mapping

  rm -rf /coda

  ln -s /cygdrive/$MOUNTPOINT /coda

  ${exec_prefix}/sbin/codaconfedit venus.conf mapprivate 0
  ${exec_prefix}/sbin/codaconfedit venus.conf mountpoint $MOUNTPOINT:
  ${exec_prefix}/sbin/codaconfedit venus.conf cache_prefix /??/`mount | grep ' / ' | cut -d' ' -f1 | tr \\\\\\\\ /`

else

  makedir 755 $mountpoint

  # set up the /coda root if it isn't there yet
  if [ ! -f $mountpoint/NOT_REALLY_CODA ]; then 
    echo 'If you can see this file, venus is not running.' > \
	$mountpoint/NOT_REALLY_CODA
  fi

  # make the psdev, on udev and devfs systems the device should exist by now
  if [ ! -c /dev/cfs0 -a ! -c /dev/coda/0 ] ; then
    if [ ! -x /dev/MAKEDEV ] ; then
      echo 'You need a character device for the Coda kernel module'
      echo 'On *BSD systems you probably have to run "mknod /dev/cfs0 c 93 0"'
    else
      ( cd /dev ; ./MAKEDEV cfs )
    fi
  fi
fi

# next run will always initialize
touch $cachedir/INIT

if [ -f /usr/coda/etc/vstab ] ; then
    echo "To avoid confusion, remove /usr/coda/etc/vstab, we have switched to"
    echo "the /etc/coda/venus.conf file for configuring the client."
fi

