#! /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: mkutsbin.sh 1.3 90/04/11 $
#
# mkutsbin.sh:
#	Copy into tmpos all uts files needed for external binary products
#	(both install tape and exports).  Usually invoked via 
#	uts/$MACH/Makefile.skel.
#
# Usage:
# sh mkutsbin.sh tmpos machine config_type_list product_list tools IMPORTS
#
# Note that each list must be quoted to form a single argument.
#
# E.g.,
# sh mkutsbin.sh view/i386/tmpos symmetry "std mfg" "nfs" tools view/IMPORTS


TMPOS=$1		# Install directory, e.g., i386/tmpos
MACH=$2			# Machine type, e.g., "symmetry"
CONFIG_T_LIST=$3	# List of config types to install, e.g., "std mfg"
PRODUCT_LIST=$4		# List of layered product names, e.g., "unix lan nfs"
TOOLS=$5
IMPORTS=$6		# IMPORTS file or /dev/null if no imports

if [ ! -f $IMPORTS ]
then
	IMPORTS=/dev/null
fi

PATH=$TOOLS:$PATH
export PATH

cd ..			# cd to the uts directory

#########################################################################
# 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/usr/conf ]
then
	mkdir $TMPOS/usr/conf
fi

if [ ! -d $TMPOS/etc/conf ]
then
	# SVAE does not support symlinks
	`[ -f /dynix ] && echo /.bin/`ln -s ../usr/conf $TMPOS/etc/conf
fi

#########################################################################
# Clear and recreate tmpos/usr/conf/uts (the binary tree)		#
#########################################################################
rm -rf $TMPOS/usr/conf/uts

mkdir $TMPOS/usr/conf/uts
mkdir $TMPOS/usr/conf/uts/$MACH

#########################################################################
# For each product in the product list					#
#########################################################################
for PRODUCT in $PRODUCT_LIST
do

	#################################################################
	# Copy the contents of the MACH directory to the tmpos		#
	#################################################################
	if [ $PRODUCT = unix ]
	then
		#
		# The base system has files that layered products do not:
		#
		cp $MACH/Makefile.binary $TMPOS/usr/conf/uts/$MACH/Makefile.skel
		cp $MACH/newvers.sh      $TMPOS/usr/conf/uts/$MACH
		cp $MACH/vers.c          $TMPOS/usr/conf/uts/$MACH
		#
		# and the config & modules files have different names:
		#
		cp $MACH/modules         $TMPOS/usr/conf/uts/$MACH
		PRODUCT2=system
	else
		cp $MACH/$PRODUCT.mod    $TMPOS/usr/conf/uts/$MACH
		PRODUCT2=$PRODUCT
	fi

	for CONFIG_T in $CONFIG_T_LIST
	do
		cp $MACH/$PRODUCT2.$CONFIG_T $TMPOS/usr/conf/uts/$MACH
	done

	#################################################################
	# Copy the resulting ar file to the tmpos			#
	#################################################################
	for CONFIG_T in $CONFIG_T_LIST
	do
		cp $MACH.$CONFIG_T/${PRODUCT}_$CONFIG_T.a  $TMPOS/usr/conf/uts/$MACH
	done

	#################################################################
	# Copy the lint library to the tmpos				#
	#################################################################
	for CONFIG_T in $CONFIG_T_LIST
	do
		cp $MACH.$CONFIG_T/llib-l${PRODUCT}_${CONFIG_T}.ln $TMPOS/usr/conf/uts/$MACH
	done

	#################################################################
	# For the base product, copy some extra files to the tmpos.	#
	# Also install a "standard" kernel as $TMPOS/unix.		#
	#################################################################
	if [ $PRODUCT = unix ]
	then
	 cp kernel/$MACHINE/vuifile    $TMPOS/usr/conf/uts/kernel/$MACHINE
	 cp kernel/$MACHINE/asm.sed    $TMPOS/usr/conf/uts/kernel/$MACHINE
	 cp kernel/$MACHINE/genassym.c $TMPOS/usr/conf/uts/kernel/$MACHINE
	 echo kernel/debug/vlint.sed | cpio -pd $TMPOS/usr/conf/uts

	 if [ -f $MACHINE.std/unix ]
	 then
		#
		# Replace "/unix" with the most recent "standard" one, if any.
		#
		rm -f 			$TMPOS/unix
		cp $MACHINE.std/unix	$TMPOS/unix
		chmod 755		$TMPOS/unix
		chown 0			$TMPOS/unix
	 fi
	fi


done	# for PRODUCT in $PRODUCT_LIST

#########################################################################
# Copy all binary distribution source files to the tmpos (as indicated 	#
# by the contents of bfiles as generated by config)			#
#########################################################################

TMP1=/tmp/tmp1.$$
TMP2=/tmp/tmp2.$$

#
# Create a merged, unique list of all source files needed
# for binary config of this set of products.  
# 
# This is not strictly necessary as all $MACH.$CONFIG_T/bfiles 
# should be identical.
#
for CONFIG_T in $CONFIG_T_LIST
do
	cat $MACH.$CONFIG_T/bfiles
done						|
sort -u						> $TMP1

#
# Create a list of all imported files in uts.
#
grep '^	usr/src/uts/' $IMPORTS			|
sort -u 					|
sed 's/^	usr\/src\/uts\//\$\(UTS\)\//'	> $TMP2

#
# Create a list of all source files unique to this set of
# products that are needed for binary config.
# Make the pathnames relative to the current directory (UTS).
# Copy them into the TMPOS directory.
#
comm -23 $TMP1 $TMP2				|
sed 's/$(UTS)/\./g'				|
cpio -pd $TMPOS/usr/conf/uts

rm -f $TMP1 $TMP2

#########################################################################
# Strip all config files in the tmpos of their "modules" and "headers" 	#
# directives.								#
#########################################################################
#
# 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/usr/conf/uts -type f -name config -exec strip_config {} \;

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


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



#########################################################################
# Create the include file tree (if it is not already there)		#
#########################################################################
if [ ! -d $TMPOS ]
then
	mkdir $TMPOS
fi

if [ ! -d $TMPOS/usr ]
then
	mkdir $TMPOS/usr
fi

if [ ! -d $TMPOS/usr/include ]
then
	mkdir $TMPOS/usr/include
fi


#########################################################################
# Copy header files into tmpos/usr/include				#
#########################################################################
#
# First remove files in tmpos if already present.
#
(cd head; find . -type f -print) | (cd $TMPOS/usr/include; xargs rm -f)
(cd head; find . -type f -print  | cpio -pd $TMPOS/usr/include)


#########################################################################
# Set the correct owner and modes on all files and directories in the	#
# include file tree.							#
#########################################################################
umask 0
find $TMPOS/usr/include -type d -print | xargs chmod 775
chmod 775 $TMPOS/usr/include
find $TMPOS/usr/include -type f -print | xargs chmod 444
find $TMPOS/usr/include -print | xargs chgrp 2
find $TMPOS/usr/include -print | xargs chown 2
