#!/bin/sh

guis="gtk kde fltk sdl" # fb aqua
renderers="ogl agg cairo"
media="ffmpeg gst mad"
#srcdir=/home/rob/projects/gnu
#srcdir=/work/gnash-cvs/
#srcdir=/home/strk/src/gnash/gnash-head
srcdir=/home/strk/src/gnash/gnash-0.8.1-branch
test=$srcdir/testsuite/movies.all/gravity.swf
date=`date`

renderers="ogl"
#
# Create a directory for the build
#
build_create ()
{
    if test ! -d build-$1-$2-$3; then
        echo "Creating build directory for $1 $2 $3 "
	mkdir build-$1-$2-$3
    fi
}

build_test ()
{
    touch test.log
    alldone=0

    if test ! -d build-$1-$2-$3; then
        echo "WARNING: No build directory for $1 $2"
    else
	if test -e build-$1-$2-$3/gui/gnash; then
	    echo "============= $date ===============" >> test.log
	    build-$1-$2-$3/gui/gnash -t 5 $test
	    alldone=`grep -c "TRACE: bounced" gnash-dbg.log`
	    if test $alldone -gt 3; then
		echo "PASS: ran test case sucessfully"
		echo "$date: build-$1-$2-$3 PASSED" >> test.log
	    else
		echo "FAIL: couldn't run test case"
		echo "$date: build-$1-$2-$3 FAILED" >> test.log
	    fi
	fi
    fi
}

compare_config ()
{
    if test ! -d build-$1-$2-$3; then
        echo "WARNING: No build directory for $1 $2"
	return
    fi

    if test ! -f build-$1-$2-$3/config.status; then
	return
    fi

    if test -f build-$1-$2-$3/.tmp; then
	rm -f build-$1-$2-$3/.tmp
    fi

    cd build-$1-$2-$3

    make dumpconfig > .tmp 2>&1
    out1=0
    out2=0
    if test -e .tmp; then
	newgui=`grep "GUI:" .tmp | cut -d ' ' -f 3 | tr "[:lower:]" "[:upper:]"`
	newrend=`grep "Renderer engine:" .tmp | cut -d ' ' -f 3 | tr "[:lower:]" "[:upper:]"`
	gui=`echo $1 | tr "[:lower:]" "[:upper:]"`
	rend=`echo $2 | tr "[:lower:]" "[:upper:]"`
 	if test -f Makefile; then
	    guilib=$gui
	    rendlib=$gui
	    if test x$gui = xGTK; then
		guilib=GTK2
	    fi
	    if test x$gui = xFLTK; then
		guilib=FLTK2
	    fi
	    if test x$rend = xOGL; then
		rendlib=OPENGL
	    fi
	    out1=`grep "^${guilib}_LIBS = " Makefile`
	    out1=`echo $out1 | wc -w`
	    out2=`grep "^${rendlib}_LIBS = " Makefile`
	    out2=`echo $out2 | wc -w`
        fi
	rm -f .tmp
	if test x$newrend = x$rend -a x$newgui = x$gui -a $out1 -gt 2 -a $out2 -gt 2; then
	    echo "PASS: config matches for build-$1-$2-$3 "
	else
	    echo -n "FAIL: config doesn't match for build-$1-$2-$3:"
            echo " $newrend != $rend || $newgui != $gui || $out1 <= 2 (${guilib}_LIBS)|| $out2 <= 2 (${rendlib}_LIBS)"
	fi
    else
	echo "FAIL: couldn't dump the configuration"
    fi

    cd ..
}

#
# Configure Gnash for a set of renderer and gui toolkits
#
build_config ()
{
    echo "Configuring build for $1 $2-$3"
    options=""
    if test ! -d build-$1-$2-$3; then
	echo "Build directory build-$1-$2-$3 hasn't been created yet!"
	build_create $1 $2 $3
    fi
    cd build-$1-$2-$3
    $srcdir/configure --enable-gui=$1 --enable-renderer=$2 --enable-media=$3
    cd ..
    compare_config $1 $2 $3
}

