#! /bin/sh
if [ $# -lt 2 ]
then
	echo "usage: `basename $0` target-alias devodir [host-triplet]"
	exit 1
fi

ttrip="$1";
sourcedir="$2"
ht="$3"

# convert target alias to a canonical triplet
ft=`sh "$sourcedir/config.sub" "$ttrip"` || {
  # config.sub might not work if we test a development branch using
  # main-trunk devo.  so we fake something up.
  case "$ttrip" in
  *-*-*)	ft="$ttrip";;
  *-*)		ft=`echo "$ttrip" | sed -e 's/-/-unknown-/'`;;
  *)		ft="$ttrip-unknown-unknown";;
  esac
  echo "(Using '$ft' as the canonical target triple)"
}

rootme=`pwd`

bt=`sh "$sourcedir/config.guess"`
if [ -z "${ht}" ]
 then ht="${bt}"
fi

tools="binutils ld gas gdb expect dejagnu gcc libstdc++ sim libstdc++-v3"
if [ -d $sourcedir/libjava ]; then
    tools="$tools libjava"
fi
for tool in $tools
do

mkdir -p $tool/testsuite
exec > $tool/testsuite/site.exp

if [ -d "$sourcedir/$tool" ]
then
	srcdir="$sourcedir/$tool"
else
	srcdir="$sourcedir";
fi

echo "## these variables are automatically generated by make ##"
echo "# Do not edit here. If you wish to override these values"
echo "# add them to the last section"
if [ $tool != "gcc" -a $tool != "dejagnu" ]
then
	echo "set tool \"$tool\""
fi

# Gotta love it!
if [ $tool = "dejagnu" ]
then
	echo "set tool runtest"
fi

if [ $tool = "sim" ]
then
	arch=`echo $ft | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'`
	echo "set arch \"$arch\""
fi

echo "set srcdir \"$srcdir\""
echo "set host_triplet \"$ht\""
echo "set build_triplet \"$bt\""
echo "set target_triplet \"$ft\""
echo "set target_alias \"$ttrip\""
echo "set srcdir \"\${srcdir}/testsuite\""
if [ $tool != "gdb" ]
then
	echo "set rootme \"$rootme\""
	echo "set tmpdir \"$rootme/$tool/testsuite\""
	echo "set objdir \"$rootme/$tool\""
fi
if [ $tool = "ld" ]
then
	echo "set OFILES \"\""
	echo "set BFDLIB \"\""
	echo "set LIBIBERTY \"\""
	echo "set HOSTING_EMU \"\""
	echo "set HOSTING_CRT0 \"\""
	echo "set HOSTING_LIBS \"\""
fi
echo "## All variables above are generated by bozoware. Do Not Edit ##"


exec
if [ $tool = "gdb" ]
then
	for x in `ls -d $srcdir/testsuite/gdb.*`; do
		if [ -d $x ]; then
			mkdir -p $tool/testsuite/`basename $x`
		fi
	done
fi

exec > "$tool/Makefile"

echo 'EXPECT = `if [ -f $${rootme}/../expect/expect ] ; then \
            echo $${rootme}/../expect/expect ; \
          else echo expect ; fi`

RUNTEST=`if [ -f $${srcdir}/../dejagnu/runtest ] ; then \
	       echo $${srcdir}/../dejagnu/runtest ; \
	    else echo runtest; fi`
RUNTESTFLAGS='

string='	EXPECT=${EXPECT} ; export EXPECT ; \
	rootme=`pwd`; export rootme; \
	srcdir="'$srcdir'"; export srcdir; \
	cd testsuite;\
	if [ -f $${rootme}/../expect/expect ] ; then  \
	   TCL_LIBRARY=$${srcdir}/../tcl/library ; \
	   export TCL_LIBRARY ; fi ; \'

vars=""

if [ $tool = gcc ]
then
	echo "check: check-gcc check-g++"
	echo ""
	echo "check-gcc:"
	echo "$string"
	echo '	$(RUNTEST) --tool gcc $(RUNTESTFLAGS)'
	echo ""
	echo "check-g++:"
	vars="--tool g++"
else
	echo "check:"
fi
echo "$string"
if [ $tool = "binutils" -o $tool = "ld" ]
then
	vars='CC="'${ttrip}-gcc'"'
fi
if [ $tool = "ld" ]
then
	vars="$vars CXX="'"'"${ttrip}-g++"'" CFLAGS="" CXXFLAGS=""'
fi
if [ $tool = "expect" ]
then
	vars='EXPECT=$${EXPECT}'
fi
if [ $tool = "dejagnu" ]
then
	vars='RUNTEST=runtest'
fi
echo '	$(RUNTEST) '"$vars"' $(RUNTESTFLAGS)'
echo ""
exec

done

exec > Makefile
ct=`echo $tools | sed 's/^/check-/;s/ / check-/g;s/check-gcc/check-gcc check-g++/;'`
echo "check: $ct"
for tool in $tools
do
	echo "check-${tool}:"
	if [ $tool != "gcc" ]
	then
		echo "	@cd $tool; make check RUNTESTFLAGS="'"$(RUNTESTFLAGS)"'
	else
		echo "	@cd gcc; make check-gcc RUNTESTFLAGS="'"$(RUNTESTFLAGS)"'
		echo ""
		echo "check-g++:"
		echo "	@cd gcc; make check-g++ RUNTESTFLAGS="'"$(RUNTESTFLAGS)"'
	fi
	echo ""
done
exec
