#	Copyright (c) 1984, 1987 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#ident  "@(#)mkunix.sh	1.6 - 87/05/12"

#	Build a kernel in a $CONF tree.
#       Note: the correct i386 config must be first in your $PATH.
#       For cross builds, it should be $ROOT/i386/bin/config.
#       /etc/config will work only if you're running UNIX V.3/386

CC=${CC:-$ROOT/bin/cc}
LD=${LD:-$ROOT/bin/ld}
STRIP=${STRIP:-$ROOT/bin/strip}
CONF=${CONF:-$ROOT/etc/atconf}
SYSTEM=${SYSTEM:-system.std}
MORECPP=${MORECPP:--DAT386}

while [ "$1" != "" ]
do
	case $1 in
	-s)	shift; SYSTEM=$1; shift;;
	-c)	shift; CONF=$1; shift;;
	*)	echo "usage:  mkunix [-c confdir] [-s system]";
		exit 1;;
	esac
done

cd $CONF; CONF=`pwd`			# expand to full name
INCRT=${INCRT:-$ROOT/usr/include}
[ -d $CONF/../sys ] && INCRT=$CONF/..	# if in development environment, use
					# development sys directory
CFLAGS="-O -I$INCRT -I$CONF/tmp -DINKERNEL $MORECPP"

# get version number from version file (start it at 1 if not already there)

VERSION=$CONF/tmp/Version
SYSSUFFIX=`expr $SYSTEM : '.*\.\(.*\)'`
if grep "^$SYSSUFFIX " $VERSION >/dev/null 2>&1
	then :
	else echo "$SYSSUFFIX 1" >>$VERSION
fi
set `grep "^$SYSSUFFIX " $VERSION`
version=$2
UNIX=$CONF/kernels/unix.$SYSSUFFIX.$version

# run config and compile conf.c

echo
echo "running config..."
config -s $SYSTEM -c $CONF    # creates config.h, conf.c, modlist
[ $? != 0 ] && { echo config failed;  exit 1; }
cd $CONF/tmp
echo "#undef VER\n#define VER \"$version\"" >>config.h
echo "compiling conf.c..."
$CC -c $CFLAGS conf.c
[ $? != 0 ] && { echo $CC conf.c failed;  exit 1; }
OFILES=tmp/conf.o

cd $CONF/modules
echo "compiling space.c and stubs.c modules..."
for i in *
do
	[ -d $i ] || continue
	cd $i
	if grep \^$i\$ $CONF/tmp/modlist >/dev/null
	then
		if [ -f space.c ]
		then
			$CC -c $CFLAGS space.c
			[ $? != 0 ] &&
				{ echo $CC `pwd`/space.c failed; exit 1; }
		fi
		rm -f stubs.o
		OFILES=$OFILES\ modules/$i/*.o
	else
		if [ -f stubs.c ]
		then
			$CC -c $CFLAGS stubs.c
			[ $? != 0 ] &&
				{ echo $CC `pwd`/stubs.c failed; exit 1; }
			OFILES="$OFILES modules/$i/stubs.o"
		fi
	fi
	cd ..
done

cd $CONF
echo "linking the kernel..."
$LD -o $UNIX vuifile $OFILES
[ $? != 0 ] && { echo "kernel link failed:   $LD -o $UNIX vuifile $OFILES"
				exit 1; }

grep -v "^$SYSSUFFIX " $VERSION >${VERSION}xxx
mv ${VERSION}xxx $VERSION
echo $SYSSUFFIX `expr $version + 1` >>$VERSION

echo "stripping the kernel..."
$STRIP -x $UNIX
echo $UNIX made.

if [ "$DEBUGGER" ]
then
	echo "running unixsyms on the kernel..."
	unixsyms $UNIX
fi

exit 0
