#!/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: coda-client-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
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 ] && [ $# != 2 ]; then
	echo "Usage: $0 default-realm-name [cachesize-in-kb]"
	exit 1
    fi
fi

REALM=$1
if [ "$2" != "" ] ; then
    CACHESIZE=$2
else
    CACHESIZE=100000
fi
if [ "$3" != "" ] ; then
    MOUNTPOINT=$(echo "$3" | cut -c1)
    case "$MOUNTPOINT" in
    [A-Za-z]) ;;
    *) echo "Drive letter must be a letter!" ; exit 1 ;;
    esac
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 'coda-client-setup $REALM $CACHESIZE' if you're happy"
#    exit
#fi

# default paths
errorlog=/var/log/coda/venus.err
cachedir=/var/lib/coda/cache
checkpointdir=/var/lib/coda/spool
logfile=/var/log/coda/venus.log
marinersocket=/var/run/coda-client.mariner
pid_file=/var/run/coda-client.pid
run_control_file=/var/run/coda-client.ctrl
rvm_log=/var/lib/coda/LOG
rvm_data=/var/lib/coda/DATA
mountpoint=/coda

# override with user defined paths
if [ "${conf}" != /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" != "" ] && [ ! -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 "$errlogdir"
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 [ "$OS" = "Linux" ] ; then
    # make sure the module dependencies are current
    /sbin/depmod -a
    # udev managed /dev
    if [ -d /dev/.udevdb ] || [ -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
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 ] && [ ! -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 || exit ; ./MAKEDEV cfs )
    fi
  fi
fi

# next run will always initialize
touch $cachedir/INIT
