#!/bin/bash

## Things to test:
##   the plugin builds!
##   plugin loads
##   passes vamp-plugin-tester tests
##   does not export any unnecessary symbols
##   has valid .cat and .n3

platform=linux
bits=64

configure() {
    dir="$1"
    if [ -f "$dir/configure" ] ; then
	( cd "$dir" ; ./configure )
    fi
}

configure_maybe() {
    dir="$1"
    if [ ! -f "$dir/Makefile" ] ; then
	configure "$dir"
    fi
}

find_makefile() {
    dir="$1"
    for f in Makefile Makefile.$platform Makefile.$platform$bits build/$platform/Makefile build/$platform/Makefile.$platform build/$platform/Makefile.$platform$bits; do
	if [ -f "$dir/$f" ]; then
	    echo $f
	    break
	fi
    done
}

build() {
    dir="$1"
    if configure_maybe "$dir"; then
	mfile=$(find_makefile "$dir")
	if [ -n "$mfile" ]; then
	    make -C "$dir" -f "$mfile"
	else
	    echo "Failed to find a Makefile in $dir"
	    return 1
	fi
    fi
}

rebuild() {
    dir="$1"
    if configure "$dir"; then
	mfile=$(find_makefile "$dir")
	if [ -n "$mfile" ]; then
	    make -C "$dir" -f "$mfile" clean
	    make -C "$dir" -f "$mfile"
	else
	    echo "Failed to find a Makefile in $dir"
	    return 1
	fi
    fi
}

successes="/tmp/successes.$$.txt"
failures="/tmp/failures.$$.txt"
trap 'rm -f "$successes" "$failures"' 0

for dir in $(cat .hgsub | awk '{ print $1; }') ; do
    echo
    echo "Processing: $dir"
    if build "$dir"; then
	echo "$dir" >> "$successes"
    else
	echo "$dir" >> "$failures"
    fi
done

echo
echo "** Succeeded in building:"
cat "$successes"

echo
echo "** Failed to build:"	    
cat "$failures"

echo
