#!/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 @sysconfdir@ and have configure
#	splice in the right location of the venus.conf file.
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=40000
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

codaconfedit venus.conf realm $REALM
codaconfedit venus.conf cacheblocks $CACHESIZE

#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 [ -f /etc/coda/venus.conf ] ; then
    . /etc/coda/venus.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_crontrol_file_dir
makedir 700 $rvm_logdir
makedir 700 $rvm_datadir

if [ $OS = CYGWI ]; then

  # setup the drive mapping

  rm -rf $mountpoint  

  ln -s /cygdrive/$MOUNTPOINT $mountpoint

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

  # now set up venus as a service

  /usr/local/sbin/venus -install

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
  if [ ! -c /dev/cfs0 -a ! -c /dev/coda/0 ] ; then
      ( cd /dev ; ./MAKEDEV cfs )
  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

# tell the kernel about the new module 
if [ X$OS = XLinux -a ! -f /etc/debian_version ]; then 
    MODULES=/etc/conf.modules
    [ -f /etc/modules.conf ] && MODULES=/etc/modules.conf
    if ! grep coda $MODULES > /dev/null; then
        echo >> $MODULES
	echo '# old style and devfs style auto module loading'
        echo 'alias char-major-67 coda' >> $MODULES
        echo 'alias /dev/coda coda' >> $MODULES
    fi
    if [ -d /etc/devfs ] ; then
    	if ! grep cfs0 /etc/devfs/devices > /dev/null; then
	    echo "cfs0 c 67 0 root root 0200" >> /etc/devfs/devices
	    /etc/init.d/devfsd restart
	fi
    fi
    /sbin/depmod -a
fi

# Setup the ports
[ ! -f /etc/debian_version ] && coda-setup-ports

