#!/sbin/sh
#
# Created my Andrey Ryzhov aryzhov@japan.ml.com
#
# Two main goals:
#   - make it fast
#   - make it from chroot'ed environment
#
# How it works:
#
# Does some additional mounts:
#     /a/tmp on swap (tmpfs)
#     /a/tmp/install_config on $confighost:$ConfigDir (nfs)
#     /a/cdrom on $MediaDir
# Extract patches into /a/tmp directory (tmpfs!)
# Then make a new mnttab in /a/etc by cutting /a,
# chroot and use Casper's fastpatch
#
#
# We suspect SI_CONFIG_DIR is under /tmp
# and MediaDir is under /cdrom
#
PATCHFILE=${SI_CONFIG_DIR}/Patches/2.5.1.tgz
PATCHDIR=/tmp/2.5.1
ZCAT=${SI_CONFIG_DIR}/bin/zcat
ConfigDir=`/usr/bin/cat /etc/mnttab|grep ${SI_CONFIG_DIR}|awk '{print $1}'`
LocalCDir=`/usr/bin/cat /etc/mnttab|grep ${SI_CONFIG_DIR}|awk '{print $2}'`
LocalMDir=/cdrom
MediaDir=`/usr/bin/cat  /etc/mnttab|grep ${LocalMDir} |awk '{print $1}'`
#
# Cutting /a does not work for nfs and tmpfs - this mount just gets lost...
# so mount them once again, copy all necessary info from /tmp to /a/tmp,
# and make new /a/etc/mnttab for chroot'ed environment
#
/usr/bin/cp ${slash_a}/etc/mnttab ${slash_a}/etc/mnttab.slash_a
/sbin/umount ${slash_a}/tmp >/dev/null 2>&1
/sbin/mount -f tmpfs swap ${slash_a}/tmp
(cd /tmp;find . -mount -print|cpio -pcdm ${slash_a}/tmp) >/dev/null 2>&1
mkdir ${slash_a}/${LocalCDir} > /dev/null 2>&1
/sbin/mount ${ConfigDir} ${slash_a}${LocalCDir}
mkdir ${slash_a}${LocalMDir}        > /dev/null 2>&1
/sbin/mount ${MediaDir} ${slash_a}${LocalMDir}
/usr/bin/cat /etc/mnttab |\
  /usr/bin/awk '$2 ~ /\/a/ { print $0 }' | /usr/bin/sed "s?/a?/?" |\
  /usr/bin/sed "s?//?/?" > ${slash_a}/etc/mnttab

#
# Install the patches
# First, apply the patches from CD
#
echo Installing patches from Solaris media
chroot ${slash_a} \
  ${SI_CONFIG_DIR}/Tools/perl \
    ${SI_CONFIG_DIR}/Tools/fastpatch.pl \
      -p ${LocalMDir}/Patches `/bin/ls ${LocalMDir}/Patches/`

echo Extracting patches from ${PATCHFILE}
if [ ! -f ${PATCHFILE} ] ; then
  echo WARNING: ${PATCHFILE} does not exist
  echo WARNING: Recommended patch cluster not applied to the system
else
  cd ${slash_a}/tmp
  ${ZCAT} ${PATCHFILE} | tar xpf -
  ln -s ${slash_a}${PATCHDIR} ${PATCHDIR}
  echo ===== Patching started  at `date` =====

  chroot ${slash_a} \
    ${SI_CONFIG_DIR}/Tools/perl \
      ${SI_CONFIG_DIR}/Tools/fastpatch.pl \
	-p ${PATCHDIR} `/bin/ls ${PATCHDIR}`

  echo ===== Patching finished at `date` =====
fi

#
# And put the things back
#

/sbin/umount ${slash_a}${LocalCDir}
/sbin/umount ${slash_a}/tmp
/sbin/umount ${slash_a}${LocalMDir}
/usr/bin/cp ${slash_a}/etc/mnttab.slash_a ${slash_a}/etc/mnttab



