#!/bin/sh

# Configuration script
#   Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc.

#This file is part of GNU.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

# $Id$

#
# Shell script to create proper links to machine-dependent files in
# preparation for compilation.
#
# If configure succeeds, it leaves its status in config.status.
# If configure fails after disturbing the status quo, 
# 	config.status is removed.
#

remove=rm
hard_link=ln
symbolic_link='ln -s'

#for Test
#remove="echo rm"
#hard_link="echo ln"
#symbolic_link="echo ln -s"

progname=$0

# clear some things potentially inherited from environment.
ansi=
arguments=$*
defaulttargets=
destdir=
fatal=
hostsubdir=
Makefile=Makefile
Makefile_in=Makefile.in
norecursion=
recurring=
removing=
srcdir=
srctrigger=
target=
targets=
commontargets=
configdirs=
targetsubdir=
template=
verbose=

for arg in $*;
do
	case ${arg} in
	-ansi | +a*)
		ansi=true
		clib=clib
		;;
	-destdir=* | +destdir=* | +destdi=* | +destd=* | +dest=* | +des=* | +de=* | +d=*)
		destdir=`echo ${arg} | sed 's/[+-]d[a-z]*=//'`
		;;
	-languages=* | +languages=* | +language=* | +languag=* \
		| +langua=* | +langu=* | +lang=* | +lan=* | +la=* \
		| +l=*)
		languages="${languages} `echo ${arg} | sed 's/[+-]l[a-z]*=//'`"
		;;
	-gas | +g*)
		gas=yes
		;;
	-help | +h*)
		fatal=true
		;;
	-nfp | +nf*)
		nfp=yes
		;;
	-norecursion | +no*)
		norecursion=true
		;;
	-recurring | +recurring | +recurrin | +recurri | +recurr | +recur | +recu | +rec | +re)
		recurring=true
		;;
	-rm | +rm)
		removing=${arg}
		;;
#	-srcdir=* | +srcdir=* | +srcdi=* | +srcd=* | +src=* | +sr=* | +s=*)
#		srcdir=`echo ${arg} | sed 's/[+-]s[a-z]*=//'`
#		;;
	-subdirs | +f* | +su*)
		subdirs=${arg}
		;;
	-target=* | +target=* | +targe=* | +targ=* | +tar=* | +ta=*)
		if [ -n "${targets}" ] ; then
			subdirs="+subdirs"
		fi

		newtargets="${targets} `echo ${arg} | sed 's/[+-]t[a-z]*=//'`"
		targets="${newtargets}"
		;;
	-template=* | +template=* | +templat=* | +templa=* | +templ=* | +temp=* | +tem=* | +te=*)
		template=`echo ${arg} | sed 's/[+-]template=//'`
		;;
	-v | -verbose | +v*)
		verbose=${arg}
		;;
	-* | +*)
		(echo ;
		echo "Unrecognized option: \"${arg}\"". ;
		echo) 1>&2
		fatal=true
		;;
	*)
		if [ -n "${hosts}" ] ; then
			subdirs="+subdirs"
		fi

		newhosts="${hosts} ${arg}"
		hosts=${newhosts}
		;;
	esac
done

if [ -n "${verbose}" ] ; then
	echo `pwd`/configure $*
fi

# process host and target only if not rebuilding configure itself or removing.
if [ -z "${template}" -a -z "${removing}" -a -z "${fatal}" ] ; then
	# Complain if an arg is missing
	if [ -z "${hosts}" ] ; then
		(echo ;
		echo "configure: No HOST specified." ;
		echo) 1>&2
		fatal=true
	fi
fi

if [ -n "${fatal}" -o "${hosts}" = "help" ] ; then
	(echo "Usage: configure HOST" ;
	echo ;
	echo "Options: [defaults in brackets]" ;
	echo " +ansi		configure w/ANSI library. [no ansi lib]" ;
	echo " +destdir=MYDIR	configure for installation into MYDIR. [/usr/local]" ;
	echo " +subdirs		configure in subdirectories.  [in source directories]" ;
	echo " +lang=LANG	configure to build LANG. [gcc]" ;
	echo " +help		print this message. [normal config]" ;
	echo " +gas		configure the compilers for use with gas. [native as]" ;
	echo " +nfp		configure the compilers default to soft floating point. [hard float]" ;
	echo " +norecursion	configure this directory only. [recurse]" ;
	echo " +rm		remove this configuration. [build a configuration]" ;
	echo " +target=TARGET	configure for TARGET.  [TARGET = HOST]" ;
	echo " +template=TEM	rebuild configure using TEM. [normal config]" ;
	echo ;
	echo "Where HOST and TARGET are something like \"vax\", \"sun3\", \"encore\", etc." ;
	echo "Asking for more than one \"+target\" implies \"+subdirs\".  Any other" ;
	echo "options given will apply to all targets.") 1>&2

	if [ -r config.status ] ; then
		cat config.status
	fi

	exit 1
