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

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

# cd to our workgroup area:
if [ -f /etc/workgroup ]; then
  WORKGROUP=`cat /etc/workgroup`
  cd /etc/$WORKGROUP
else
  echo "Emergency: could not find /etc/workgroup for post-install script!" 1>&2
  exit 2
fi

########################################################################
# Check portmap is enabled (not done for laptop installs)
echo "  Enabling portmap"
chkconfig portmap on
########################################################################

########################################################################
# Enable other services
echo "  Enabling other network services"
./scripts/enable-services
########################################################################

########################################################################
# Configure info
echo "  Configuring the FNAL info utility"
./scripts/info-config -i ./configfiles/info-message
########################################################################

########################################################################
# Add admins to root's .k5login
echo "  Adding admin entries to root's .k5login"
./scripts/k5login-admin -u root -a greenc -a ormes
########################################################################

########################################################################
# Configure xemacs
echo "  Configuring xemacs"
./scripts/xemacs-config -f
# Newer postgresql-libs doesn't provide libpq.so, just libpq.so.2
[[ -f /usr/lib/libpq.so ]] || ( cd /usr/lib ; ln -s libpq.so.2 libpq.so )
########################################################################

########################################################################
# Fix /etc/shells
echo "  Fixing /etc/shells"
./scripts/fix-shells
########################################################################

########################################################################
# Open up hosts.allow
echo "  Configuring /etc/hosts.* files"
install -b -m 644 ./configfiles/hosts.allow /etc/
########################################################################

########################################################################
# Add X11 forwarding to ssh
echo "  Configuring ssh to allow X-forwarding"
perl -wn -i.bak -e \
'if (! /ForwardX11/ or /^\s*#/) {print}; END {print "ForwardX11 yes\n"}' \
/etc/ssh/ssh_config
########################################################################

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

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

########################################################################
# 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 "  Found a network: Updating system with yum ..."

  ######################################################################
  # Use yum to bring itself up to date.
  echo "    Updating yum"
  yum -y update yum yum-conf
  ######################################################################
  
  ######################################################################
  # Use yum to remove autorpm
  echo "    Removing autorpm"
  yum -y erase autorpm
  ######################################################################
  
  ######################################################################
  # Now use yum to update everything we can lay our hands on
  echo "    Using yum to update the system"
  yum -y update
  ######################################################################
elif rpm -q autorpm; then
  # 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
fi
########################################################################

########################################################################
# Configure desktop to be KDE by default
echo "  Setting default session to KDE"
echo 'DESKTOP="KDE"' > /etc/sysconfig/desktop
########################################################################

########################################################################
# Configure /etc/inittab
echo "  Configuring /etc/inittab"
/usr/bin/perl -wap -i.bak -e 's%^x:5:respawn:.*$%x:5:respawn:/etc/X11/prefdm -nodaemon%' /etc/inittab
########################################################################

########################################################################
# Configuring X-login screen
echo "  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 
########################################################################

. /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 "  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
fi

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

  ######################################################################
  # Configure automounting
  ######################################################################
  ./scripts/automount-NIS-config

  ######################################################################
  # Configure root's email forwarding
  echo "    Forwarding root's mail to boone-computing@fnal.gov"
  ./scripts/root-forward
  ######################################################################

  ######################################################################
  # Link to OpenOffice under AFS
  if [ ! -d /usr/local/OpenOffice -o -h /usr/local/OpenOffice ]; then
    echo "    Installing link to AFS intallation of OpenOffice"
    rm -f /usr/local/OpenOffice
    ln -s /afs/fnal.gov/files/code/e898/code/13/OpenOffice /usr/local/OpenOffice
  fi
  ######################################################################

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

  echo "  FNAL-local post-install section done"
fi
########################################################################

echo "BooNE Workgroup post-install done"
