#!/bin/sh
######################################################################
#
# $XFree86: xc/programs/Xserver/hw/xfree86/etc/svr4_rem_pch,v 3.3 1996/12/23 06:47:24 dawes Exp $
#
# Back out the patch to kernel that prevents losing IOPL on signals.
#
# Version 1.0 - 11/18/92
#	initial version - dwex@goblin.org, dwex@aib.com
#
# $XConsortium: svr4_rem_pch /main/3 1996/02/21 17:48:40 kaleb $
#

N=`basename $0`

#
# Make sure we're running on SVR4!
#
uname -r | grep '4' > /dev/null
if [ "$?" != "0" ]
then
	echo "$N: This only applies to SVR4!"
	exit 1
fi

#
# Next make sure we are running as root
#
id | grep 'uid=0' > /dev/null
if [ "$?" != "0" ]
then
	echo "$N: Must be run as root!"
	exit 1
fi

#
# OK.  Now undo the patch.
#
PACK=/etc/conf/pack.d/kernel
BKUP=${PACK}/.xfree86
if [ ! -d ${BKUP} ]
then
	echo "$N: Backup directory ${BKUP} does not exist!"
	exit 1
fi
OWD=${PWD}
cd ${BKUP}

./patcher -c ../os.o
if [ "$?" != "0" ]
then
	echo "$N: Patch not applied. Aborting"
	cd ${OWD}
	exit 1
fi

LOC=`./patcher -u ../os.o ./Nos.o`
if [ "$?" != "0" ]
then
	echo "$N: Patch removal failed!!!"
	cd ${OWD}
	exit 1
fi
LOC1=`cmp -l ../os.o ./Nos.o | awk '{print $1; exit}'`
if [ "$LOC" != "$LOC1" ]
then
	echo "$N: Patch sanity check failed!!!"
	cd ${OWD}
	exit 1
fi
echo "$N: Patch successfully removed.  Installing unpatched module."
mv ./Nos.o ../os.o

#
# OK.  Patch is removed.  Now do an idbuild
#
echo "$N: Building the new kernel."
/etc/conf/bin/idbuild 2>/tmp/idb.$$
if [ "$?" != "0" ]
then
	echo "$N: Kernel build failed!  Errors are in /tmp/idb.$$"
	cd ${OWD}
	exit 1
fi
rm -f /tmp/idb.$$

#
# Kernel is now rebuilt.  
#
echo "$N: Kernel successfully rebuilt."
cd ${OWD}
rm -rf ${BKUP}

#
# Kernel is now rebuilt.  Check if we should reboot now.
#
REBOOT=0
echo "$N: You must reboot before patch takes effect.  Reboot now? \c"
read RESP
case ${RESP} in
	[yY]*)
		REBOOT=1
		;;
	*)
		echo "$N: OK.  But remember to reboot later"
		;;
esac

cd ${OWD}
rm -rf ${BKUP}
#
# All done.  Reboot if necessary
#
if [ ${REBOOT} = "1" ]
then
	cd /
	sync
	/usr/sbin/shutdown -i6 -g15 -y &
fi
exit 0
######################################################################
