#!/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/afs/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 openafs-server-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 openafs-server-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() 
{
    if hostname | grep -q localhost; then
	echo "WARNING: hostname must be set and resolvable by DNS"
    fi

    echo -n "IP adress of this server"
    uname=$(uname -n)
    ip=$(host $uname | awk '{print $NF}')
    if [ ! -z "$ip" -a "$ip" != "3(NXDOMAIN)" ]; then
      echo -n " [$ip]"
    else
      ip="127.0.0.1"
    fi
    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 get_partition() 
{
    partition=`vos listpart localhost | grep /vicep | awk '{print $1}' 2>/dev/null`
    if [ -z "$partition" ]; then
	echo "no /vicep partition found. Please create a partition vicepa and mount it using /etc/fstab and restart this script."
	exit 1
    fi
}

function create_volume() 
{
    volume=$1
    shift
    get_partition
    verbose_exec vos create localhost $partition $volume -cell $ThisCell "$@"
}

function create_and_mount_volume()
{
    volume=$1
    shift
    mount_point=$1
    shift
    create_volume $volume "$@"
    verbose_exec fs mkmount $mount_point $volume
    verbose_exec fs setacl $mount_point system:anyuser l
}

function config_files() 
{
    # [1] enter cell name into a single file and use symbolic links
    rm -f /usr/vice/etc/ThisCell /usr/afs/etc/ThisCell
    mkdir /usr/afs/etc 2>/dev/null
    echo "$ThisCell" > /usr/afs/etc/ThisCell
    ln -s /usr/afs/etc/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=on/g' > /tmp/afs.$$
    #sed 's/^AFS_CLIENT=.*$/AFS_CLIENT=on/g' < /etc/sysconfig/afs | sed 's/^AFS_SERVER=.*$/AFS_SERVER=on/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
}

function start_bos() 
{
    # Shut down old "bos" if running
    echo "shutting down old \"bos\""
    bos shutdown localhost -localauth -wait > /dev/null 2>&1
    pkill bosserver

    echo "starting and configuring new \"bos\""
    safe_exec /usr/afs/bin/bosserver -noauth
#    safe_exec bos setcellname localhost $ThisCell -noauth
    verbose_exec bos create localhost kaserver simple /usr/afs/bin/kaserver -cell $ThisCell -noauth
    verbose_exec bos create localhost ptserver simple /usr/afs/bin/ptserver -cell $ThisCell -noauth
    verbose_exec bos create localhost vlserver simple /usr/afs/bin/vlserver -cell $ThisCell -noauth

    verbose_exec kas create -name afs -initial_password admin -cell $ThisCell -noauth
    verbose_exec kas create -name admin -initial_password admin -cell $ThisCell -noauth
    verbose_exec kas setfields -name admin -flags admin -cell $ThisCell -noauth

    verbose_exec bos adduser localhost admin -cell $ThisCell -noauth
    verbose_exec bos addkey localhost -key admin -kvno 0 -cell $ThisCell -noauth
    verbose_exec pts createuser -name admin -cell $ThisCell -id 1 -noauth
    verbose_exec pts adduser admin system:administrators -cell $ThisCell -noauth
    safe_exec bos restart localhost -all -cell $ThisCell -noauth
    verbose_exec sleep 20

    verbose_exec bos create localhost fs fs /usr/afs/bin/fileserver /usr/afs/bin/volserver /usr/afs/bin/salvager -cell $ThisCell -noauth
}

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 server 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
start_bos
create_volume root.afs -noauth

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

verbose_exec sleep 20

verbose_exec klog -principal admin -password admin
verbose_exec fs setacl /afs system:anyuser l

create_and_mount_volume root.cell /afs/$ThisCell
create_and_mount_volume home /afs/$ThisCell/home
create_and_mount_volume software /afs/$ThisCell/software
create_and_mount_volume software.emacs-21.3 /afs/$ThisCell/software/emacs-21.3

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.

Don't forget to change the password for "admin".

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

EOF
exit 0


