:
#ident	"@(#)idbuild	1.3	90/10/03"
#ident "@(#)idbuild	1.2 89/09/29"
#	@(#) idbuild 1.16 89/07/21 
#
#	      UNIX is a registered trademark of AT&T
#		Portions Copyright 1976-1989 AT&T
#	Portions Copyright 1980-1989 Microsoft Corporation
#    Portions Copyright 1983-1989 The Santa Cruz Operation, Inc
#		      All Rights Reserved

#
# This script will build a UNIX Kernel using ID native mode.
# It is called by ID Install scripts that add device drivers
# to the UNIX Kernel.
#
# This script calls /etc/conf/bin/idconfig, /etc/conf/bin/idmkunix
# This script calls /etc/conf/bin/idvidi and /etc/conf/bin/idscsi.
#

if [ ! "$DEVSYS" ]
then DEVSYS="/."
fi

if [ ! "$ROOT" ]
then ROOT="/."
fi

echo "
	The UNIX Operating System will now be rebuilt.
	This will take a few minutes. Please wait.

	Root for this system build is ${ROOT}"
if [ "$DEVSYS" != "/." ]
then
	echo "
	Devsys for this system build is ${DEVSYS}"
fi

ID=$ROOT/etc/conf
IDBLD="vector.c fsconf.c conf.c direct ifile"
IDLCK="$ROOT/etc/.unix_reconf $ROOT/etc/.last_dev_add $ROOT/etc/.last_dev_del"

set +e

>$ROOT/etc/.unix_reconf
sync
 
cd $ID/cf.d
rm -f unix
cat ../sdevice.d/* > sdevice.new
cat ../sfsys.d/* > sfsys 2>/dev/null
cat ../mfsys.d/* > mfsys 2>/dev/null

$ID/bin/idvidi -r $ID
if [ $? != 0 ]
then
	rm -rf $IDBLD $IDLCK
	exit 1
fi
$ID/bin/idconfig -d sdevice.new -r $ID
if [ $? != 0 ]
then
	rm -rf $IDBLD $IDLCK
	exit 1
fi
$ID/bin/idscsi $ID/cf.d/mscsi
if [ $? != 0 ]
then
	rm -rf $IDBLD $IDLCK
	exit 1
fi

$ID/bin/idmkunix -r $ROOT -d $DEVSYS 3<&- 4<&- 5<&- 6<&- 7<&- 8<&- 9<&-
if [ $? != 0 ]
then

	rm -rf $IDBLD $IDLCK
	rm -f fsconf.o conf.o vector.o
	rm -f $ID/pack.d/*/space.o
	rm -f $ID/pack.d/*/stubs.o
	exit 1
fi

rm -rf $IDBLD
rm -f fsconf.o conf.o vector.o
rm -f $ID/pack.d/*/space.o
rm -f $ID/pack.d/*/stubs.o
 
mv sdevice.new sdevice
chmod 744 unix
chgrp bin unix

# link of unix to $ROOT/unix done at shutdown in /etc/rc0
>$ROOT/etc/.new_unix
rm -rf $IDLCK
sync
sync

echo "
	The UNIX Kernel has been rebuilt.
"
#
# call idmkenv here - no more rc script references to it.
# only call if build is in actual root. 
#
[ "$ROOT" = "/." ] && $ID/bin/idmkenv $*
exit 0
