#!/bin/sh
#
# Post install script for use with install.gridpp.ac.uk
#
# Andrew McNab <mcnab@hep.man.ac.uk> 8 May 2002
# Steve  Traylen <s.m.traylen@rl.ac.uk> 13th May 2002
# Owen Maroney <o.maroney@bristol.ac.uk> 28th June 2002
# Andrew McNab <mcnab@hep.man.ac.uk> 6th September 2002
# Steve Traylen <s.m.traylen@rl.ac.uk> 11th November 2002
# Anybody else?

LOGFILE=/tmp/gpp-install.log
(
################## CURRENT VERSION ##############################
VERSION=v1_4_7_1
EDG_RELEASE=v1_4_7


################### LOG FILE #####################################

echo 'All the following output will also go to '$LOGFILE
echo -n '.' ; sleep 2 ; echo -n ' .' ; sleep 2 ; echo -e ' .\n' ; sleep 2

################### SERVICES #####################################

echo -e 'Turning of some services that are not required.\n'


  /sbin/chkconfig inet off
  /sbin/service inet stop
  /sbin/chkconfig identd off
  /sbin/service identd stop
  /sbin/chkconfig lpd off
  /sbin/service lpd stop

  # Stop some services that need to be upgraded
  # before they should be used

  /sbin/service portmap stop
  /sbin/service nfslock stop
  /sbin/service httpd   stop



################## INSTALL WGET ####################################
rpm -qa | grep wget-1.5.3-6 > /tmp/test-wget.$$
if [ ! -s /tmp/test-wget.$$ ] ; then 
  echo -e 'Installing wget.'
   
lynx -source http://www.gridpp.ac.uk/gpp-install/rpms/wget-1.5.3-6.i386.rpm  > /tmp/wget-1.5.3-6.i386.rpm
      rpm -Uvh /tmp/wget-1.5.3-6.i386.rpm 2> /dev/null

  
  if [ $? != 0 ] ; then
     echo 'Died installing wget'
     exit 1
  fi
fi
rm /tmp/test-wget.$$


################## POPULATE LOCAL GPP-INSTALL DIRECTORY #############

echo -e 'Populating /opt/gpp-install with installation files.\n'
 
  rm -rf /opt/gpp-install
  mkdir /opt/gpp-install
  cd /opt/gpp-install
  wget --reject='*?history'  --no-parent -nd \
       -r -l 0 http://www.gridpp.ac.uk/gpp-install/$VERSION/
  wget --reject='*?history'  --no-parent -nd \
       -r -l 0 http://www.gridpp.ac.uk/gpp-install/rpms/
  if [ ! -f install-rpms.txt ] ; then
    echo 'Could not download the rpms'
  fi


echo -e 'Populating /opt/gpp-install with installation files.\n'
 
  
  rm -rf /opt/gpp-images
  mkdir /opt/gpp-images
  cd /opt/gpp-images
  wget --no-parent -nd -r -l 0 http://www.gridpp.ac.uk/gpp-install/images/
  
################## REMOVE GPP-INSTALL IN CASE WE ARE UPGRADING ######

 rpm -e gpp-install 2> /dev/null

################## INSTALL EXTRA RPMS THAT ARE NEEDED ###############

echo -e 'Installing some rpms required by the installation'

  for RPM in `cat /opt/gpp-install/install-rpms.txt` ; do 
    STUB=`echo $RPM | sed 's/^\(.*\)\.i386\.rpm$/\1/'`
    rpm -qa | grep $STUB > /tmp/rpm-test.$$
    if [ ! -s /tmp/rpm-test.$$ ] ; then
      rpm -Uvh /opt/gpp-install/$RPM 2> /dev/null
    fi
    rm /tmp/rpm-test.$$
  done

#################### NTP PROTOCOL #################################

grep gridpp.ac.uk /etc/ntp.conf > /tmp/test-ntp.$$

if [ ! -s /tmp/test-ntp.$$ ] ; then
   echo -e 'Setting up network time protocol.\n'

   
     /sbin/service xntpd stop
     /usr/sbin/ntpdate  ntp2a.gridpp.ac.uk ntp2b.gridpp.ac.uk \
                   ntp2c.gridpp.ac.uk ntp2d.gridpp.ac.uk

     if [ ! -d /etc/ntp ] ; then
        mkdir /etc/ntp
     fi

     echo 'server ntp2a.gridpp.ac.uk' > /etc/ntp.conf
     echo 'server ntp2b.gridpp.ac.uk' >> /etc/ntp.conf
     echo 'server ntp2c.gridpp.ac.uk' >> /etc/ntp.conf
     echo 'server ntp2d.gridpp.ac.uk' >> /etc/ntp.conf
     echo 'driftfile /etc/ntp/xntpd.drift' >> /etc/ntp.conf

     /sbin/service xntpd start
     /sbin/chkconfig xntpd on

  
fi
rm /tmp/test-ntp.$$

##################  SENDMAIL  ################################

grep DAEMON=no /etc/sysconfig/sendmail > /tmp/test1.$$
if [ -s /tmp/test-sendmail.$$ ] ; then 
   echo -e  'Stopping sendmail from listening\n'

   
      cp /etc/sysconfig/sendmail /etc/sysconfig/sendmail.saved
      cat /etc/sysconfig/sendmail.saved | \
                sed 's/yes/no/' > /etc/sysconfig/sendmail
      /sbin/service sendmail restart
   
fi
rm /tmp/test1.$$

################## EDG-RELEASE ################################

echo -n 'Generating a list of available releases in chronological order'
echo -n '.' ; sleep 2 ; echo -n ' .' ; sleep 2 ; echo -e ' .\n'

/usr/bin/gpp-fetch-edg-release.sh -l



echo -e 'Checking out release '$EDG_RELEASE' from Marianne'

  cd /tmp
  /usr/bin/gpp-fetch-edg-release.sh -o /tmp -r $EDG_RELEASE


##################### Build RPM Repository ############################

echo -e 'Creating an RPM repository of the the EDG release'
echo -e 'in /opt/local/linux/6.2/RPMS.'
echo -e 'This will take a long time the first time.'
echo -e 'Go and have lunch.\n'
echo -n '.' ; sleep 2 ; echo -n ' .' ; sleep 2 ; echo -e ' .\n' ; sleep 2

   if [  ! -d /opt/local/linux/6.2/RPMS ]
   then 
   mkdir -p /opt/local/linux/6.2/RPMS
   fi

   mv -f /etc/updaterep.conf /etc/updaterep.conf.saved
   cp -f /tmp/edg-release/updaterep/updaterep.conf /etc/updaterep.conf

   /usr/sbin/updaterep 


################## Copy in the RPM lists. ######################

echo -e 'Copying in the rpm lists.\n'

   if [ -d /opt/local/linux/6.2/rpmcfg ] ; then
     cp -r /opt/local/linux/6.2/rpmcfg \
                /opt/local/linux/6.2/rpmcfg-`date +%y%m%d%k%M`
   fi
   mkdir -p /opt/local/linux/6.2/rpmcfg/
   cp /tmp/edg-release/rpmlist/* /opt/local/linux/6.2/rpmcfg
   cp /opt/gpp-install/LCFG-rpm /opt/local/linux/6.2/rpmcfg


################## COPY IN THE MACHINE PROFILES ################
echo -e 'Copying in the machine profiles.\n'

  if [ -d /var/obj/conf/profile/source ] ; then
      cp -r /var/obj/conf/profile/source \
                /var/obj/conf/profile/source-`date +%y%m%d%k%M`
  fi 
  mkdir -p /var/obj/conf/profile/source
  cp edg-release/source/* /var/obj/conf/profile/source

################## INSTALL ALL THE RPMS ########################

echo -e 'Starting to Install all the RPMS'

 
# We install glibc first as everyhting is happier if we
# do that it way.

rpm -qa | grep  glibc-2.1.3-22 > /tmp/test-glibc.$$
if [ ! -s /tmp/test-glibc.$$ ] ; then 
  echo -e 'Installing glibc.'
  rpm -Uvh /opt/local/linux/6.2/RPMS/updates/glibc-2.1.3-22.i386.rpm
fi
rm /tmp/test-glibc.$$

/sbin/ldconfig
sleep 20

echo 'Installaing all the other rpms.'
echo 'Be patient with this, it can take a long time '
echo "and appear to be doing nothing."
echo -n  "." ; sleep 2 ; echo -n " ." ; sleep 2 ; echo -e " .\n" ; sleep 2

/usr/bin/gpp-testrpms.sh -i -l LCFG-rpm -r /

################## Set Up a few Demons #################################

# set various daemons to start automatically and set up their configs
echo -e "Starting up demons that we need.\n"

  /sbin/chkconfig portmap on
  /sbin/chkconfig nfs   on
  /sbin/chkconfig httpd on



################# Configure DHCP ###################################

echo -e "Configure DHCP but do not start it.\n"

  if [ ! -f /var/state/dhcp/dhcpd.leases ]
  then 
    touch /var/state/dhcp/dhcpd.leases
  fi


# Must not enable dhcpd by default since may seriously screw up sites
# already running a DHCP service elsewhere.
# /sbin/chkconfig dhcpd on

################## Web Server #######################################
echo -e "Setting up the document root for apache.\n"
 
  if [ ! -L /home/httpd/html ]
  then
    mv -f /home/httpd/html /home/httpd/html.dist
    ln -sf /var/obj/conf/profile/web /home/httpd/html
  fi

  # Edit out some modules from apache.conf since we are not installing
  # the rpms anyway.
  grep '^AddModule mod_php3.c' /etc/httpd/conf/httpd.conf > /tmp/http-testd.$$
  if [ ! -s /tmp/http-test.$$ ] ; then
    if [ -f /etc/httpd/conf/httpd.conf ] ; then 
        cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.orig
        cat /etc/httpd/conf/httpd.conf.orig | sed 's/^AddModule mod_perl.c.*$//'  \
                      | sed 's/^AddModule mod_php.c.*$//'  \
                      | sed 's/^AddModule mod_php3.c.*$//'  \
                      | sed 's/^LoadModule perl_module.*$//'  \
                      | sed 's/^LoadModule php_module.*$//'  \
                      | sed 's/^LoadModule php3_module.*$//'  > /etc/httpd/conf/httpd.conf
    fi
  fi
  rm /tmp/http-test.$$


################ And SOME PORTS TO SERVICES #########################


line=`grep 'lcfg ' /etc/services`
if  [ "X$line" = "X" ] ; then
  echo -e "Adding some ports to /etc/services.\n" 
  echo "lcfg          732/udp" >> /etc/services
fi
line=`grep 'lcfgack ' /etc/services`
if  [ "X$line" = "X" ] ; then
  echo -e "Adding some ports to /etc/services.\n" 
  echo "lcfgck        733/udp" >> /etc/services
fi
############### Edit the /etc/exports FILE. #########################

echo "Editing the /etc/exports file:"
# /etc/exports probably doesnt exist already,
if [ ! -f /etc/exports.saved ] ; then
 mv -f /etc/exports /etc/exports.saved
fi

domain=`/bin/dnsdomainname`
if [ "$domain" != "" ] ; then
 echo "/opt/local/linux *."$domain'(ro,no_root_squash)' >/etc/exports
 echo "/etc/exports set to: "
 cat /etc/exports
 echo '(you may want to restrict that further /etc/exports)'
fi

####################CREATE THE NFS ROOT ##############################

echo "Building a mini root distribution."

# Build a mini root distribution in 
# /opt/local/linux/installroot/6.2
# using /opt/local/linux/6.2/rpmcfg
# This is the nfs root that is mounted
# during client installations.

/usr/bin/gpp-testrpms.sh -i -l installroot \
              -r /opt/local/linux/installroot/6.2 

echo "Setting the NFS Root Mount Point"

if [ ! -L /ir62 ]
then 
   ln -sf opt/local/linux/installroot/6.2 /ir62
fi

############### DELETE SOME REDUNSANT FILE ####################

echo "Delete some files that are never used"
for FILE in client.h lcfgsrv1 site-cfg.h.cern site.h.legnaro testbed1
do
  if [ -f /var/obj/conf/profile/source/$FILE ]
  then 
      rm /var/obj/conf/profile/source/$FILE
  fi
done

for FILE in boot-server.h client core.latest.h core.versions.h lcfg-client.h \
            lcfg-client.latest.h lcfg-common.h lcfgsrv1 nfs-server.h openssh.h \
            profile-server.h rh62-release.h rh62-updates.h \
            server testbed1 
do
   if [ -f /opt/local/linux/6.2/rpmcfg/$FILE ]
   then
       rm /opt/local/linux/6.2/rpmcfg/$FILE 
   fi
done

################## SET UP LILO ################################

echo "Set up /etc/lilo.conf with the new kernel"
# determine kernel version now installed.
KERNELVERSION=`rpm -q kernel | sed 's/kernel-'//`

# now lilo - the old kernel has gone so this must be done before reboot

# Check that there really is a file there.
KERNELFILE="/boot/vmlinuz-$KERNELVERSION"
if [ ! -f $KERNELFILE ]
then
   echo "Could not find kernel image so aborting"
   echo "$KERNELFILE"
   exit
fi

# In case we need a ramdisk we have to make one here.
INITRDFILE=/boot/initrd-$KERNELVERSION.img
/sbin/mkinitrd $INITRDFILE $KERNELVERSION

mv -f /etc/lilo.conf /etc/lilo.conf.saved

sed "s|image=\/boot.*|image=$KERNELFILE|" /etc/lilo.conf.saved |  \
 sed "s|initrd=\/boot.*|initrd=$INITRDFILE|" \
            > /etc/lilo.conf

if [ -f /sbin/lilo ] ; then
  /sbin/lilo -v
fi
mkdir -p /opt/local/linux/6.2/RPMS/WP10

# finished!

echo ""
echo You may want to run '/sbin/lilo' to check that 
echo your machine will reboot.
echo Unless you see scary error messages above,
echo you should now do the commmand: shutdown -r now.
echo Then proceed with the manual configuration 
echo in /var/obj/conf/profile/source

)  2>&1 | tee -a $LOGFILE




