#!/bin/sh

# if LLVM_CONFIG variable doesn't explicitly specify which llvm-config
# we should use, then we will search across all possible llvm-configs
# that might work for us.  Our search strategy is to try to pick up
# 3.4, then 3.8, and, if neither found, fallback to any llvm-config.
#
# Note1 - suffix `-mp-` stands for macports, and will work on macs
# ifused with macports.
#
# Note2 - the LLVM_CONFIG variable is a fallback for debugging or
# workarounds. The recommended way to install a desired <version> of
# llvm is to use `llvm-conf.<version>` package.


configs="llvm-config-3.4 llvm-config-mp-3.4 llvm-config-3.8 llvm-config-mp-3.8 llvm-config"

config=`opam config var llvm-config 2>/dev/null` || true
if [ -z $config ]; then
    config=$LLVM_CONFIG
fi

if ! which "$config"; then
    for c in $configs; do
        if which $c; then
            config=$c
            break
        fi
    done
fi

if ! which "$config" ; then
    exit 1
else
    version=`$config --version`
fi

filename="conf-bap-llvm.config"
cat > $filename <<EOF
opam-version: "2.0.0"
variables {
  config: "$config"
  package-version: "$version"
}
EOF
