#!/bin/sh

PERL=$SI_CONFIG_DIR/bin/perl
patches=/opt/patches
patchroot=/tmp/patchroot
patchdir=${patchroot}${patches}
mkdir -p ${patchroot}`dirname $patches`

#
# This system's filesystems are mounted at this point.
# The $patches filesystem needs to be mounted; the code that follows
# is site specific and can easily be replaced by your own.
#
getpaths ()
{
    for f
    do
	echo $f
	dir=`dirname $f`
	case $dir in
	/) ;;
	/*) getpaths $dir;;
	esac
    done
}

echo "Trying to mount $patchdir"

r=${newroot}
newroot=${patchroot}
for mp in `getpaths $patches|sort -u`
do
    if readvfstab $mp
    then
	mountfs ro
    fi
done
newroot=$r

# This item moved to the start of the finish script.
#echo "Switching filesystems to fast"
#$SI_CONFIG_DIR/bin/fastfs -a fast
if [ ! -d ${patchdir} ]
then
    echo Failed to mount patch directory
    echo "Type control-D to continue"
    /sbin/sulogin > /dev/console < /dev/console 2>&1
fi
echo "running patch script"
status=1
if [ -x ${patchdir}/fastpatch ]
then
    echo ""
    echo "WARNING: Using experimental fastpatch program (if you have $PERL)"
    echo "if this fails, we'll fallback to do-patch"
    echo ""
    $PERL ${patchdir}/fastpatch -c -R ${newroot} \
	`${patchdir}/do-patch -R ${newroot} -c -d 2>/dev/null`
    status=$?
    ${patchdir}/fix-modes -R ${newroot}
fi
[ $status = 0 ] || ${patchdir}/do-patch -R ${newroot} -n -c
#echo "Switching filesystems to slow"
#$SI_CONFIG_DIR/bin/fastfs -a slow
sync
