#!/bin/bash

RESULTS_FILE=$1
shift

# Run remain args as test command, capturing the output.
LINTER_OUTPUT=$( $@ 2>&1 )
LINTER_RESULT=$?

mkdir -p `dirname $RESULTS_FILE`
if [[ "$LINTER_RESULT" == "0" ]]; then
  # Lint pass
cat <<ENDXML > $RESULTS_FILE
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="1" failures="0" errors="0" name="Linters">
  <testsuite name="Linters" tests="1" failures="0">
    <testcase name="roslint" status="run" classname="Linter" />
  </testsuite>
</testsuites>
ENDXML
  exit 0
else
  # Lint failed
  # TODO: This output could be more helpful; really just a first cut.
 cat <<ENDXML > $RESULTS_FILE
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="1" failures="1" errors="0" name="Linters">
  <testsuite name="Linters" tests="1" failures="1">
    <testcase name="roslint" classname="Linter">
      <failure message="One or more linter errors was reported." type=""><![CDATA[$LINTER_OUTPUT]]></failure>
    </testcase>
  </testsuite>
</testsuites>
ENDXML
  exit 1
fi
