:
#	@(#) idbuild 25.2 92/07/28 
#
#	Copyright (C) 1988-1992 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
#	      UNIX is a registered trademark of AT&T
#		Portions Copyright 1976-1989 AT&T
#		      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.
#
#	Normally, this script deletes various intermediate files (such as
#	driver space.o's) before exiting; the -p (preserve) flag says not
#	to.  Nonetheless, old intermediate files are always deleted.
#
#	This script calls /etc/conf/bin/idvidi, /etc/conf/bin/idconfig,
#	/etc/conf/bin/idscsi, /etc/conf/bin/idmkunix, /etc/conf/bin/idmkenv.
#

Preserve=NO
[ "X$1" = "X-p" ] && Preserve=YES && shift

if [ -z "$DEVSYS" ]
then DEVSYS="/."
fi

cd `dirname $0`
ORIGROOT="$ROOT"
if [ -z "$ROOT" ]
then ROOT=`cd ../../..; pwd`
fi
if [ "X$ROOT" = "X/" ]
then ROOT=/.
fi

echo "\n\tThe UNIX Operating System will now be rebuilt."
echo "\tThis will take a few minutes.  Please wait.\n"
echo "\tRoot for this system build is ${ROOT}"
if [ "X$DEVSYS" != "X/." ]
then
echo "\tDevsys for this system build is ${DEVSYS}"
fi

ID=$ROOT/etc/conf
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 sdevice.new sfsys mfsys xdevmap direct ifile config.h
rm -f fsconf.c conf.c vector.c fsconf.o conf.o vector.o
rm -f ../pack.d/*/space.o
rm -f ../pack.d/*/stubs.o

awk '{print $1}' mdevice | sort -u | grep -v "*" | \
while read dev
do	cat ../sdevice.d/$dev
done > 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 $IDLCK
	[ "X$Preserve" = XNO ] && rm -f direct ifile
	exit 1
fi
$ID/bin/idconfig -d sdevice.new -r $ID
if [ $? != 0 ]
then
	rm -rf $IDLCK
	[ "X$Preserve" = XNO ] && rm -f direct ifile
	exit 1
fi
$ID/bin/idscsi $ID/cf.d/mscsi
if [ $? != 0 ]
then
	rm -rf $IDLCK
	[ "X$Preserve" = XNO ] && rm -f direct ifile
	exit 1
fi

# CPPFLAGS for Industry Standard or MicroChannel Architecture
ARCHFLAGS="-DARCH=AT+EISA+MC+I486"

if [ "X$ROOT" = "X/." ]
then
	# AT386 or MC386 may be needed by 3rd party space.cs
	if cat /dev/mcapos 1>/dev/null 2>/dev/null
	then	ARCHFLAGS="`echo $ARCHFLAGS | sed s/AT+EISA+//` -DMC386"
	else	ARCHFLAGS="`echo $ARCHFLAGS | sed s/+MC//` -DAT386"
	fi
fi

# CPPFLAGS supplied by 3.2v2 idmkunix, may be needed by 3rd party space.cs
OLDFLAGS="-Di386 -DiAPX386 -DINKERNEL"

# CPPFLAGS used by the 3.2v4 sys header files
NEWFLAGS="-DM_I386 -DM_UNIX -D_INKERNEL -D_NO_PROTOTYPE"

[ -x $ID/pack.d/scodb/bin/mkdef ] &&
	grep Y $ID/sdevice.d/scodb >/dev/null 2>/dev/null &&
		$ID/pack.d/scodb/bin/mkdef -cf -s -r $ROOT

$ID/bin/idmkunix \
	-r $ROOT -d $DEVSYS -I$ROOT/usr/include $ARCHFLAGS $OLDFLAGS $NEWFLAGS \
	3<&- 4<&- 5<&- 6<&- 7<&- 8<&- 9<&-

if [ $? != 0 ]
then
	rm -rf $IDLCK
	[ "X$Preserve" = XNO ] && {
		rm -f direct ifile
		rm -f fsconf.o conf.o vector.o
		rm -f ../pack.d/*/space.o
		rm -f ../pack.d/*/stubs.o
	}
	exit 1
fi

if grep Y $ID/sdevice.d/mskdb >/dev/null 2>/dev/null
then	
	[ -x $ID/pack.d/mskdb/putsym ] &&
		$ID/pack.d/mskdb/putsym unix $ID/pack.d/mskdb/stun.def

elif grep Y $ID/sdevice.d/scodb >/dev/null 2>/dev/null
then	
	[ -x $ID/pack.d/scodb/putsym ] &&
		$ID/pack.d/scodb/putsym unix $ID/pack.d/scodb/stun.def
	[ -x $ID/pack.d/scodb/bin/ldsy ] &&
		$ID/pack.d/scodb/bin/ldsy unix
fi

if [ "X$Preserve" = XNO ]
then
	rm -f direct ifile
	rm -f fsconf.o conf.o vector.o
	rm -f ../pack.d/*/space.o
	rm -f ../pack.d/*/stubs.o
fi
 
mv -f sdevice.new sdevice

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

echo "\n\tThe UNIX Kernel has been rebuilt.\n"

#
# call idmkenv here - no more rc script references to it.
# only call if build is in actual root; but remain compatible
# with a product whose installation set ROOT=/ to avoid idmkenv 
#
[ "X$ROOT" = "X/." -a "X$ORIGROOT" != "X/" ] && $ID/bin/idmkenv $*
exit 0
