VERSION=612
#RELEASE=61rolling
#RELEASE=611
SECUREFTPSERVER=linux1.fnal.gov
#
CHROOT="/mnt/sysimage/"
RUN="$CHROOT/usr/sbin/chroot /mnt/sysimage/"
INSTALLIMAGE="/mnt/source/"
PATH=$PATH:/$CHROOT/bin:/$CHROOT/usr/bin:/$CHROOT/sbin:/$CHROOT/usr/sbin
if [ -s $CHROOT/tmp/upgrade.log ] ; then
   METHOD=UPGRADE
else
   METHOD=INSTALL
fi
mount | grep -q 61rolling 
if [ $? -eq 0 ] ; then
  RELEASE=61rolling
else
  mount | grep -q 612 
  if [ $? -eq 0 ] ; then
    RELEASE=612
  else
    mount | grep -q 6xtest
    if [ $? -eq 0 ] ; then
       RELEASE=6xtest
    else
       RELEASE=611
    fi
  fi
fi
echo $RELEASE > /dev/tty5
mount | grep -q nfs 
if [ $? -ne 0 ] ; then
  MEDIA=CDROM
else
  MEDIA=NFS
fi
export CHROOT
export RUN
export INSTALLIMAGE
export PATH
export METHOD
# This was put in because RedHat does not put in the nameserver entry
# It is a bug.  I fix it here for simplicity purposes.  The check for
# the name server entry is to only do it if it is not there.  This
# is mostly for the cdrom install where it does put it in and I do not
# want to overwrite it
grep -q nameserver $CHROOT/etc/resolv.conf
if [ $? -ne 0 ] ; then
   $CHROOT/bin/cp /etc/resolv.conf /$CHROOT/etc/
