#!/bin/bash

ROOT=$PWD
REV=$1
LOGFILE=${ROOT}/logs/${REV}.log

function im-not-the-next() {
    for i in `ls queue`
    do
	if [ $i -lt $REV ]; then
	    true
	    return
	fi
    done
    false
}

function make-dir () {
    if [ ! -d $1 ]; then
	mkdir $1
    fi
}

function makecheck () {
    if [ `which $(echo $CXX|cut -f1 -d' ' )` ]; then
	make-dir $ODIR &&
	cd $ODIR &&
	time ../trunk/configure $* &&
	time make check &&
	cd .. 
    else
	echo
	echo '***************************************************************'
	echo "  COMPILER $CXX CANNOT BE FOUND"
	echo '***************************************************************'
	echo
    fi
}

function check-compilers() {
cd trunk &&
autoreconf -vi &&
cd .. &&
ODIR=gcc-3.3 CXX=g++-3.3 CXXFLAGS='-W -Wall -Werror' makecheck --enable-demo --enable-benchmark &&
ODIR=gcc-3.4 CXX=g++-3.4 CXXFLAGS='-W -Wall -Werror' makecheck --enable-demo --enable-benchmark &&
ODIR=gcc-4.0 CXX=g++-4.0 CXXFLAGS='-W -Wall -Werror' makecheck --enable-demo --enable-benchmark &&
ODIR=gcc-4.1 CXX=g++-4.1 CXXFLAGS='-W -Wall -Werror' makecheck --enable-demo --enable-benchmark &&
# # ODIR=icc-8.0 CXX=icpc-8.0 CXXFLAGS='-Werror' makecheck --enable-demo --enable-benchmark &&
# ODIR=icc-9.0 CXX='icpc-9.0' CXXFLAGS='-Werror' makecheck --enable-demo --enable-benchmark &&
# ODIR=mingw CXX='g++' makecheck --with-msw --target=i586-mingw32msvc --host=i586-mingw32msvc --build=i386-linux --without-glpk &&
echo &&
echo '**********************************************************************' &&
echo '   REPOSITORY SEEMS OK' &&
echo '**********************************************************************' &&
echo
}

if [ $# -eq 0 ]; then
    echo 'Usage:'
    echo '   commit-checker --init'
    echo '   commit-checker revision'
    exit 1
elif [ $1 = '--init' ]; then
    rm -rf queue
    mkdir queue
    make-dir logs
    rm -rf trunk
    svn co https://lemon.cs.elte.hu/svn/hugo/trunk
    exit 0
fi

touch ${ROOT}/queue/$REV

while im-not-the-next; do
    sleep 10
done

# echo RUN $REV

svn up -r$REV trunk >${LOGFILE} 2>&1

AUTHOR=`svn info trunk|grep 'Last Changed Author:'|cut -d ' ' -f 4`
EMAIL=`awk '$1=="'${AUTHOR}'" {print $2}' <${ROOT}/e-mails`

echo '

*************************************************************
Check revision '${REV}' of '${AUTHOR}' ('${EMAIL}')
*************************************************************

'>>${LOGFILE} 2>&1

if check-compilers >>${LOGFILE} 2>&1
then
    echo -n
else
    echo 'Dear '${AUTHOR}',
Your svn commit -r'${REV}' made the repository broken, or at least
the compiler emits warnings during the build process.
The compilation log is attached.
Please fix the problem as soon as possible.

Best regards,
LEMON Commit Checking Service
'|
    mail -a ${LOGFILE} -s '[LEMON-SVN] WARNING: Revision '${REV}' is broken' \
	${EMAIL} alpar@cs.elte.hu

    echo 'Commit -r'${REV}' made the repository broken, or at least
the compiler emits warnings during the build process.
Do not update until this bug is fixed, unless you really need it.

Best regards,
LEMON Commit Checking Service
'|
    mail -s 'WARNING: Revision '${REV}' is broken' \
	${EMAIL} lemon-commits@lemon.cs.elte.hu
fi
    
#    rm ${LOGFILE}
rm ${ROOT}/queue/$REV

