########################################################################
# BooNE post-install script
#
# FL LTS3.0
#
# Chris Green <greenc@fnal.gov>
# 2004/01/12
#
########################################################################

shopt -s extglob

pwd=`/bin/pwd`

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

echo "${WORKGROUP} workgroup post-install: post-RPMS setup"

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 boyd/root \
-a colombo/root \
-a ettab/root \
-a gcooper/root \
-a greenc \
-a jallen/root \
-a jason \
-a jason/root \
-a jhedden/root \
-a ling/root \
-a ormes \
-a schmitz/root \
-a sether/root \
-a stefansk \
> /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
########################################################################

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

########################################################################
# Fix /etc/krb.conf
echo -n "  Fixing /etc/krb.conf: "
perl -wn -i.bak -e 'm%^\s*FNAL\.GOV\s*$% or print;' /etc/krb.conf > /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
########################################################################

if [ "$WORKGROUP" = "BooNE" ]; then
########################################################################
# 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
########################################################################
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
########################################################################

########################################################################
# 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
  ######################################################################
  
  ######################################################################
  # Now use yum to update everything we can lay our hands on
  echo -n "2 ... "
  yum -y update > /dev/null 2>&1
  ######################################################################

  ######################################################################
  # See if we picked up firefox and thunderbird or we have to do it
  # manually:
  if ! rpm -q firefox >/dev/null 2>&1; then
    echo -n " firefox & thunderbird ... "
    yum -y install firefox thunderbird > /dev/null 2>&1
  fi
  ######################################################################

  check_status 0

  ######################################################################
  # Configure sudo permission for bb to run 3ware diagnostics
  echo -n "Configure sudo permission for Big Brother: "
  ./scripts/config-sudoers
  check_status $?
fi
########################################################################

########################################################################
# Configure desktop to be KDE by default
echo -n "  Setting default session to KDE: "
/usr/bin/perl -wn -e 'm%^\s*DISPLAYMANAGER% or print;
END {print "DISPLAYMANAGER=\"KDE\"\n";}' /etc/sysconfig/desktop > /tmp/after.rpms.sh.$$ && \
mv /tmp/after.rpms.sh.$$ /etc/sysconfig/desktop
check_status $?
########################################################################

########################################################################
# 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 >/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 
install -m 644 ./configfiles/kdmrc /etc/X11/xdm/
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

. /etc/sysconfig/network
if [ "$WORKGROUP" = "BooNE" ]; then
  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
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
  ######################################################################

#
# 2006-11-14 jason harrington <jason@fnal.gov>
# config-kernel removed as per discussion with joe boyd
#  ######################################################################
#  # Install config-kernel.init
#  echo -n "    Configuring kernel module installation: "
#  install ./configfiles/config-kernel.init /etc/rc.d/init.d/ >/dev/null 2>&1
#  chkconfig --add config-kernel.init >/dev/null 2>&1
#  check_status
#  ######################################################################

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

echo "${WORKGROUP} workgroup post-install done"
