#!/bin/bash

set -e

# 2014-09-11 - this detected 128 cores but the build failed
#              Web site suggests 1.5 cores
#              => hardcode for 1 core
#CORES=`cat /proc/cpuinfo | grep -c ^cpu`
CORES=1
echo "*** Detected ${CORES} CPU core(s). ***"
MAKE_ARGS=-j`expr ${CORES} + 1`

# Check if we are building for a Coverity scan
# We may not do the scan every time on every platform
if [ -e /tmp/coverity-scan/cov-analysis/bin ];
then
  # Coverity complains with a message
  #    Use the linux64 Prevent distribution on this machine
  #    This platform is not supported by Coverity.
  #    See documentation for the ltest of supported platforms.
  #
  # Setting COVERITY_UNSUPPORTED=1 should work around the error:

  COVERITY_UNSUPPORTED=1 \
    PATH=/tmp/coverity-scan/cov-analysis/bin:${PATH} \
    cov-build --dir cov-int \
    make ${MAKE_ARGS}

  # submit the coverity artifacts to their scanning service:
  tar czf resiprocate-coverity.tgz cov-int
  curl \
    --form project=reSIProcate \
    --form token=-OmW1wnKp9CVC7iGPDUlqw \
    --form email=daniel@pocock.com.au \
    --form file=@resiprocate-coverity.tgz \
    --form version=unknown \
    --form description="Travis auto build" \
    http://scan5.coverity.com/cgi-bin/upload.py
else
  make ${MAKE_ARGS}
fi

# we run the test cases after the Coverity scan for now, because
# the Coverity scan might find false positives in the test code
# it would be better to find a way to tell Coverity that the test
# code is not released code, as scanning the test code may allow
# it to evaluate more code paths through the stack itself

make ${MAKE_ARGS} check