fi

#### configure.in common parts come in here.
# This file is a shell script that supplies the information necessary
# to tailor a template configure script into the configure script
# appropriate for this directory.  For more information, check any
# existing configure script.

srctrigger=ar.c
srcname="binutils"

## end of common part.

# are we rebuilding config itself?
if [ -n "${template}" ] ; then
	if [ ! -r ${template} ] ; then
		echo '***' "Can't find template ${template}." 1>&2
		exit 1
	fi

# prep the template
	sed -e '/^#### configure.in common parts come in here.$/,/^## end of common part.$/c\
#### configure.in common parts come in here.\
## end of common part.' \
	-e '/^#### configure.in per-host parts come in here.$/,/^## end of per-host part.$/c\
#### configure.in per-host parts come in here.\
## end of per-host part.' \
	-e '/^#### configure.in per-target parts come in here.$/,/^## end of per-target part.$/c\
#### configure.in per-target parts come in here.\
## end of per-target part.' \
	-e '/^#### configure.in post-target parts come in here.$/,/^## end of post-target part.$/c\
#### configure.in post-target parts come in here.\
## end of post-target part.' \
	< ${template} > template.new

	if [ -r configure.in ] ; then
		if [ -z "`grep '^# per\-host:' configure.in`" ] ; then
			echo '***' `pwd`/configure.in has no "per-host:" line. 1>&2
			exit 1
		fi

		if [ -z "`grep '^# per\-target:' configure.in`" ] ; then
			echo '***' `pwd`/configure.in has no "per-target:" line. 1>&2
			exit 1
		fi

		# split configure.in into common, per-host, per-target,
		# and post-target parts.  Post-target is optional.
		sed -e '/^# per\-host:/,$d' configure.in > configure.com
		sed -e '1,/^# per\-host:/d' -e '/^# per\-target:/,$d' configure.in > configure.hst
		if grep -s '^# post-target:' configure.in ; then
		  sed -e '1,/^# per\-target:/d' -e '/^# post\-target:/,$d' configure.in > configure.tgt
		  sed -e '1,/^# post\-target:/d' configure.in > configure.pos
		else
		  sed -e '1,/^# per\-target:/d' configure.in > configure.tgt
		  echo >configure.pos
		fi

		# and insert them
		sed -e '/^#### configure.in common parts come in here.$/  r configure.com' \
			-e '/^#### configure.in per\-host parts come in here.$/  r configure.hst' \
			-e '/^#### configure.in per\-target parts come in here.$/  r configure.tgt' \
			-e '/^#### configure.in post\-target parts come in here.$/  r configure.pos' \
			template.new > configure.new

		rm -f configure.com configure.tgt configure.hst configure.pos
	else
		echo Warning: no configure.in in `pwd`
		cat ${template} >> configure
	fi

	chmod a+x configure.new
	rm template.new
#	mv configure configure.old
	mv configure.new configure

	if [ -n "${verbose}" ] ; then
		echo Rebuilt configure in `pwd`
	fi

	# Now update config.sub from the template directory.
	if echo "$template" | grep -s 'configure$' ; then
		cp `echo "$template" | sed s/configure$/config.sub/` ./config.sub.new
	#	mv config.sub config.sub.old
		mv config.sub.new config.sub

		if [ -n "${verbose}" ] ; then
			echo Rebuilt config.sub in `pwd`
		fi
	fi

	if [ -z "${norecursion}" ] ; then
		# If template is relative path, make it absolute for recurring.
		if echo "${template}" | grep -s '^/' ; then
		   true
		else
		   template=`pwd`/${template}
		fi

		while [ -n "${configdirs}" ] ; do
			# set configdir to car of configdirs, configdirs to cdr of configdirs
			set ${configdirs}; configdir=$1; shift; configdirs=$*

			if [ "`echo ${configdir}.*`" != "${configdir}.*" ] ; then
				targetspecificdirs=${configdir}.*
			else
				targetspecificdirs=
			fi

			for i in ${configdir} ${targetspecificdirs} ; do
				if [ -d $i ] ; then
					if [ -r $i/configure ] ; then
						(cd $i ;
							./configure +template=${template} ${verbose})
					else
						echo Warning: No configure script in `pwd`/$i
					fi
				else
					if [ -n "${verbose}" ] ; then
					  echo Warning: directory $i is missing.
					fi
				fi
			done
		done
	fi

	exit 0
