#!/bin/bash

# This is a wrapper for javac

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

HOST_COMPILER=(`PATH=$INFER_OLD_PATH which javac`)
COMPILER_ARGS="$@"
HOST_COMPILER_COMMAND=("$HOST_COMPILER" $COMPILER_ARGS)
FRONTEND_COMMAND=("infer" "-a" "capture" "-o" "$INFER_RESULTS_DIR" "--" "javac" $COMPILER_ARGS)

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"
    "${FRONTEND_COMMAND[@]}"
fi

exit $?
