########################################################################
# BooNE post-install script
#
# FRHL 7.3
#
# Chris Green <greenc@fnal.gov>
# 2002/08/29
#
########################################################################

shopt -s extglob

echo "BooNE Workgroup post-install: post-RPMS setup"

# Make sure we're in the right place:
if [ -f /etc/workgroup ]; then
  WORKGROUP=`cat /etc/workgroup`
fi
pwd=`/bin/pwd`

if [ "$pwd" = "/etc/$WORKGROUP/scripts" -o -x after.rpms.sh ]; then
  cd ..
elif [ -x scripts/after.rpms.sh ]; then
  :
elif [ -d "/etc/$WORKGROUP" ]; then
  cd /etc/$WORKGROUP
else
  echo "Emergency: could not find /etc/workgroup for post-install script!" 1>&2
  exit 2
fi

# Setup success / failure messages a la system boot
 . /etc/rc.d/init.d/functions

check_status() {
  status=$?
  if [ -n "$1" ] && [[ $1 == +([0-9]) ]]; then
    status=$1
  fi
  if [ $status -eq 0 ]; then
    echo_success
    echo
  else
    echo_failure
    echo
  fi
}

warning() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo -n $1
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  echo
  return 1
}

########################################################################
# Install our AFS cache-clearing utility
echo -n "  Installing AFS cache-clearing utility: "
install -m 755 ./configfiles/AFSClearCache /etc/rc.d/init.d/
chkconfig --add AFSClearCache
check_status 0

########################################################################
# Enable other services
echo -n "  Enabling other network services: "
./scripts/config-services
check_status
########################################################################

########################################################################
# Configure info
echo -n "  Configuring the FNAL info utility: "
./scripts/config-info -i ./configfiles/info-message > /dev/null 2>&1
check_status
########################################################################

########################################################################
# Add admins to root's .k5login
echo -n "  Adding admin entries to root's .k5login: "
./scripts/k5login-admin -u root -a greenc -a ormes > /dev/null 2>&1
check_status 0
########################################################################

########################################################################
# Add admins to products' .k5login
echo -n "  Adding admin entries to products' .k5login: "
./scripts/k5login-admin -u products -a greenc > /dev/null 2>&1
check_status 0
########################################################################

########################################################################
# Ensure that we can log in to accounts with a .k5login if we are on a
# local account.
echo -n "  Adding local access to accounts with a .k5login file: "
perl -wap -i.bak -e 's/^(account.*)default=bad(.*)$/$1default=ignore$2/' \
/etc/pam.d/system-auth >/dev/null 2>&1
check_status 0
########################################################################

########################################################################
# Fix /etc/shells
echo -n "  Fixing /etc/shells: "
./scripts/config-shells > /dev/null 2>&1
check_status
########################################################################

########################################################################
# Open up hosts.allow
echo -n "  Configuring /etc/hosts.* files: "
install --backup=numbered -m 644 ./configfiles/hosts.allow /etc/
check_status
########################################################################

########################################################################
# Add X11 forwarding to ssh
echo -n "  Configuring ssh to allow X-forwarding: "
./scripts/config-ssh
check_status
########################################################################

########################################################################
# Configure flpr
echo -n "  Configuring flpr: "
install -m 644 ./configfiles/flp.defaults /usr/local/etc/
check_status
########################################################################

########################################################################
# Check for products group
echo -n "  Checking for existence of products group (4525): "
/usr/sbin/groupadd -g 4525 products > /dev/null 2>&1 
check_status 0
########################################################################

########################################################################
# Configure OpenOffice (if installed)
echo -n "  Configuring OpenOffice: "
./scripts/config-openoffice >/dev/null 2>&1
status=$?
if [ $status -eq 1 ]; then
  warning SKIPPED
else
  check_status $status
fi
########################################################################

########################################################################
# Fix products manpage directories
echo -n "  Fixing manpage directory permissions under ~products: "
[[ -d ~products ]] && \
( cd ~products ; find man catman -type d \
  -exec chmod go+rx \{\} \; ) > /dev/null 2>&1
check_status 0
########################################################################

########################################################################
# Wincenter configuration.
#
# Licensing terms verified to allow redistribution (but IANAL). See
# Citrix_License.txt in config/
echo -n "  Configuring wincenter application: "
# Start script
install -m 755 configfiles/wincenter /usr/bin/
# Configuration
bzcat configfiles/ICAClient_config.tar.bz2 | ( cd /usr/lib ; tar xf - )
check_status 0
########################################################################

########################################################################
# Things we can do if we're on the net
if ping -q -c 1 -w 2 linux.fnal.gov > /dev/null 2>&1; then
  echo -n "  Found a network: Updating system with yum: "

  ######################################################################
  # Use yum to bring itself up to date.
  echo -n "1 "
  yum -y update yum yum-conf > /dev/null 2>&1
  ######################################################################
  
  ######################################################################
  # Use yum to remove autorpm
  echo -n "2 "
  yum -y erase autorpm > /dev/null 2>&1
  ######################################################################
  
  ######################################################################
  # Now use yum to update everything we can lay our hands on
  echo -n "3 "
  yum -y update > /dev/null 2>&1
  ######################################################################
  check_status 0
