#!/usr/bin/env bash

###
# Taken from https://github.com/awvwgk/staged-recipes/tree/dftd4/recipes/dftd4
###

# Detecting flags from gfortran which is backed by clang fails with
#     x86_64-apple-darwin13.4.0-gfortran: error trying to exec 'cc1': execvp: No such file or directory
# This comes from
#     https://github.com/mesonbuild/meson/blob/61993f893bbdc2415155e28ee70e6ea806725e64/mesonbuild/environment.py#L668-L671
# which runs
#     x86_64-apple-darwin13.4.0-gfortran -E -dM -
# Looking at the verbose output this is trying to run:
#     cc1 -E -quiet -v -D__DYNAMIC__ - -fPIC -mmacosx-version-min=10.9 -mtune=generic -dM
# The -quiet argument must be removed from the arguments as this isn't supported by clang
# Adding this file to PATH hacks around these issues

# Already reported at: https://github.com/conda-forge/gfortran_osx-64-feedstock/issues/11

ARGS=()
for var in "$@"; do
    # Ignore known bad arguments
    [ "$var" != '-quiet' ] && ARGS+=("$var")
done

"$CC" "${ARGS[@]}"
