#!/bin/sh

# establish variations
#
func_to_use=${1:-fail}
top=`pwd`
if [ "${top}" = "/" ] ; then
    top=""
fi
configure_test=${top}/configure


# create autoconf input file if it doesnt exist
#
if [ ! -f configure.ac ] ; then
    echo "*** CREATE configure.ac ***"
    ( cat << EOF
AC_PREREQ(2.57)
AC_INIT([expr-configure],[1.5.11-1],[cygwin at cygwin dot com])
# for the purpose of this test, getting here is a success
exit 0
EOF
    ) > configure.ac ;
fi


# create configure script if it doesnt exist
#
if [ ! -f configure ] ; then
    echo "*** GENERATE configure ***"
    autoconf
fi


# declare functions which pass and fail
#
pass () {

${configure_test} -n \
    --target=i386-rtems \
    --host=i686-pc-cygwin \
    --build=i686-pc-cygwin \
    build_alias=i686-pc-cygwin \
    host_alias=i686-pc-cygwin \
    target_alias=i386-rtems \
    --cache-file=/dev/null

}

fail () {

# strace -o strace_$$.log --mask=minimal \
${configure_test} -n \
    --target=i386-rtems \
    --enable-rtemsbsp=386pc \
    --host=i686-pc-cygwin \
    --build=i686-pc-cygwin \
    build_alias=i686-pc-cygwin \
    host_alias=i686-pc-cygwin \
    target_alias=i386-rtems \
    --cache-file=/dev/null

}

ccjfail () {

# strace -o strace_$$.log --mask=minimal \
${configure_test} -n \
    --prefix=/opt/rtems \
    --with-multisubdir=m68000 \
    --with-multisrctop= \
    --with-multibuildtop= \
    --prefix=/opt/rtems \
    --host=m68k-rtems \
    --build=i686-pc-mingw32 \
    --target=m68k-rtems \
    --enable-multilib \
    --enable-doc \
    --enable-cxx \
    --enable-posix \
    --enable-networking \
    --disable-tests \
    --disable-itron \
    --with-target-subdir=m68k-rtems \
    --exec-prefix=/opt/rtems/m68k-rtems \
    --cache-file=/dev/null \
    --srcdir=/crossfire/src/rtems/cpukit \
    build_alias=i686-pc-mingw32 \
    host_alias=m68k-rtems \
    target_alias=m68k-rtems \
    -srcdir=/crossfire/src/rtems/cpukit \
    CC='m68k-rtems-gcc --pipe -m68000' \
    --cache-file=/dev/null \
    --srcdir=/crossfire/src/rtems/cpukit/posix
}

ccjminfail () {

# strace -o strace_$$.log --mask=minimal \
${configure_test} -n \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    --enable-foobar
}

ccjmin2fail () {

# strace -o strace_$$.log --mask=minimal \
${configure_test} -n \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    build_alias=xxx-yyy
}

ccjminpass () {

# strace -o strace_$$.log --mask=minimal \
${configure_test} -n \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    --enable-doc

}

ccjmin2pass () {

# strace -o strace_$$.log --mask=minimal \
${configure_test} -n \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    --cache-file=/dev/null \
    build_alias=xxx-yyy

}

# declare maintenance functions
#
clean () 
{
	set -x
	rm -rf *.log *.cache
	touch .stop
	set +x
}

distclean () 
{
	set -x
	clean
	rm -f configure configure.ac
	touch .stop
	set +x
}


# begin iterations
#
echo "Running using function $func_to_use()..."
iter=0
while [ ! -f ${top}/.stop ] && echo "*** TEST ${iter} ***" ; do
    if ! $func_to_use > ./fail.log 2>&1 ; then break ; fi
    iter=`expr $iter + 1`;
done
if [ -f ${top}/.stop ] ; then
    echo "*** STOP ***" ;
else
    echo "*** TEST ${iter} FAILS ***"
    tail ./fail.log
fi
rm -f .stop
exit 1
