#!/bin/sh
#
# |-----------------------------------------------------------|
# | Copyright (c) 1991, 1990 MIPS Computer Systems, Inc.      |
# | All Rights Reserved                                       |
# |-----------------------------------------------------------|
# |          Restricted Rights Legend                         |
# | Use, duplication, or disclosure by the Government is      |
# | subject to restrictions as set forth in                   |
# | subparagraph (c)(1)(ii) of the Rights in Technical        |
# | Data and Computer Software Clause of DFARS 252.227-7013.  |
# |         MIPS Computer Systems, Inc.                       |
# |         950 DeGuigne Avenue                               |
# |         Sunnyvale, California 94088-3650, USA             |
# |-----------------------------------------------------------|
#
# $Header: idbuild,v 1.3.2.1 92/06/24 13:34:41 marker Exp $
# v4.51 RISC/OS version of the AT&T idbuild utility.
#
# This is a shell script replacement for the AT&T binary
# executable. The syntax has been modified as follows:
#
#	idbuild [-s <suffix>]
#
# where <suffix> is the system name suffix to be used.
# Running this script will make /unix.rxxxx_<suffix>
# where rxxxx is the appropriate model number (e.g. r2400).
# For example, 'idbuild pnw' would build /unix.r2400_pnw.
# If no argument is given, then the default argument is 'sys'.
# That is, the default system to be built will be
# /unix.rxxxx_sys. If either kernel.rxxxx_<suffix> or
# sysgen.rxxxx_<suffix> exist, then it will be created from
# the config files of the currently running system.
#

#
#	MODIFICATION HISTORY
#	10/25/90	CChan	Initial Coding
#	12/07/90	TDhar	Trap interrupts
#	12/07/90	TDhar	Do not move the new unix in '/' directory or
#				link it to /unix now, it may mess up system 
#				image, it will be done in idreboot.
#	12/14/90	TDhar	If idbuild is called after removing a driver
#				it fails because in bootarea_i directory the
#				file master_suffix_Makdepend file exists and
#				Makefile checks the existence of all include
#				files from that list. So move that file to a
#				temporary file and touch master.d/sysgen.suffix
#				and master.d/kernel.suffix file and call 'make'
#				then bootarea_i/master_suffix_Makedepend will
#				will be created from scratch, if the build goes
#				go ok then remove the temporary file otherwise
#				move it back in master_suffix_Makedepend file.
#	12/21/90	TDhar   For 6000 series, call make as:
#				"make UNICES_1= UNICES_2=$newunix
#	01/22/91	TDhar	Redirect output of 'make' to /tmp/make.log

# Setup some global variables
masterd=/usr/src/uts/mips/master.d
root=/
idbin=/etc/conf/bin
mips=/usr/src/uts/mips

# Do a uname -t to find out if it is 6000 series machine or not. If so
# bootarea will be /usr/src/uts/mips/bootarea_ii
UNIX_2=0
model=`uname -t`
case "$model"
in
	RC62*)  boot=/usr/src/uts/mips/bootarea_ii
		UNIX_2=1
		suffix=local;;
	m2000-6|m2000-8)
	 	boot=/usr/src/uts/mips/bootarea_i
		suffix=ijc_local;;
	*) 	boot=/usr/src/uts/mips/bootarea_i
		suffix=local;;
esac

# Setup default values
sflg=0
#suffix='sys'

# Parse the command line
while getopts s: option
do
	case "$option"
	in
		s) sflg=1
		   suffix=$OPTARG;;
		*) echo 'USAGE: idbuild  [-s <suffix>]'
		   exit 1;;
	esac
done

# Build the corresponding kernel and sysgen filenames
stdunix=`$idbin/idfindsys -u`
newunix=`echo $stdunix | sed "s/_.*\$/_$suffix/"`
newsysgen=`echo $newunix | sed 's/unix/sysgen/'`
newkernel=`echo $newunix | sed 's/unix/kernel/'`
newmkdpnd=`echo $newunix | sed 's/unix\./master_/'`
newmkdpnd=${newmkdpnd}_Makedepend

# If either kernel.rxxxx_<suffix> or sysgen.rxxxx_<suffix> do not exist,
# then create them from the config files of the currently running 
# system. If -s option not specified, then /unix.rxxxx_sys will be
# created.
trap '' 1 2 3 9 15		# not to be disturbed
if [ ! -f $masterd/$newsysgen -o ! -f $masterd/$newkernel ]
then
	$idbin/idmakesys -s $suffix
	if [ $? -ne 0 ]
	then
		echo "idbuild aborted. Could not access/create $newsysgen"
		echo "and/or $newkernel. idmakesys error code is $?"
		exit 1
	fi
fi

# Now make the system
cd $mips 
if [ -f ${boot}/${newmkdpnd} ]
then
	mv  ${boot}/${newmkdpnd} ${boot}/mkdpnd.tmp
fi
touch ${masterd}/${newsysgen}
touch ${masterd}/${newkernel}
BUILDTYPE=reconfig
export BUILDTYPE
echo "\n\tThe ${newunix} Operating System will now be rebuilt\n"
echo "\n\tThis will take some time. Please wait.....\n"
# For 6000 series we have to define UNICES_1= UNICES_2=$newunix
(
if [ $UNIX_2 = 0 ]
then
	make UNICES_1=$newunix UNICES_2=
else
	make UNICES_1= UNICES_2=$newunix
fi ) > /tmp/make.log 2>&1
if [ $? -ne 0 ]
then
	echo 'System build was unsuccessful\n'
	echo "Please check /tmp/make.log file for the possible reason(s)\n"
	if [ -f ${boot}/mkdpnd.tmp ]
	then
		mv ${boot}/mkdpnd.tmp ${boot}/$newmkdpnd
	fi
	exit 1
else
	# We should not move the new unix in '/' directory or should not
	# link it to /unix, leave it up to 'idreboot', but save the name
	# of the newly built unix in /etc/.new_unix file, so that idreboot 
	# knows what unix to link with /unix.

	#mv $newunix /$newunix
	# Can possibly get error if not enough disk space
	#if [ $? -ne 0 ]
	#then
	#	echo "Not enough disk space in / for new system"
	#	exit 1
	#fi
	#	ln /$newunix /unix

	# The following is needed for 'sysadm softwaremgmt'.
	if [ -f ${boot}/mkdpnd.tmp ]
	then
		rm ${boot}/mkdpnd.tmp ${boot}/$newmkdpnd
	fi
	echo "${newunix}" > /etc/.new_unix
	echo "\n\t${newunix} has been rebuilt"
fi
