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

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

rootme=`pwd`

if [ -z "${bt}" ]
 then bt="${ht}"
fi

# list of potential tools to test
alltools="binutils ld gas gdb expect gcc"

# prune the list
tools=
for tool in $alltools; do
    if [ -d "$sourcedir"/"$tool" ]; then
	tools="$tools $tool"
    fi
done

for tool in $tools; do
    mkdir -p $tool/testsuite
    exec > $tool/testsuite/site.exp

    srcdir="$sourcedir"/"$tool"

    echo "## These variables are automatically generated by bozoware ##"

    case "$tool" in
	gcc)
            echo "set libiconv \"\""
            echo "set libiconv \"\""
	    echo "set CXXFLAGS \"\""
	    ;; # nothing, since it can be gcc or g++
	dejagnu)
	    echo "set tool \"runtest\"" ;;
	*)
	    echo "set tool \"$tool\"" ;;
    esac
    
    echo "set srcdir \"$srcdir/testsuite\""
    echo "set host_triplet \"$ht\""
    echo "set build_triplet \"$bt\""
    echo "set target_triplet \"$ht\""
    echo "set target_alias \"$ht\""

    if [ $tool != "gdb" ]; then
        echo "set rootme \"$rootme\""
        echo "set tmpdir \"$rootme/$tool/testsuite\""
        echo "set objdir \"$rootme/$tool\""
    fi

    if [ $tool = "ld" ]; then
	mkdir -p "$tool/testsuite/tmpdir"
	echo "set libpath \"/usr/local/lib /usr/ccs/lib /lib /usr/lib /usr/lib/w32api\"" > $tool/testsuite/tmpdir/libpath.exp
        echo "set OFILES \"\""
        echo "set BFDLIB \"\""
        echo "set LIBIBERTY \"\""
        echo "set HOSTING_EMU \"\""
        echo "set HOSTING_CRT0 \"\""
        echo "set HOSTING_LIBS \"\""
        echo "set LIBS \"\""
	
    fi
    echo "## All variables above are generated by bozoware. Do Not Edit ##"

    exec

    if [ $tool = "gdb" ]; then
        for x in `
	    cd $srcdir/testsuite
	    find gdb.* ! -name CVS -type d -print
	`
	do
	    mkdir -p "$tool"/testsuite/"$x"
        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="'gcc'"'
    fi
    if [ $tool = "ld" ]; then
        vars="$vars CXX="'"'"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 -e 's/^/check-/' \
	  -e 's/ / check-/g' \
	  -e '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
