#!/bin/sh
#$Id: mkmake,v 1.14 89/12/07 11:54:02 chrisb Exp $

#------------------------------------------------------------------------------
#	(RE)BUILD HOST-SPECIFIC MAKEFILES FOR THE GNU/960 TOOLS
#
# Gives help message if invoked without arguments.
#
# Runs off information in files on the 'admin' subdirectory:
#    HOSTS: names of supported hosts.
#    TOOLS: directories containing source code for tools.
#		Entries are paths relative to the $G960ROOT/src directory.
#    LIBS:  directories containing source code for native 960 libraries.
#		Entries are paths relative to the $G960ROOT/src/lib directory.
# Files may contain blank lines and '#'-delimited comments.
#
# The top-level Makefile consists of a series of specific targets for each
# tool or library, and a default "all" target that invokes all the others.
# The host for which it builds depends on the 'HOST' variable being set on
# the invocation line.
#
# Lower-level (tool-specific) makefiles are made by:
#   o defining the HOST variable.
#   o "uncommenting" and appending the makefile in the corresponding 'common'
#     directory: the string "#__<hostname>__#" is removed from the beginning of
#     the line whenever <hostname> matches the host in question.
#------------------------------------------------------------------------------

PATH=/bin:/usr/bin:/usr/ucb; export PATH
umask 002
CO=/usr/local/bin/co
MYNAME=`basename $0`

if test ! -d admin ; then
	if test x"$G960ROOT" = x; then
		echo "$MYNAME: cd to the tools root directory (or set G960ROOT) first"
		exit 1
	else
		echo 'cd $G960ROOT ('$G960ROOT')'
		cd $G960ROOT
		if test ! -d admin ; then
			echo "$MYNAME: can't find 'admin' subdirectory"
			exit 1
		fi
	fi
fi
G960ROOT=`pwd`

TOOLPATH=src
LIBPATH=src/lib

#-----------------------------------------------------------------------------
# Read info files, stripping comments
#-----------------------------------------------------------------------------
HOSTS=`sed 's/#.*//' admin/HOSTS`
TOOLS=`sed -e 's/#.*//' admin/TOOLS`
LIBS=`sed 's/#.*//' admin/LIBS`

# Remove newlines
TOOLS=`echo $TOOLS`
LIBS=`echo $LIBS`

#-----------------------------------------------------------------------------
# Give usage message, if necessary
#-----------------------------------------------------------------------------
if test $# = 0 ; then
	echo "Usage:	$MYNAME [-t] tool ..."
	echo
	echo "tool	Builds makefiles for each specified tool in the source"
	echo "	directories for each of the installed hosts.  Known tools:"
	NLINES=`echo $TOOLS $LIBS|wc -w`
	NLINES=`expr \( $NLINES / 3 \) + 1`
	echo $TOOLS $LIBS | sed 's/ /\
/g' |  pr -t -3 -l$NLINES -w52 | sed 's/^/		/'
	echo
	echo "-t	Builds the top-level Makefile (in $G960ROOT)"
	exit 1;
fi

#------------------------------------------------------------------------------
# Parse args
#------------------------------------------------------------------------------
TOPLEVEL=
LIST=
while test $# != 0 ; do
	case "$1" in
	-t)
		TOPLEVEL=1
		;;
	-*)
		echo "$MYNAME: bad argument: '$1'"
		echo "Enter '$MYNAME' without arguments to get usage info"
		exit 1
		;;
	*)
		LIST="$LIST $1"
		;;
	esac;

	shift
done


if test "$TOPLEVEL" ; then
	#----------------------------------------------------------------------
	# Make top-level Makefile.
	#----------------------------------------------------------------------
	GLIBS=
	LIBPATHS=
	for i in $LIBS ; do
		GLIBS="$GLIBS ${i}g"
		LIBPATHS="$LIBPATHS lib/$i"
	done

	echo "#This makefile was generated by '$MYNAME'"	 > Makefile
	echo "#"						>> Makefile
	echo "G960ROOT= $G960ROOT"				>> Makefile
	echo "TOOLS= $TOOLS"					>> Makefile
	echo "ILIBS= $LIBS"					>> Makefile
	echo "GLIBS= $GLIBS"					>> Makefile
	echo "LIBPATHS= $LIBPATHS"				>> Makefile
	cat admin/Protomake					>> Makefile

	for t in $TOOLS ; do
		echo						>> Makefile
		echo "$t:"					>> Makefile
		echo "	make tool DIR=$t HOST=\${HOST} CLEAN=\${CLEAN} TARGET=install" >> Makefile
	done

	for l in $LIBS ; do
		d=lib/$l
		echo						>> Makefile
		echo "${l}g:"					>> Makefile
		echo "	make tool DIR=$d HOST=\${HOST} CLEAN=\${CLEAN} TARGET=install_gnu" >> Makefile
		echo						>> Makefile
		echo "$l:"					>> Makefile
		echo "	make tool DIR=$d HOST=\${HOST} CLEAN=\${CLEAN} TARGET=install_intel" >> Makefile
	done
fi

#----------------------------------------------------------------------
# For each specified tool, make makefiles for *all* hosts based
# on the template makefile in the 'common' directory of the tool.
# (Ignore hosts that are not installed.)
#----------------------------------------------------------------------
for tool in $LIST ; do
	#------------------------------------------------------------
	# Check for existence of installed tool or library.
	# If neither is found, source is not installed for this item.
	#------------------------------------------------------------
	if test -d $TOOLPATH/$tool/common ; then
		SRCDIR=$TOOLPATH/$tool
	elif test -d $LIBPATH/$tool/common ; then
		SRCDIR=$LIBPATH/$tool
	else
		echo "+	source not installed"
		continue
	fi

	if test -d $SRCDIR/common/RCS -a -f $CO; then
		cd $SRCDIR/common
		$CO Makefile
	fi

	for host in $HOSTS; do
		cd $G960ROOT

		HOSTDIR=$SRCDIR/$host
		if test ! -d $HOSTDIR ; then	# host not installed
			continue
		fi

		cd $HOSTDIR

		rm -f Makefile
		if test -f Makefile ; then
			echo "$MYNAME: no write permission in $HOSTDIR"
			continue
		fi

		echo "#The next line was generated by '$MYNAME'" \
							 >Makefile
		echo "HOST=$host"			>>Makefile
		echo					>>Makefile
		sed "s/^#__${host}__#//" <../common/Makefile >>Makefile
	done
done
