#!/bin/sh
##----------------------------------------------------------------------
## Continuous integration run script
##----------------------------------------------------------------------
## Copyright (C) 2007-2011 The NOC Project
## See LICENSE for details
##----------------------------------------------------------------------

JUNIT_REPORT=test-out.xml
COVERAGE_REPORT=test-coverage.xml
PEP8_VIOLATIONS_REPORT=pep8-violations.xml
TEST_DB=test_noc

error_exit ( ) {
    echo "$PROGNAME: ${1:-'Unknown error'}" 1>&2
    echo "Terminating" 1>&2
    exit 1
}

## Jump to NOC
cd `dirname $0`/..
rm -f $JUNIT_REPORT $COVERAGE_REPORT

## Destroy hanging test db
if [ `psql -l | grep $TEST_DB | wc -l` -ne 0 ]; then
    echo "Dropping hanging test db"
    dropdb $TEST_DB
fi

## Run
echo "Running CI scripts ..."
echo "Installing .conf files"
./scripts/install-conf || error_exit "$LINENO: Failed to install .conf"

echo "Synchronizing contrib/"
./scripts/sync-contrib || error_exit "$LINENO: contrib/ synch failed"

echo "Running tests"
./noc test --junit-xml-out=$JUNIT_REPORT --coverage-xml-out=$COVERAGE_REPORT

echo "Running violations tests"
./scripts/check_pep8 > $PEP8_VIOLATIONS_REPORT

echo "done"
