#!/bin/ksh
#       Install Almaden Research Center Local Customizations
#
#  This script copies over the AFS client code, un-TAR's it,
# installs it, and starts up AFS.

# Define a function to handle errors
function Error
{
  print "Error in configuration process; configuration aborted" | alog -t $alogtype
  exit 1
}

# Print a message to screen and to logfile
function Message
{
  print - "[$(date +%H:%M:%S)]" $@ | alog -t $alogtype
}

# Print a message to log file only
function Log
{
  print - "[$(date +%H:%M:%S)]" $@ | alog -t $alogtype -q
}

# Enable error trap
trap Error ERR

# Progress will be logged to the bosinst alog type, which translates to
# the file /var/adm/ras/bosinstlog.  One can alog -t bosinst -o to see it.
alogtype=bosinst  

# Make sure we are running as root
if    [[ $(whoami) != root ]]
then  Message "You must be logged in as root to run this command"
      exit 1
fi

# The real work starts here
Message "afsinstall script started on $(date)"

# Insure NSORDER is set correctly.  During a bos_inst, NIM sets this to
# just local, restricting name resolution to /etc/hosts (yuch!).
# It doesn't hurt to insure this is correct for a cust operation, too.
export NSORDER=bind,local

# TFTP the AFS install script and client software from NIM server
Message "Copying AFS client software..."
tftp -o /tmp/afsinstall nim /local/lib/afs/afsinstall image
chmod 755 /tmp/afsinstall
version="$(uname -v)$(uname -r)"
tftp -o /tmp/afsclient.tar nim /local/lib/afs/afsclient-$version.tar image
Message "Copy of AFS binary and install script completed"

# Insure the clock is accurate before installing AFS, else AFS won't come up.
# NIM does do a nimclient -d in /etc/firstboot, but we need it done now.
nimclient -d
Message "Clock re-synched with NIM master."

# Install AFS
if    [[ ! -x /tmp/afsinstall ]]
then  Message "Can't find AFS install script (/tmp/afsinstall)"
      exit 1
fi
/tmp/afsinstall /tmp/afsclient.tar
rm -f /tmp/afsinstall /tmp/afsclient.tar
Message "AFS installed"

# Link /local to AFS if it does not already exist
if    [[ ! -r /local ]]
then  ln -s /afs/almaden.ibm.com/@sys/local /local
      Message "/local linked to AFS"
fi

# Wait for AFS to start up
print "Waiting for AFS initialization to complete..."
until [[ -d /afs/almaden.ibm.com ]]
do    sleep 2
done

Message "AFS configuration has been completed."
