#!/bin/sh -ex

output_tar="$1"
cc="$2"
shift 2

config_dir=$(mktemp -d)

# See if the compiler is a wrapper script and find the real one if it is
if head -c 20 "$cc" | grep -q '^#!/bin/[a-z]*sh'; then
    wrapped_cc=$(grep -m 1 '^exec' "$cc" | sed 's/exec *\([^ ]*\).*/\1/')
    test -n "$wrapped_cc" && cc="$wrapped_cc"
fi

external/coverity/bin/cov-configure --compiler "$cc" --comptype gcc --config "$config_dir/coverity_config.xml" "$@"
tar cJf "$output_tar" -C "$config_dir" $(ls "$config_dir")
rm -rf "$config_dir"
