#! /bin/sh
# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.
#

# $Header: mkbinary.sh 1.21 90/02/15 $
#

TMPOS=$1
MACH=$2
CONFIG_T=$3
UNIX=$4
TOOLS=$5
PATH=$TOOLS:$PATH
export PATH

#
#	For all configurations:
#
#########################################################################
# Create the binary tree (if it is not already there)			#
#########################################################################
if [ ! -d $TMPOS ]
then
	mkdir $TMPOS
fi
if [ ! -d $TMPOS/etc ]
then
	mkdir $TMPOS/etc
fi
if [ ! -d $TMPOS/etc/conf ]
then
	if [ ! -d $TMPOS/usr/conf ]
	then
		mkdir $TMPOS/usr/conf
	fi
	# SVAE does not support symlinks
	`[ -f /dynix ] && echo /.bin/`ln -s ../usr/conf $TMPOS/etc/conf
fi
if [ ! -d $TMPOS/etc/conf/uts ]
then
	mkdir $TMPOS/etc/conf/uts
fi
(cd ..; find . -type d -print | grep -v symmetry.mfg | cpio -pd $TMPOS/etc/conf/uts)

if [ ${CONFIG_T} = std ]
then
	#
	#	Making standard configuration tmpos
	#
	#################################################################
	# Clear tmpos/etc/conf/uts (the binary tree)			#
	#################################################################
	find $TMPOS/etc/conf/uts -type f -print | xargs rm -f
	rm -rf $TMPOS/etc/conf/uts/$MACH.*
	mkdir $TMPOS/etc/conf/uts/$MACH.$CONFIG_T

	#################################################################
	# Copy the contents of the MACH directory to the tmpos		#
	#################################################################
	cp ../$MACH/Makefile.binary  $TMPOS/etc/conf/uts/$MACH/Makefile.skel
	cp ../$MACH/modules          $TMPOS/etc/conf/uts/$MACH
	cp ../$MACH/newvers.sh       $TMPOS/etc/conf/uts/$MACH
	cp ../$MACH/vers.c           $TMPOS/etc/conf/uts/$MACH

	#################################################################
	# Copy the vuifile, asm.sed, and genassym.c files to the tmpos	#
	#################################################################
	cp ../kernel/$MACHINE/vuifile \
		$TMPOS/etc/conf/uts/kernel/$MACHINE/vuifile
	cp ../kernel/$MACHINE/asm.sed \
		$TMPOS/etc/conf/uts/kernel/$MACHINE/asm.sed
	cp ../kernel/$MACHINE/genassym.c \
		$TMPOS/etc/conf/uts/kernel/$MACHINE/genassym.c

	#################################################################
	# Copy all binary distribution source files to the tmpos (as	#
	# indicated by the contents of bfiles).  This includes all of	#
	# the module configuration files, spaces files, and stubs files.#
	#################################################################
	(cd ..; cat $MACH.$CONFIG_T/bfiles |  sed 's/$(UTS)/\./g' | \
						 cpio -pd $TMPOS/etc/conf/uts)

	#################################################################
	# Strip the "modules" and "headers" directives from the		#
	# exported module configuration files.				#
	#################################################################
	#
	# create the sed script
	#
	rm -f strip.sed
	cat  >strip.sed <<- '@@eof@@'
		/^module[s ].*[)}]/d
		/^module[s ]/,/[)}][ ]*$/d
		/^header[s ].*[)}]/d
		/^header[s ]/,/[)}][ ]*$/d
	@@eof@@

	#
	# create the script to run sed (needed only for cleanliness)
	#
	rm -f strip_config
	cat >strip_config <<- '@@eof@@'
		sed -f strip.sed $1 >$1.strip
		rm -f $1
		mv $1.strip $1
	@@eof@@
	chmod +x strip_config

	#
	# run strip_config on every config file
	#
	find $TMPOS/etc/conf/uts -type f -name config -exec strip_config {} \;

	#
	# clean up
	#
	rm -f 	strip.sed strip_config

	#
	#	For all configurations:
	#
	#################################################################
	# Copy the system configuration file, resulting kernel archive	#
	# file, and lint library file to the tmpos.			#
	#################################################################
	cp ../$MACH/system.$CONFIG_T $TMPOS/etc/conf/uts/$MACH
	cp unix_$CONFIG_T.a          $TMPOS/etc/conf/uts/$MACH/unix_$CONFIG_T.a
	cp llib-lunix_$CONFIG_T.ln   $TMPOS/etc/conf/uts/$MACH/llib-lunix_$CONFIG_T.ln

fi
#
# set the correct owner and modes on all files and directories in the
# binary tree.
#
umask 0
find $TMPOS/etc/conf/uts -type d -print | xargs chmod 775
find $TMPOS/etc/conf/uts -type f -print | xargs chmod 644
find $TMPOS/etc/conf/uts -print | xargs chgrp 3
find $TMPOS/etc/conf/uts -print | xargs chown 0