fi

# some sanity checks on configure.in
if [ -z "${srctrigger}" ] ; then
	echo Warning: srctrigger not set in configure.in. `pwd` not configured.
	exit 1
fi

for host in ${hosts} ; do
	# Default other arg
	if [ -z "${targets}" -o -n "${defaulttargets}" ] ; then
		targets=${host}
		defaulttargets=true
	fi

	host_alias=${host}

	result=`/bin/sh ./config.sub ${host}`
	host_cpu=`echo $result | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\1/'`
	host_vendor=`echo $result | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\2/'`
	host_os=`echo $result | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\3/'`
	host=${host_cpu}-${host_vendor}-${host_os}
	host_makefile_frag=config/hmake-${host}

#### configure.in per-host parts come in here.

if [ "${host_os}" = "posix" ] ; then
	my_host=posix
else
	case "${host_cpu}" in
	rs6000)	my_host=aix ;;
	mips)
		case "${host_vendor}" in
		dec)	my_host=dec3100 ;;
		esac
		;;
	m88k)
		case "${host_vendor}" in
		*)
			case "${host_os}" in
			dgux)	my_host=dgux ;;
			esac
			;;
		esac
		;;

	m68k)
		case "${host_vendor}" in
		hp)	my_host=hp9000 ;;
		sony)	my_host=news ;;
		sun)	my_host=sun3 ;;
		esac
		;;

	i386)
		case "${host_vendor}" in
		*)
			case "${host_os}" in
			sysv)	my_host=i386v ;;
			esac
			;;
		esac
		;;

	sparc)
		case "${host_vendor}" in
		sun)	my_host=sun4 ;;
		esac
		;;

	rtpc)	my_host=rtbsd ;;
	tahoe | vax)	my_host=${host_cpu} ;;
	esac
fi

if [ ! -f config/hmake-${my_host} ] ; then
	echo '***' Binutils do not support host ${host}
	exit 1
fi

host_makefile_frag=config/hmake-${my_host}

## end of per-host part.

	for target in ${targets} ; do

		target_alias=${target}
		result=`/bin/sh ./config.sub ${target}`
		target_cpu=`echo $result | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\1/'`
		target_vendor=`echo $result | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\2/'`
		target_os=`echo $result | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\3/'`
		target=${target_cpu}-${target_vendor}-${target_os}
		target_makefile_frag=config/tmake-${target}

#### configure.in per-target parts come in here.

