#!/bin/sh
#ident	"@(#)cls4:incl-master/build	1.1"
###############################################################################
#
# C++ source for the C++ Language System, Release 3.0.  This product
# is a new release of the original cfront developed in the computer
# science research center of AT&T Bell Laboratories.
#
# Copyright (c) 1991 AT&T and UNIX System Laboratories, Inc.
# Copyright (c) 1984, 1989, 1990 AT&T.  All Rights Reserved.
#
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
#	
# Laboratories, Inc.  The copyright notice above does not evidence
# any actual or intended publication of such source code.
#
###############################################################################

test $# -eq 6 || { echo "Usage: build os stdincl symboliclinkflag verboseflag targetdir list-of-headers"; exit; }

OS=$1
STDINCL=$2
if test "$OS" = "sun"
then
	if test -f $STDINCL/macros.h
	then
		OS=sunos3
	else
		if test -f $STDINCL/sys/stdtypes.h
		then
			OS=sunos4.1
		else
			OS=sunos4
		fi
	fi
fi
pyr_flag=0
if test "$OS" = "pyramid"
then
	OS=svr3
	pyr_flag=1
fi
SYMBOLICLINK=$3
VERBOSE=$4
TARGET=$5
TEMPTARGET=$5.temp
ROOT=`pwd`

PATH=$ROOT:$PATH
export PATH

# Calculate the full path name of $TARGET.
#
cd $ROOT
if test ! -d $TARGET
then
	mkdir $TARGET
	cd $TARGET
	CURTARGET=`pwd`
	cd $ROOT
	rmdir $TARGET
else
	cd $TARGET
	CURTARGET=`pwd`
	cd $ROOT
fi

# Figure out where the proto-headers are.
#
cd $ROOT
if test -n "$PROTOHEADERS"
then
	echo 
	echo "Using proto-headers: $PROTOHEADERS"
	echo
	PROTOS=$PROTOHEADERS
else
	PROTOS=$ROOT/proto-headers
fi

# Make a new empty temporary target directory.
#
cd $ROOT
rm -rf $TEMPTARGET
mkdir $TEMPTARGET

# Make the link to /usr/include, if that's how we're doing it.
#
cd $ROOT
cd $TEMPTARGET
if test $SYMBOLICLINK -eq 1
then
	ln -s $STDINCL cc
fi

# Duplicate the directory structure.
#
if test $VERBOSE -eq 1
then
	echo
	echo "Creating directories:"
fi
cd $ROOT
cd $PROTOS
alldirs=`find . -type d -print`
if test -z "$6"
then
	allheaders=`find . -name \*.h -print`
else
	allheaders=$6
fi
cd $ROOT
cd $TEMPTARGET
for i in $alldirs
do
	if test $i != "."
	then
		if test $VERBOSE -eq 1
		then
			echo $i
		fi
		mkdir $i
	fi
done

# Now build the headers.
#
echo
echo "Building the headers for your machine:"

# If there's no /usr/include/stddef.h, build one.
# -- Changed this to build stddef.h even if this
# machine already has one, because some machines
# define the types in stddef.h wrong.
#
#stdcat -s stddef.h
#if test $? -ne 0
#then
	stddef > stddef.h
	nochange "$CURTARGET" ./stddef.h
#fi

# If curses.h has been ported to this OS, get it.
#
if test -f $PROTOS/curses.$OS
then
	cp $PROTOS/curses.$OS curses.h
	nochange "$CURTARGET" ./curses.h
fi

# For sunos4, copy the special version of setjmp.h
#
if test "$OS" = sunos4 -o "$OS" = sunos4.1
then
	cp $PROTOS/setjmp.sunos4 setjmp.h
	nochange "$CURTARGET" ./setjmp.h
fi

# Now transform the proto-headers into headers for this OS.
#
for h in $allheaders
do
	grep "#usedby" $PROTOS/$h >/dev/null
	if test $? -ne 0
	then
		echo
		echo "error: $h is missing a #usedby directive"
		echo
	else
		grep "#usedby.*[ 	]all\$" $PROTOS/$h >/dev/null
		G0=$?
		grep "#usedby.*[ 	]all[ 	]" $PROTOS/$h >/dev/null
		G1=$?
		grep "#usedby.*[ 	]$OS[ 	]" $PROTOS/$h >/dev/null
		G2=$?
		grep "#usedby.*[ 	]$OS\$" $PROTOS/$h >/dev/null
		G3=$?
		if test $G0 -eq 0 -o $G1 -eq 0 -o $G2 -eq 0 -o $G3 -eq 0
		then
			# This OS wants this header file.

			sh $ROOT/transform $OS $SYMBOLICLINK $PROTOS/$h > $h
			if test $h != "./dial.h" -a $h != "./arpa/nameser.h"
			then
				nochange "$CURTARGET" $h
			fi
		fi
	fi
done