#
# Compare the config options of the executable with what it should be
#
compare_build ()
{
    options=""
#     if test -d build-$1-$2-$3; then
# 	echo "Build directory build-$1-$2-$3 hasn't been configured yet!"
#     fi
    if test ! -d build-$1-$2-$3; then
	return
    fi

    cd build-$1-$2-$3
#    gui=`echo build-$1-$2-$3 |cut -d '-' -f 2` 
#    render=`echo build-$1-$2-$3 |cut -d '-' -f 3` 
    if test -f Makefile; then
      if test -f gui/gnash; then
	out=`gui/gnash --version | grep "Renderer:"`
	newgui=`echo $out | cut -d ' ' -f 5`
	newrend=`echo $out | cut -d ' ' -f 2`

	if test x$newrend = x$2 -a x$newgui = x$1; then
	    echo "PASS: config matches executable for build-$1-$2-$3 "
	else
	    echo "FAIL: config doesn't match executable for build-$1-$2-$3"
	fi
      else
	echo "	WARNING: No executable yet for build-$1-$2-$3"
      fi
    fi

    if test -e make.log -a -f gui/gnash; then
	errors=`grep -c ": error: " make.log`
	warnings=`grep -c ": warning: " make.log`
	if test $errors -gt 0; then
	    echo "$errors ERRORS found"
	fi
	if test $warnings -gt 0; then
	    echo "$errors ERRORS found"
	fi
    fi

    cd ..
}

#
# Compile Gnash
#
build_compile ()
{
    touch make.log

    echo "============= $date ===============" >> make.log
    options1="-i -k $MAKEOPTS CXXFLAGS=-g -j6" # first pass
    options2="$MAKEOPTS CXXFLAGS=-g" # first pass
    if test ! -d build-$1-$2-$3; then
	echo "Build directory build-$1-$2-$3 hasn't been configured yet!"
    fi

    cd build-$1-$2-$3
    make $options1 | tee -a make.log 2>&1 
    make $options2 | tee -a make.log 2>&1 
    cd ..
    compare_build $1 $2 $3
}

#
# Run the tests
#
build_check ()
{
    options=""			# -j4
    if test ! -d build-$1-$2-$3; then
	echo "Build directory build-$1-$2 hasn't been configured yet!"
    fi
    cd build-$1-$2-$3
    make -i -k check RUNTESTFLAGS=-a | tee -a make-check.log 2>&1
    out=`make anal`
    fail=`echo $out | grep "Total real failures" | cut -d ' ' -f 4`
    passes=`echo $out | grep "Total passes" | cut -d ' ' -f 4`
    cd ..

    if test $fail -gt 0; then
	echo "Got $fail failures"
	echo "$fail: build-$1-$2-$3 FAILURES" >> test.log
    fi
    if test $passes -gt 0; then
	echo "Got $passes sucesses"
	echo "$passes: build-$1-$2-$3 FAILURES" >> test.log
    fi
#    build_compare $1 $2 $3
}

#
# Cleanup the build tree, removing all created files
#
build_clean()
{
    if test ! -d build-$1-$2-$3; then
	echo "Build directory build-$1-$2-$3 hasn't been configured yet!"
    fi
    cd build-$1-$2-$3
    make clean
    rm make.log make-check.log
    cd ..    
}

#
# Do the dirty work
#

# take the action from the command line
case $1 in
    create|config|compile|check|clean|test)
	for i in $guis; do
	    for j in $renderers; do
		for k in $media; do
		    build_$1 $i $j $k
		done
	    done
	done
	;;
    compare)
	for i in $guis; do
	    for j in $renderers; do	
		for k in $media; do
		    compare_config $i $j $k
		    compare_build $i $j $k
		done
	    done
	done
	;;
    all)
	for i in $guis; do
	    for j in $renderers; do
		for k in $media; do
		    build_create $i $j $k
		    build_config $i $j $k
		    build_compile $i $j $k
		    build_check $i $j $k
		done
	    done
	done
	;;
    target)
	build_create $2 $3 $4
	build_config $2 $3 $4
	build_compile $2 $3 $4
#	build_check $2 $3 $4
	;;
    *)
	echo "specify one of 'create config compile check compare clean' or 'all'"
	;;
    esac

