#!/bin/sh

check_config() {
    cat ./AUDIT | grep "^$1"
}

clean_files() {
    if [ -e .config ]; then
        SDK=`grep "^PTXCONF_PREFIX" ./.config | cut -d"=" -f2 | sed 's/\"//g'`
	if [ -d ${SDK} ]; then
	    rm -rf ${SDK}
	fi
    fi
    make distclean
}

if [ $# -ne 1 ]
then
    echo "Available options:"
    echo "xtools"
    echo "all"
    exit 0
fi

OPT=$1

XTOOLS="no"
if [ $OPT = "xtools" ]; then
    CONFIGS=`./configure | grep "^ " | grep "xtools"`
    XTOOLS="yes"
elif [ $OPT = "all" ]; then
    CONFIGS=`./configure | grep "^ "`
else
    echo "Bad option!"
    exit 1
fi

test -e ./AUDIT || touch AUDIT

echo "Start auditing `date`" >> AUDIT

clean_files

for config in ${CONFIGS}; do
    if [ "x`check_config ${config}`" = "x" ]; then
	./configure ${config}
	RET="0"
	if [ "${XTOOLS}" = "yes" ]; then
	    make virtual-xchain_install
	else
	    echo "Not work yet!"
	    exit 1
	    if [ `echo ${config} | grep "xtools"` ]; then
		make virtual-xchain_install
	    else
		make world
	    fi
	fi

	if [ "$?" = "0" ]; then
	     echo "${config} OK" >> AUDIT
	else
	     echo "${config} FAILED" >> AUDIT
	fi
	clean_files
    fi
done
