#!/bin/sh -e

emit_dir="$1"
emit_db_file="$2"
emit_wd_file_list="$3"
license_file="$4"
coding_std_path="$5"
shift 5

mkdir --parent "$emit_dir/emit/$(hostname)"
# Version 2021.03
echo 299 > "$emit_dir/emit/version"
ln "$emit_db_file" "$emit_dir/emit/$(hostname)/emit-db"

# Pass the working directories for each emit-db in using a response file
# so we don't run over Linux's command line length limit.  The format of
# this file is one argument per line.
response_file="$(mktemp)"
while read emit_wd_file; do
  echo --strip-path >> "$response_file"
  cat "$emit_wd_file" >> "$response_file"
done < "$emit_wd_file_list"

# Coding standards are supplied by the target.
if [ -n "$coding_std_path" ]; then
  echo --coding-standard-config >> "$response_file"
  echo "$coding_std_path" >> "$response_file"
fi

# Emit defects when parse errors occur.
echo --enable >> "$response_file"
echo PARSE_ERROR >> "$response_file"

# Tell cov-analyze where to find our license file.
echo --security-file >> "$response_file"
echo "$license_file" >> "$response_file"

external/coverity/bin/cov-analyze --dir "$emit_dir" @@"$response_file"