fi
echo "Installing Post Install RPMS" >/dev/tty5
echo "Log file in /tmp/postinstall.log" >/dev/tty5
if [ -d /$INSTALLIMAGE/DONOTEXPORT/autoinstalled ] ; then
   /$CHROOT/bin/rpm --root $CHROOT -i /$INSTALLIMAGE/DONOTEXPORT/autoinstalled/*rpm >>  $CHROOT/tmp/postinstall.log 2>&1
fi 
if [ -s $CHROOT/etc/sysconfig/network ] ; then
# if we already have installed ssh we do not want to overwrite the keys so
# we skip it here
   if  [ -s $CHROOT/etc/ssh_host_key ]; then
       echo "ssh keys already exist,  skipping ssh install" >> $CHROOT/tmp/postinstall.log 2>&1
   else
       $RUN /etc/rc.d/init.d/network restart >& /dev/null
       /$CHROOT/bin/rpm --root $CHROOT -i --force ftp://linux-rep.fnal.gov/pub/security/ssh-current.i386.rpm >> $CHROOT/tmp/postinstall.log 2>&1
#	/$CHROOT/bin/rpm --root $CHROOT -i --force ftp://$SECUREFTPSERVER/linux/$RELEASE/i386/DONOTEXPORT/ssh-current.i386.rpm >> $CHROOT/tmp/postinstall.log 2>&1
   fi
#  $RUN  rdate -s 131.225.8.120  2> /dev/null
   $RUN /sbin/chkconfig xntpd on
   /$CHROOT/bin/rpm --root $CHROOT -i --force /$INSTALLIMAGE/RedHat/RPMS/zz_xntp3_change-*.i386.rpm >> $CHROOT/tmp/postinstall.log 2>&1
fi 
$RUN /sbin/hwclock --systohc
$RUN /sbin/chkconfig --level 2345 linuxconf off
$RUN /sbin/chkconfig --list ypbind | grep -q on
if [ $? = 0 ] ; then
   $RUN /sbin/chkconfig --level 2345 portmap on
else
   $RUN /sbin/chkconfig --level 2345 portmap off
fi
$RUN /sbin/chkconfig --level 2345 lpd off
#
# Determine which workgroup we are
if [ -s $CHROOT/etc/workgroup ]; then
   WORKGROUP=`cat $CHROOT/etc/workgroup`
else
   echo "CUSTOM" > $CHROOT/etc/workgroup
   WORKGROUP=`cat  $CHROOT/etc/workgroup`
fi
echo "Fermi Linux 6.1.2 $METHOD for $WORKGROUP via $MEDIA on `$RUN /bin/date`" > /tmp/banner
grep -q "NOTICE TO USERS" $CHROOT/etc/motd
if [ $? -ne 0 ] ; then
   cat $CHROOT/etc/motd $INSTALLIMAGE/Fermi/common/configfiles/fermibanner > /tmp/fermibanner
   cp /tmp/fermibanner $CHROOT/etc/motd
fi
if [ -s $CHROOT/etc/motd.rpmsave ] ; then
   grep "Fermi " $CHROOT/etc/motd.rpmsave > /tmp/fermilinux
   cat /tmp/banner /tmp/fermilinux | uniq > /tmp/bannerlinux
   cp /tmp/bannerlinux /tmp/banner
fi
cat /tmp/banner $CHROOT/etc/motd > /tmp/tmpbanner
cp /tmp/banner $CHROOT/etc/FermiLinuxHistory
$CHROOT/bin/cp /tmp/tmpbanner $CHROOT/etc/motd
# go and copy all of the $WORKGROUP/* files from $INSTALLIMAGE which is
# either NFS or the CDROM
mkdir  $CHROOT/etc/$WORKGROUP >> /$CHROOT/tmp/postinstall.log 2>&1
mkdir  $CHROOT/etc/$WORKGROUP/RPMS >> /$CHROOT/tmp/postinstall.log 2>&1
cd $CHROOT/etc/$WORKGROUP
if [ -d /$INSTALLIMAGE/DONOTEXPORT/workgroups/$WORKGROUP/RPMS ] ; then
   $CHROOT/bin/cp /$INSTALLIMAGE/DONOTEXPORT/workgroups/$WORKGROUP/RPMS/*.rpm RPMS/ >> /$CHROOT/tmp/postinstall.log 2>&1
else
   $CHROOT/usr/bin/ncftpget -R $SECUREFTPSERVER ${CHROOT}etc/$WORKGROUP/RPMS /linux/$RELEASE/i386/DONOTEXPORT/workgroups/$WORKGROUP/RPMS/ >> $CHROOT/tmp/postinstall.log 2>&1
fi
$CHROOT/bin/cp -R /$INSTALLIMAGE/Fermi/workgroups/$WORKGROUP/* . >> /$CHROOT/tmp/postinstall.log 2>&1
cd $CHROOT
ls etc/$WORKGROUP/RPMS/*.rpm > $CHROOT/tmp/rpmfiles 2> /dev/null
if [ -s $CHROOT/tmp/rpmfiles ] ; then
   cd $CHROOT/etc/$WORKGROUP/RPMS
   /$CHROOT/bin/rpm --root $CHROOT -U --force --nodeps *.rpm  >& $CHROOT/etc/$WORKGROUP/workgroup.rpm.log
fi
cd $CHROOT
if [ -d $CHROOT/etc/$WORKGROUP/RPMSI ] ; then
   ls $CHROOT/etc/$WORKGROUP/RPMSI/*.rpm > $CHROOT/tmp/rpmfiles.i 2> /dev/null
   if [ -s $CHROOT/tmp/rpmfiles.i ] ; then
      cd $CHROOT/etc/$WORKGROUP/RPMSI
     /$CHROOT/bin/rpm --root $CHROOT -i --force --nodeps *.rpm  >> $CHROOT/etc/$WORKGROUP/workgroup.rpm.log 2>&1
   fi
fi  
$CHROOT/bin/touch $CHROOT/etc/$WORKGROUP/after.rpms.log
if [ -x $CHROOT/etc/$WORKGROUP/scripts/after.rpms.nochroot.sh ] ; then
   $CHROOT/etc/$WORKGROUP/scripts/after.rpms.nochroot.sh  >> $CHROOT/etc/$WORKGROUP/after.rpms.log 2>&1
else
   echo "No after.rpms.nochroot.sh found" >> $CHROOT/tmp/after.rpms.log 2>&1
fi
if [ -x $CHROOT/etc/$WORKGROUP/scripts/after.rpms.sh ] ; then
   ($RUN /etc/$WORKGROUP/scripts/after.rpms.sh  >> $CHROOT/etc/$WORKGROUP/after.rpms.log 2>&1 )
else
   echo "No after.rpms.sh found" >> $CHROOT/tmp/after.rpms.log 2>&1
fi
