#! /bin/sh
# @(#)addlockd.sh	1.2 91/02/05
#
# Addlockd.sh is a bourne shell script to uncompress, tar 
# and install files for three architecures of the lock daemon
# and associated kernel files. The architectures and machine
# reference are where the lockd and kernel files can be installed:
#
#   sun3 - 3/50, 3/60, 3/75, 3/110,3/140, 3/160, 3/180, 3/260, 3/280
#   sun3x - 3/80, 3/460, 3/470, 3/480
#   sun4 (for sun4 and sun4c architectures) - 
#	 4/110, 4/260, 4/280, SPARCsystem 330, SPARCserver 390, 
#	   SPARCserver 470, SPARCserver 490, SPARCstation 1, SPARCstation 2
#
# The kernel files are:
#
#   kern_descrip.o, klm_lockmgr.o, ufs_lockf.o
#
# and the lockd is: rpc.lockd
#
# The intent is to have a new lockd installed that is consistent with
# SUN's CTE level 6 lock daemon.
#
#

fatalerror ()   # handle fatal errors
{
    echo "" ; echo "*** FATAL ERROR: $@" 1>&2 ; exit 1
}

DIR=`pwd`
HOST=`hostname`
ARCH=`arch -k`
SYSDIR=/usr/sys/$ARCH/OBJ
CONFDIR=/usr/sys/$ARCH/conf
LOCKDDIR=/usr/etc
PRODUCT="Lock Daemon Patch"
VERSION="- version CTE level 100075-06"
COPYRIGHT="Copyright 1991 Sun Microsystems, Inc."


if [ `whoami` != root ]
then
    fatalerror "You must be root to run $0."
fi

printheader()  # print installation header
{
    clear ; cat <<ENDHEADER
#############################################################
#
#         $PRODUCT $VERSION $PATCH
#
#         $COPYRIGHT
#
#############################################################
ENDHEADER
}

Proceed()
{
        echo
        echo -n "DO you wish to continue? [y/n] "
        while read choice
        do
          case "$choice" in
                [Yy]* ) break;;
                [Nn]* ) exit 1;;
                * ) echo -n "Please answer y or n:  ";;
          esac
        done
}

printheader

case "$ARCH" in
	sun3) 		
		LOCKD=lockd3
		rm -f lockd3x.tar lockd4.tar
		;;
	sun3x) 
		LOCKD=lockd3x
		rm -f lockd3.tar lockd4.tar
		;;
	sun4|sun4c)
		LOCKD=lockd4
		rm -f lockd3.tar lockd3x.tar
		;;
	*)  fatalerror "$0: cannot install lockd for architecture $ARCH. ***" ;;
esac

if [ ! -f $LOCKD.tar ]
then
    fatalerror "The compressed tar file $LOCKD.tar is missing." 
fi

FILE=`file $LOCKD.tar | awk '{print $2}'`

if [ $FILE = "compressed" ]
then
	echo
	echo "Uncompressing the $LOCKD.tar file..."
	mv $LOCKD.tar $LOCKD.tar.Z
	uncompress $LOCKD.tar
	if [ $? != 0 ] ; then
		fatalerror "Uncompresing $LOCKD.tar failed."
	fi
fi

echo
echo "Unpacking the $LOCKD.tar file..."
tar xvf $LOCKD.tar
if [ $? != 0 ] ; then
	fatalerror "Extracting files from $LOCKD.tar failed."
fi

if [ -d $SYSDIR ]
then
	echo
	echo "Saving kernel files..."
	if [ -f $SYSDIR/kern_descrip.o ] && [ ! -f $SYSDIR/kern_descrip.org ]
	then
		mv $SYSDIR/kern_descrip.o $SYSDIR/kern_descrip.org
		chmod 444 $SYSDIR/kern_descrip.org
	fi
	if [ -f $SYSDIR/klm_lockmgr.o ]  && [ ! -f $SYSDIR/klm_lockmgr.org ]
	then
		mv $SYSDIR/klm_lockmgr.o $SYSDIR/klm_lockmgr.org
		chmod 444 $SYSDIR/klm_lockmgr.org
	fi
	if [ -f $SYSDIR/ufs_lockf.o ] && [ ! -f $SYSDIR/ufs_lockf.org ]
	then
		mv $SYSDIR/ufs_lockf.o $SYSDIR/ufs_lockf.org
		chmod 444 $SYSDIR/ufs_lockf.org
	fi
	echo
	echo "Installing kernel file kern_descrip.o in $SYSDIR..."
	cp kern_descrip.o $SYSDIR
	if [ $? != 0 ]
	then
		fatalerror "Installing kern_descrip.o failed."
	fi
	echo
	echo "Installing kernel file klm_lockmgr.o in $SYSDIR..."
	cp klm_lockmgr.o $SYSDIR
	if [ $? != 0 ]
	then
		fatalerror "Installing klm_lockmgr.o failed."
	fi
	echo
	echo "Installing kernel file ufs_lockf.o in $SYSDIR..."
	cp ufs_lockf.o $SYSDIR
	if [ $? != 0 ]
	then
		fatalerror "Installing ufs_lockf.o failed."
	fi
else
	fatalerror "The kernel directory $SYSDIR does not exist."
fi

if [ ! -f $LOCKDDIR/rpc.lockd.org ]
then
    mv $LOCKDDIR/rpc.lockd $LOCKDDIR/rpc.lockd.org
fi
echo
echo "Installing lockd in $LOCKDDIR..."
cp rpc.lockd /usr/etc
if [ $? != 0 ]
then
	fatalerror "Installing lockd failed."
fi
CONFIG=`strings /vmunix | grep "SunOS Release" | sed -e "s/^.*(//" | sed -e "s/).*//"`
echo
echo "Your system name is $HOST and is a $ARCH machine that is running"
echo "a kernel which was built from the configuration file $CONFIG."
echo
echo -n "Do you wish to use the configuration file $CONFIG to rebuild the kernel? [Y|N] "
while read choice
do
	case "$choice" in
		[Yy]* ) break ;;
		[Nn]* ) echo -n "Enter the name of configuration file: " 
			while read ans
			do
			  if [ -f $CONFDIR/$ans ]
			  then
				CONFIG=$ans
				break
			  else
				echo -n "Please enter the correct file name: "
			  fi
			done ;;
		* ) echo -n "Please answer y or n:  " ;;
	esac
	break
done

cd $CONFDIR
echo
echo "Configuring a new lockd kernel..."
/etc/config $CONFIG
cd $CONFDIR/../$CONFIG
echo
echo "Rebuilding a new lockd kernel..."
make 
if [ $? != 0 ]
then
	fatalerror "Making a new kernel failed."
fi
if [ ! -f /vmunix.org ]
then
    mv /vmunix /vmunix.org
else
    echo
    echo "WARNING: unable to save the original kernel file to the name vmunix.org."
    Proceed
fi
echo
echo "Installing the new kernel in / (root) directory."
cp $CONFDIR/../$CONFIG/vmunix /
if [ $? != 0 ]
then
	fatalerror "Installing the new lockd kernel (vmunix) failed."
fi
sync; sync; sync
echo
echo "*** Installation of the LOCK DAEMON is complete. ***"
echo 
echo "RUN /etc/fastboot FOR THE CHANGES TO WORK."
echo
