#!/bin/bash

# This is a wrapper for clang/clang++ gcc/g++

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if [ -z "$INFER_RESULTS_DIR" ]; then
    echo '$INFER_RESULTS_DIR with a path to the results dir not provided.' > /dev/stderr
    exit 1
fi

# invoke the right compiler looking at the final plusplus (e.g. gcc/g++ clang/clang++)
if [ "${0%++}" != "$0" ]; then XX="++"; fi
FRONTEND_COMMAND=("$SCRIPT_DIR/../clang_wrappers/hijack_and_normalize_clang_command$XX" "$@")
HOST_COMPILER_COMMAND=("$SCRIPT_DIR/../clang_wrappers/filter_args_and_run_fcp_clang$XX" "$@")

if [ -n "$INFER_COMPILER_WRAPPER_IN_RECURSION" ]; then
    if [ -z "$INFER_LISTENER" ]; then
        "${HOST_COMPILER_COMMAND[@]}"
    fi
else
    export INFER_COMPILER_WRAPPER_IN_RECURSION="Y"
    export FCP_RESULTS_DIR="$INFER_RESULTS_DIR";

    "${FRONTEND_COMMAND[@]}"
fi

exit $?