elif rpm -q autorpm > /dev/null 2>&1; then
  echo -n "Removing autorpm if installed: "
  # Remove autorpm even if we're not on the net: yum will run next time
  # we're connected
  rpm -e autorpm autorpm-conf > /dev/null 2>&1
  check_status 0
fi
########################################################################

########################################################################
# Configure desktop to be KDE by default
echo -n "  Setting default session to KDE: "
echo 'DESKTOP="KDE"' > /etc/sysconfig/desktop
check_status 0
########################################################################

########################################################################
# Configure /etc/inittab
echo -n "  Configuring /etc/inittab: "
/usr/bin/perl -wap -i.bak -e \
's%^x:5:respawn:.*$%x:5:respawn:/etc/X11/prefdm -nodaemon%' /etc/inittab
check_status
echo -n "  Setting best value for initdefault in /etc/inittab: "
current_level=`perl -wane '/^id:(\d):initdefault:/ && print $1, "\n";' /etc/inittab`
if grep -e 'Driver[ 	][ 	]*"\{0,1\}nvidia' \
 /etc/X11/XF86Config-4 >/dev/null 2>&1; then
  initdefault=3
else
  initdefault=$current_level
fi
/usr/bin/perl -wap -i.bak -e \
's%^id:\d:initdefault:%id:'"$initdefault"':initdefault:%' /etc/inittab
warning $initdefault
########################################################################

########################################################################
# Configuring X-login screen
echo -n "  Configuring X login screen: "
install -m 755 -d /etc/X11/xdm/pixmaps/login_pics
install -m 644 ./configfiles/login_pics/*.* /etc/X11/xdm/pixmaps/login_pics/
./scripts/config-xlogin /etc/X11/xdm/Xsetup_0 
check_status 0
########################################################################

########################################################################
# Set "normal" collation for (eg) ls
echo -n "  Setting POSIX collation order for ls: "
./scripts/config-i18n
status=$?
if [ $status -eq 2 ]; then
  warning SKIPPED
else
  check_status $status
fi
########################################################################

########################################################################
# Network-related configuration

echo -n "  Installing apmcontinue script: "
install -m 755 ./configfiles/apmcontinue /etc/sysconfig/apm-scripts/
check_status 0

. /etc/sysconfig/network
network_files=`ls /etc/sysconfig/network-scripts/ifcfg-{wlan,eth}* 2>/dev/null`
have_dhcp=0
[[ -n "$network_files" ]] && grep -i dhcp $network_files >/dev/null 2>&1 && \
have_dhcp=1
if [ $have_dhcp -eq 1 ]; then
  echo -n "  Configuring DHCP settings: "
  install -m 644 ./configfiles/pump.conf /etc/
  if [ -z "$DHCPCDARGS" ]; then
    # Not defined: just add on the end
    echo 'DHCPCDARGS="-t 5"' >> /etc/sysconfig/network
  else
    # Need to combine
    if echo "$DHCPCDARGS" | grep -e '-t[ 	][ 	]*[0-9]' \
    >/dev/null 2>&1; then
      # Someone has already set a timeout: leave it there
      :
    else
      # Add the timeout argument to the others
      echo "DHCPCDARGS=\"$DHCPCDARGS -t 5\"" >> /etc/sysconfig/network
    fi
  fi
  check_status 0
else
  echo -n "  No DHCP configurations found: "
  warning SKIPPED
fi

########################################################################
# Things to do if we're a FNAL-local installation
echo -n "  FNAL-local post-install section: "
if [ "$NISDOMAIN" = "boone-nis.fnal.gov" ]; then
  echo

  ######################################################################
  # Configure automounting
  ######################################################################
  echo -n "    Configuring NIS automount tables: "
  ./scripts/automount-NIS-config > /dev/null 2>&1
  check_status

  ######################################################################
  # Configure root's email forwarding
  echo -n "    Forwarding root's mail to boone-computing@fnal.gov: "
  echo '\boone-computing@fnal.gov' > ~root/.forward
  check_status 0
  ######################################################################

  ######################################################################
  # Configure Enstore for machines with static addresses
  echo -n "    Configuring Enstore: "
  if grep -i dhcp /etc/sysconfig/network-scripts/ifcfg-{eth,wlan}* \
  >/dev/null 2>&1; then
    warning SKIPPED
  else
    ./scripts/check-enstore >/dev/null 2>&1
    if [ $? -ne 0 ]; then
      echo "Enstore permission required for $HOSTNAME" | \
      mail -s "Enstore permission required for $HOSTNAME" \
      boone-computing@fnal.gov
      warning PENDING
    else
      check_status 0
    fi
  fi
  ######################################################################

  echo -n "  FNAL-local post-install section: "
  check_status 0
else
  warning SKIPPED
fi
########################################################################

echo "BooNE Workgroup post-install done"
