#!/bin/sh

unset CDPATH

REPO_URL=http://svn.dvm-system.org/svn/dvmhrepo/dvm
REPO_USER=dvmhuser
REPO_PASS=dvmh2013
PATHS_OF_INTEREST="cdvm/trunk cdvmh-clang/trunk driver/trunk fdvm/trunk general/examples/trunk general/platforms/trunk general/trunk rts/trunk rts-dvmh/trunk tools/pppa/trunk tools/predictor/trunk"

SVN_PARAMS="--username $REPO_USER --password $REPO_PASS --non-interactive"

PREV_REV=`cat latest-seen-revision`
[ "$PREV_REV" -ge 0 ] 2>/dev/null
if [ $? -ne 0 ]; then
    PREV_REV=0
fi

NEW_REV=`svn info $SVN_PARAMS $REPO_URL | grep "Revision" | awk '{ print $2 }'`
[ "$NEW_REV" -ge 1 ] 2>/dev/null
if [ $? -ne 0 ]; then
    NEW_REV=0
fi

# If latest-seen-revision is corrupted, then inspect only latest revision
if [ $PREV_REV -eq 0 -a $NEW_REV -gt 0 ]; then
    PREV_REV=$(( NEW_REV - 1 ))
fi

CUR_REV=$(( PREV_REV + 1 ))
while [ $CUR_REV -le $NEW_REV ]; do
    TO_TEST=1
    COMMIT_MSG=`svn log $SVN_PARAMS --incremental -c $CUR_REV $REPO_URL | tail -n +4`
    IS_INTERMEDIATE=`echo "$COMMIT_MSG" | grep -i "intermediate" | wc -l`
    IS_TRIVIAL=`echo "$COMMIT_MSG" | grep -i "trivial" | wc -l`
    IS_COSMETICS=`echo "$COMMIT_MSG" | grep -i "cosmetics" | wc -l`
    IS_MAJOR=`echo "$COMMIT_MSG" | grep -i "major" | wc -l`
    if [ $IS_INTERMEDIATE -ne 0 -o $IS_TRIVIAL -ne 0 -o $IS_COSMETICS -ne 0 ]; then
        TO_TEST=0
    elif [ $IS_MAJOR -ne 0 ]; then
        TO_TEST=2
    fi
    if [ $TO_TEST -ne 0 -a -n "$PATHS_OF_INTEREST" ]; then
        NO_TEST=1
        for p in $PATHS_OF_INTEREST; do
            WHAT_CHANGED=`svn diff $SVN_PARAMS --summarize -c $CUR_REV $REPO_URL/$p`
            if [ -n "$WHAT_CHANGED" ]; then
                NO_TEST=0
                break
            fi
        done
        if [ $NO_TEST -ne 0 ]; then
            TO_TEST=0
        fi
    fi
    if [ $TO_TEST -eq 1 ]; then
        echo $CUR_REV >>pending-revisions
    elif [ $TO_TEST -eq 2 ]; then
        echo $CUR_REV >>pending-revisions-full
    fi
    CUR_REV=$(( CUR_REV + 1 ))
done

if [ $NEW_REV -gt 0 ]; then
    echo $NEW_REV >latest-seen-revision
fi
