#!/bin/sh

if [ "$testdir" = "" ]; then
   echo You must use make check or make check-ada
   exit 1
fi

# Set up environment to use the Ada compiler from the object tree

host_gnatchop=`type gnatchop | awk '{print $3}'`
host_gnatmake=`type gnatmake | awk '{print $3}'`
ROOT=`${PWDCMD-pwd}`
BASE=`cd $ROOT/../../..; ${PWDCMD-pwd}`

PATH=$BASE:$ROOT:$PATH
ADA_INCLUDE_PATH=$BASE/ada/rts
LD_LIBRARY_PATH=$ADA_INCLUDE_PATH:$LD_LIBRARY_PATH
ADA_OBJECTS_PATH=$ADA_INCLUDE_PATH

if [ ! -d $ADA_INCLUDE_PATH ]; then
   echo gnatlib missing, exiting.
   exit 1
fi

if [ ! -f $BASE/gnatchop ]; then
   echo gnattools missing, exiting.
   exit 1
fi

if [ ! -f $BASE/gnatmake ]; then
   echo gnattools missing, exiting.
   exit 1
fi

GCC_DRIVER="$BASE/xgcc"
GCC="$BASE/xgcc -B$BASE/"
export PATH ADA_INCLUDE_PATH ADA_OBJECTS_PATH GCC_DRIVER GCC LD_LIBRARY_PATH

echo '#!/bin/sh' > host_gnatchop
echo PATH=`dirname $host_gnatchop`:'$PATH' >> host_gnatchop
echo unset ADA_INCLUDE_PATH ADA_OBJECTS_PATH GCC_EXEC_PREFIX >> host_gnatchop
echo export PATH >> host_gnatchop
echo exec $host_gnatchop '"$@"' >> host_gnatchop

chmod +x host_gnatchop

echo '#!/bin/sh' > host_gnatmake
echo PATH=`dirname $host_gnatmake`:'$PATH' >> host_gnatmake
echo unset ADA_INCLUDE_PATH ADA_OBJECTS_PATH GCC_EXEC_PREFIX >> host_gnatmake
echo export PATH >> host_gnatmake
echo exec $host_gnatmake '"$@"' >> host_gnatmake

chmod +x host_gnatmake

exec $testdir/run_all.sh "$@"