# pyramid is just like svr3, except that it also has a values.h file
if [ "$pyr_flag" = 1 ]
then
	sh $ROOT/transform pyramid $SYMBOLICLINK $PROTOS/values.h > values.h
	nochange "$CURTARGET" values.h
fi

###########################################################
# Operating System Dependent Adjustments
#
# TO ANYONE PORTING THE HEADERS TO A NEW OPERATING SYSTEM: 
# THIS IS WHERE YOU SHOULD ADD ANY OF YOUR OWN OPERATING 
# SYSTEM DEPENDENT ADJUSTMENTS.
#
if test "$OS" = "svr2" -o "$OS" = "svr3" -o "$OS" = "hpux" -o "$OS" = "uts"
then
	# fix the faulty comment in dial.h
	sed -e 's/device used$/device used \*\//' -e 's/device used to make$/device used to make \*\//' dial.h >$$.tmp
	mv $$.tmp dial.h
	nochange "$CURTARGET" ./dial.h
fi
if test "$OS" = "sunos4" -o "$OS" = "sunos4.1"
then
	# fix the faulty comment in arpa/nameser.h
	sed -e 's/\/\* 0x3 reserved \*\/$/\*\/\/\* 0x3 reserved \*\//' arpa/nameser.h >$$.tmp
	mv $$.tmp arpa/nameser.h
	nochange "$CURTARGET" ./arpa/nameser.h
fi
if test "$OS" = "sunos3" -o "$OS" = "sunos4" -o "$OS" = "sunos4.1"
then
	# change the name of struct core to struct Bsdcore in sys/core.h
	sed -e 's/struct core/struct Bsdcore/' $STDINCL/sys/core.h >sys/core.h
	nochange "$CURTARGET" ./sys/core.h
fi
if test "$OS" = "sunos3"
then
	# fix the prototype of itol in macros.h
	sed -e 's/itol()/itol(int)/' $STDINCL/macros.h >macros.h
	nochange "$CURTARGET" ./macros.h
fi
if test "$OS" = "sgi"
then
	# remove all of the "#pragma" statements from math.h, and all lgammas
	sed -e '/^#pragma/d' -e '/lgamma/d' math.h >$$.tmp
	mv $$.tmp math.h
	nochange "$CURTARGET" ./math.h
	# remove the extra declaration of function "stat()"
	sed -e 's/^extern int	stat/\/\/&/' sys/stat.h >$$.tmp
	mv $$.tmp sys/stat.h
	nochange "$CURTARGET" ./sys/stat.h
fi
if test "$OS" = "sunos4.1"
then
	# remove the superfluous declaration of wait() in sys/wait.h
	sed -e '/^pid_t	wait(\/\* int \*loc \*\/);$/d' sys/wait.h >$$.tmp
	mv $$.tmp sys/wait.h
	nochange "$CURTARGET" ./sys/wait.h
fi
if test "$OS" = "svr4"
then
	# remove the mblen define in stdlib.h
	sed -e '/^#define mblen(s, n)/d' stdlib.h >$$.tmp
	mv $$.tmp stdlib.h
	nochange "$CURTARGET" ./stdlib.h
	# remove the C style static functions in sys/stat.h
	sed -e 's/^#if !defined(_STYPES) && !defined(_KERNEL)$/& \&\& !defined(__cplusplus)/' sys/stat.h >$$.tmp
	mv $$.tmp sys/stat.h
	nochange "$CURTARGET" ./sys/stat.h
fi
#
# END OF OPERATING SYSTEM DEPENDENT ADJUSTMENTS
###########################################################


# Remove all empty directories.
#
for d in `find . -type d -print`
do
	rmdir $d >/dev/null 2>&1
done

# The common headers can just be copied.
#
cd $ROOT
if test -d const-headers
then
	cp const-headers/* $TEMPTARGET
fi

# Save the current contents of $TARGET.
#
cd $ROOT
if test -d $TARGET
then
	if test -d $TARGET.OLD
	then
		if test $VERBOSE -eq 1
		then
		        echo
		        echo "Removing directory structure $TARGET.OLD..."
		fi
	        /bin/rm -fr $TARGET.OLD
	fi
	if test $VERBOSE -eq 1
	then
		echo
		echo "Saving current $TARGET directory structure as $TARGET.OLD."
	fi
	mv $TARGET $TARGET.OLD

	# Remove all empty directories.
	#
	cd $TARGET.OLD
	for d in `find . -type d -print`
	do
		rmdir $d >/dev/null 2>&1
	done
	rm -f cc
fi

# Move the stuff we built to its final resting place.
#
cd $ROOT
mv $TEMPTARGET $TARGET

# All done.
#
if test $VERBOSE -eq 1
then
	echo
	echo "All done.  The following directories now contain the following stuff:"
	echo
	echo "	$TARGET -- the newest C++ headers for this machine"
	echo "	$TARGET.OLD -- (if present) the old versions of any headers which"
	echo "			have changed"
	echo
fi


