#!/bin/bash
#
# This script installs and initializes OpenAFS version 1.2.10 for a
# Red Hat 9 system.
#
# Released under GPL.
#
# (C) 2003 by T. Bubeck, t.bubeck@reinform.de
#

RPM_PREFIX="http://www.openafs.org/dl/openafs/1.2.10"
#RPM_PREFIX="/mnt/tmp/download/openafs"

CELLSERVDB="/usr/vice/etc/CellServDB"

function verbose_exec() 
{
    echo "$@"
    "$@"
    return $?
}

function safe_exec() 
{
    verbose_exec "$@"
    if [ $? -ne 0 ]; then
	echo "failure. exit."
	exit 1
    fi
}

function install_rpms() 
{
    if grep -q "Red Hat Linux release 9 (Shrike)" /etc/redhat-release ; then
	RPMS="openafs-1.2.10-rh9.0.1 openafs-kernel-1.2.10-rh9.0.1 openafs-client-1.2.10-rh9.0.1"
	DISTRIBUTION="redhat-9.0";
    elif grep -q "Fedora" /etc/redhat-release ; then
	RPMS="openafs-1.2.10-fd1.0.1 openafs-kernel-1.2.10-fd1.0.1 openafs-client-1.2.10-fd1.0.1"
	DISTRIBUTION="fedora-1.0";
    else
	echo "fatal: this script could only be run on RH 9 oder Fedora 1"
	exit 1
    fi

    for rpm in $RPMS; do
    # check to see, if this package is not yet installed
	rpm -q $rpm > /dev/null 2>&1
	if [ $? -ne 0 ]; then
	    safe_exec rpm -i "$RPM_PREFIX/$DISTRIBUTION/$rpm.i386.rpm"
	fi
    done
}

function read_ip() 
{
    echo -n "IP adress of the AFS server installed before"
    echo -n "? "
    read ip_address
    if [ -z "$ip_address" ]; then
	ip_address=$ip
    fi
}

function read_this_cell() 
{
    echo -n "Name of cell to install"
    dnsdomainname=$(dnsdomainname)
    if [ ! -z "$dnsdomainname" ]; then
	echo -n " [$dnsdomainname]"
    fi
    echo -n "? "
    read ThisCell
    if [ -z "$ThisCell" ]; then
	ThisCell=$dnsdomainname
    fi
}

function config_files() 
{
    # [1] enter cell name into a single file and use symbolic links
    rm -f /usr/vice/etc/ThisCell
    echo "$ThisCell" > /usr/vice/etc/ThisCell

    # [2] Turn off AFS client and turn on AFS server in /etc/sysconfig/afs
    sed 's/^AFS_CLIENT=.*$/AFS_CLIENT=on/g' < /etc/sysconfig/afs | sed 's/^AFS_SERVER=.*$/AFS_SERVER=off/g' > /tmp/afs.$$
    mv /tmp/afs.$$ /etc/sysconfig/afs

    # [3] get IP-Adress
    read_ip

   # if [ ! -L /usr/vice/etc/CellServDB ]; then
#	mv /usr/vice/etc/CellServDB /usr/afs/etc/CellServDB
#	ln -s /usr/afs/etc/CellServDB  /usr/vice/etc/CellServDB
#    fi

    # [4] Enter ThisCell/IP in CellServDB if not already present
    if grep -q $ThisCell $CELLSERVDB; then
	:
    else
	echo ">$ThisCell" >> $CELLSERVDB
	echo "$ip_address        # $HOSTNAME" >> $CELLSERVDB
    fi
}

cat <<EOF
# This script installs and initializes OpenAFS version 1.2.10 for a
# Red Hat 9 or Fedora 1 system.
#
# Released under GPL.
#
# (C) 2003 by T. Bubeck, t.bubeck@reinform.de
#

This script will install a AFS client on this machine. It will first download 
the necessary RPM files from www.openafs.org, install them and then configure
and run AFS.

You can re-run this script at any time, if there is a problem during execution.
In this case you will see some errors (like "already exists") but the
script will work.

Press Return to continue;
EOF
read line

install_rpms
read_this_cell
config_files

echo starting AFS for the first time. This will take a while to fill the cache.
/etc/init.d/afs restart

cat <<EOF


AFS is now up and running. You could go to /afs/$ThisCell to look for the two 
volumes created just a few seconds ago. Create new volumes and have fun with
AFS.

T. Bubeck, t.bubeck@reinform.de, http://www.reinform.de

EOF
exit 0