files=
links=
## end of per-target part.

		# Temporarily, we support only direct subdir builds.
		hostsubdir=H-${host_alias}
		targetsubdir=T-${target_alias}

		if [ -n "${removing}" ] ; then
			if [ -n "${subdirs}" ] ; then
				if [ -d "${hostsubdir}" ] ; then
					rm -rf ${hostsubdir}/${targetsubdir}

					if [ -z "`(ls ${hostsubdir}) 2>&1 | grep Target- | grep -v Target-independent`" ] ; then
						rm -rf ${hostsubdir}
					fi
				else
					echo Warning: no `pwd`/${hostsubdir} to remove.
				fi
			else
				rm -f ${Makefile} config.status ${links}
			fi
		else
			if [ -n "${subdirs}" ] ; then
				# check for existing status before allowing forced subdirs.
				if [ -f ${Makefile} ] ; then
					echo '***' "${Makefile} already exists in source directory.  `pwd` not configured." 1>&2
					exit 1
				fi

				if [ ! -d ${hostsubdir} ] ; then mkdir ${hostsubdir} ; fi
				cd ${hostsubdir}

				if [ ! -d ${targetsubdir} ] ; then
					if [ -z "${commontargets}" ] ; then
						mkdir ${targetsubdir}
					else
						if [ ! -d Target-independent ] ; then
							mkdir Target-independent
						fi

						${symbolic_link} Target-independent ${targetsubdir}
					fi # if target independent
				fi # if no target dir yet

				cd ${targetsubdir}

				srcdir=../..
			else
				# if not subdir builds, then make sure none exist.
				if [ -n "`(ls .) 2>&1 | grep Host-`" ] ; then
					echo '***' "Configured subdirs exist.  `pwd` not configured." 1>&2
					exit 1
				fi
			fi

			# Find the source files, if location was not specified.
			if [ -z "${srcdir}" ] ; then
				srcdirdefaulted=1
				srcdir=.
				if [ -n "${srctrigger}" -a ! -r ${srctrigger} ] ; then
					srcdir=..
				fi
			fi

			if [ -n "${srctrigger}" -a ! -r ${srcdir}/${srctrigger} ] ; then
				if [ -z "${srcdirdefaulted}" ] ; then
					echo '***' "${progname}: Can't find ${srcname} sources in `pwd`/${srcdir}" 1>&2
				else
					echo '***' "${progname}: Can't find ${srcname} sources in `pwd`/. or `pwd`/.." 1>&2
				fi

				echo '***' \(At least ${srctrigger} is missing.\) 1>&2
				exit 1
			fi

			# Set up the list of links to be made.
			# ${links} is the list of link names, and ${files} is the list of names to link to.

			# Make the links.
			while [ -n "${files}" ] ; do
				# set file to car of files, files to cdr of files
				set ${files}; file=$1; shift; files=$*
				set ${links}; link=$1; shift; links=$*

				if [ ! -r ${srcdir}/${file} ] ; then
					echo '***' "${progname}: cannot create a link \"${link}\"," 1>&2
					echo '***' "since the file \"${file}\" does not exist." 1>&2
					exit 1
				fi

				${remove} -f ${link}
				rm -f config.status
				# Make a symlink if possible, otherwise try a hard link
				${symbolic_link} ${srcdir}/${file} ${link} 2>/dev/null || ${hard_link} ${srcdir}/${file} ${link}

				if [ ! -r ${link} ] ; then
					echo '***' "${progname}: unable to link \"${link}\" to \"${srcdir}/${file}\"." 1>&2
					exit 1
				fi

				if [ -n "${verbose}" ] ; then
					echo "Linked \"${link}\" to \"${srcdir}/${file}\"."
				fi
			done

			# Create a .gdbinit file which runs the one in srcdir
			# and tells GDB to look there for source files.

			case ${srcdir} in
			.)
				;;
			*)
				echo "dir ." > .gdbinit
				echo "dir ${srcdir}" >> .gdbinit
				echo "source ${srcdir}/.gdbinit" >> .gdbinit
				;;
			esac

			# Install a makefile, and make it set VPATH
			# if necessary so that the sources are found.
			# Also change its value of srcdir.

		# FIXME-someday: This business of always writing to .tem and mv back
		# is so that I don't screw things up while developing.  Once this
		# template is stable, these should be optimized. xoxorich.

			# Define macro CROSS_COMPILE in compilation if this is a cross-compiler.
			if [ "${host}" != "${target}" ] ; then
				echo "CROSS=-DCROSS_COMPILE" > ${Makefile}
				echo "ALL=start.encap" >> ${Makefile}
			else
				echo "ALL=all.internal" > ${Makefile}
			fi

			# set target, host, VPATH
			echo "host_alias = ${host_alias}" >> ${Makefile}
			echo "host_cpu = ${host_cpu}" >> ${Makefile}
			echo "host_vendor = ${host_vendor}" >> ${Makefile}
			echo "host_os = ${host_os}" >> ${Makefile}

			echo "target_alias = ${target_alias}" >> ${Makefile}
			echo "target_cpu = ${target_cpu}" >> ${Makefile}
			echo "target_vendor = ${target_vendor}" >> ${Makefile}
			echo "target_os = ${target_os}" >> ${Makefile}

			if [ -n "${subdirs}" ] ; then
				echo "subdir = /${hostsubdir}/${targetsubdir}" >> ${Makefile}
			else
				echo "subdir =" >> ${Makefile}
			fi

		#	echo "workdir = `pwd`" >> ${Makefile}
			echo "VPATH = ${srcdir}" >> ${Makefile}

			# add "Makefile.in" (or whatever it's called)
			cat ${srcdir}/${Makefile_in} >> ${Makefile}

			# Conditionalize the makefile for this host.
			if [ -f ${srcdir}/${host_makefile_frag} ] ; then
				(echo "host_makefile_frag = ${srcdir}/${host_makefile_frag}" ; 
					sed -e "/^####/  r ${srcdir}/${host_makefile_frag}" ${Makefile}) > Makefile.tem
				mv Makefile.tem ${Makefile}
			fi

			# Conditionalize the makefile for this target.
			if [ -f ${srcdir}/${target_makefile_frag} ] ; then
				(echo "target_makefile_frag = ${srcdir}/${target_makefile_frag}" ; 
					sed -e "/^####/  r ${srcdir}/${target_makefile_frag}" ${Makefile}) > Makefile.tem
				mv Makefile.tem ${Makefile}
			fi

			# set srcdir
			sed "s@^srcdir = \.@srcdir = ${srcdir}@" ${Makefile} > Makefile.tem
			mv Makefile.tem ${Makefile}

			# set destdir
			if [ -n "${destdir}" ] ; then
				sed "s:^destdir =.*$:destdir = ${destdir}:" ${Makefile} > Makefile.tem
				mv Makefile.tem ${Makefile}
			fi

			# reset SUBDIRS
			sed "s:^SUBDIRS =.*$:SUBDIRS = ${configdirs}:" ${Makefile} > Makefile.tem
			mv Makefile.tem ${Makefile}

			# reset NONSUBDIRS
			sed "s:^NONSUBDIRS =.*$:NONSUBDIRS = ${noconfigdirs}:" ${Makefile} > Makefile.tem
			mv Makefile.tem ${Makefile}

			using=
			if [ -f ${srcdir}/${host_makefile_frag} ] ; then
				using=" using \"${host_makefile_frag}\""
			fi

			# remove any form feeds.
			sed -e "s///" ${Makefile} > Makefile.tem
			mv Makefile.tem ${Makefile}

			if [ -f ${srcdir}/${target_makefile_frag} ] ; then
				if [ -z "${using}" ] ; then
					andusing=" using \"${target_makefile_frag}\""
				else
					andusing="${using} and \"${target_makefile_frag}\""
				fi
			else
				andusing=${using}
			fi

			if [ -n "${verbose}" -o -z "${recurring}" ] ; then
				echo "Created \"${Makefile}\"" in `pwd`${andusing}.
			fi

