#!/bin/sh
#
# $Id: setup.sh,v 1.1 2007/11/20 17:06:01 jdreed Exp jdreed $
#
# A script to setup OpenAFS and configure it for MIT.

usage() {
    echo "Usage: $0 "
    exit 255
}

setuproot=/opt/mit-openafs-setup
while getopts ":r:" opt; do
  case $opt in
      r)
	  setuproot=$OPTARG
	  if [ ! -d $setuproot ]; then
	      echo "-r was passed non-existent directory"
	      exit 255
	  fi
	  ;;
      *)
	  usage
	  ;;
  esac
done

config=$setuproot/etc/setup.config
funcs=$setuproot/lib/functions.sh
logfile=$setuproot/log/setup.log

if [ -r $config ]; then
    source $config
else
    echo "ERROR: Unable to read $config"
    exit 1
fi

if [ -r $funcs ]; then
    source $funcs
else
    echo "ERROR: Unable to read $funcs"
    exit 1
fi



update_kmod() {
    echo -n "Checking for a module for the running kernel (`uname -r`)..."
    # We don't need to run depmod here, the RPMs do it in %post
    /sbin/modprobe -nq openafs 2>/dev/null
    if [ $? != 0 ]; then
	echo "not found"
	echo "You do not have a module for the currently running kernel."
	echo -n "Would you like to build one now? (y/n) "
	read answer
	case $answer in 
	    y|Y)
		/opt/mit-openafs-setup/bin/build-module
		;;
	    n|N)
		;;
	    *)
		echo "Invalid answer, assuming 'n'"
		;;
	esac
    else
	echo "found"
    fi
    echo -n "Would you like to build a module for another kernel version? (y/n)"
    read answer
    case $answer in 
	y|Y)
	    echo "Enter the kernel version, including any suffixes"
	    echo -n "such as 'smp' or 'hugemem': "
	    read kv
	    /opt/mit-openafs-setup/bin/build-module -k $kv
	    ;;
	n|N)
	    return
	    ;;
	*)
	    echo "Invalid answer, assuming 'n'"
	    return
	    ;;
    esac
}    

update_configs() {
    cfgfiles="CellAlias \
              SuidCells"
    for c in $cfgfiles; do
	update_afs_config $c
    done
    cp $cfgdest/SuidCells $cfgdest/SuidCells.dist
    touch $cfgdest/SuidCells.local
}

install_init_script() {
    echo "OpenAFS includes an init script, which runs at boot time, and"
    echo "attempts to ensure that you have a suitable AFS kernel module."
    echo "If you choose not to have this script run at boot time, and you"
    echo "upgrade your kernel, you will need to re-run this setup script"
    echo "in order to fetch a new module."
    echo -n "Run script at boot time? (y/n) "
    read answer
    case $answer in 
	y|Y)
	    /sbin/chkconfig --levels 2345 mit-openafs-check-kmod on
	    ;;
	n|N)
	    ;;
	*)
	    echo "Invalid answer, assuming 'n'"
	    ;;
    esac
}

install() {
    echo "Installing packages from yum:"
    yum -y install openafs openafs-client openafs-kernel-source openafs-krb5
    # Yum returns 0 no matter what happens, so we get to do it the stupid way
    rpm -q openafs openafs-client openafs-kernel-source openafs-krb5
    if [ $? != 0 ]; then
	echo "Failed to install one or more packages.  Try again later."
	errorout
    fi
    
# Do the config files here, in case building the kernel module fails, and the user restarts it
    echo "Updating AFS config files..."
    update_configs
    set_cell_name 

    echo -n "Checking for a module for the running kernel..."
# We don't need to run depmod here, the RPMs do it in %post
    /sbin/modprobe -nq openafs 2>/dev/null
    if [ $? != 0 ]; then
	echo "not found"
	echo "I could not find a pre-compiled openafs module for your kernel."
	echo "Attempting to run build script..."
	/opt/mit-openafs-setup/bin/build-module
	if [ $? != 0 ]; then
	    echo "The build script failed for some reason.  AFS is installed,"
	    echo "but will not be usable until you build and install a kernel"
	    echo "module.  You can do that be re-running this script at a later time."
	fi
    else
	echo "found"
    fi

    install_init_script
    
    cat <<EOF

OpenAFS is now installed.  The OpenAFS service will start
automatically upon reboot, or you may start it manually with the
following command (as root):
   
    # /sbin/service openafs-client start

Note that this will not work if for some reason the kernel module
failed to build and you were not able to installed pre-built module.

If you ever upgrade your kernel, you will need a new OpenAFS kernel
module.  The best way to get the module is to run this setup script
again and follow the prompts.  However, we can also install a service
which will attempt to build a new copy of a kernel module if a
suitable one is not available.

EOF
    
    echo 
}


###################################################################
# Body




case `id` in
"uid=0("*)
  ;;
*)
  echo "You are not root.  This script must be run as root."
  errorout
  ;;
esac

if [ ! -x $curl ]; then
    echo "ERROR: $curl is not executable"
    errorout
fi

if [ -f /etc/athena/version ]; then
  echo "This script is not meant to be run on Athena machines."
  errorout
fi

if [ ! -f /etc/redhat-release ]; then
  echo "/etc/redhat-release doesn't exist.  Not continuing."
  errorout
fi

mkdir $tmpspace > /dev/null 2>&1
if [ $? != 0 ]; then
  echo "Failed to create temporary space at $tmpspace."
  errorout
fi

if [ -f $logfile ]; then
    mv -f $logfile $logfile.old
fi

echo "Beginning mit-openafs-setup at "`date` > $logfile
getosname
echo "osname=$osname" >> $logfile
if [ "$osname" == "unknown" ]; then
    echo "This installer will only work on Red Hat Enterprise Linux"
    echo "or Fedora Core.  Exiting..."
    errorout
fi

iafsver=`rpm -q --qf "%{VERSION}" openafs`
if [ $? != 0 ]; then
    install
    exit 0
fi


echo "What would you like to do?"

while true; do
    echo "1) Install a kernel module for an updated kernel"
    echo "2) Update OpenAFS configuration files"
    echo "3) Quit"
    echo
    echo -n "Enter a number: "
    read choice
    case $choice in
	1) 
	    update_kmod
	    echo -e "\nWhat would you like to do now?"
	    ;;
	2) 
	    update_configs
	    echo "Note that you will need to restart OpenAFS for these changes"
	    echo "to take effect."
	    echo -e "\nWhat would you like to do now?"
	    ;;
	3)
	    break
	    ;;
	*)
	    echo "Incorrect entry ($choice); try again"
	    echo "Please choose one of the following:"
	    ;;
    esac
done

if [ -d "$tmpspace" ]; then
    rm -rf $tmpspace
fi

exit 0    

    