#### configure.in post-target parts come in here.

## end of post-target part.

			# describe the chosen configuration in config.status.
			# Make that file a shellscript which will reestablish
			# the same configuration.  Used in Makefiles to rebuild
			# Makefiles.

			echo "#!/bin/sh
# ${srcname} was configured as follows:
${srcdir}/configure" ${arguments} `if [ -z "${norecursion}" ] ; then echo +norecursion ; else true ; fi` > config.status
			chmod a+x config.status

			originaldir=`pwd`
			cd ${srcdir}
		fi

		# If there are subdirectories, then recurse. 
		if [ -z "${norecursion}" -a -n "${configdirs}" ] ; then 
			for configdir in ${configdirs} ; do
				if [ -n "${verbose}" ] ; then
					echo Configuring ${configdir}...
				fi

				if [ -d ${configdir} ] ; then
					(cd ${configdir} ;
						./configure +recurring ${host_alias} +target=${target_alias} \
							${verbose} ${subdirs} ${removing} +destdir=${destdir}) \
						| sed 's/^/	/'
				else
					if [ -n "${verbose}" ] ; then
						echo Warning: directory \"${configdir}\" is missing.
					fi
				fi
			done
		fi
	done # for each target

	# Now build a Makefile for this host.
	if [ -n "${subdirs}" -a ! -n "${removing}" ] ; then
		cd ${hostsubdir}
		cat > GNUmakefile << E!O!F
# Makefile generated by configure for host ${host_alias}.

ALL := $(shell ls -d Target-*)

%:
	$(foreach subdir,$(ALL),$(MAKE) -C $(subdir) \$@ &&) true

all:
E!O!F
		cd ..
	fi
done # for each host

exit 0

#
# Local Variables:
# fill-column: 131
# End:
#

# end of configure