#!/usr/bin/env bash
#
# Shim for 'make' on Windows where GNU make is not available.
# Delegates to cmake --build for plugin btest tests.
#
# This script is placed in testing/scripts/ which is on the btest PATH.
# On non-Windows platforms, delegate to the system make.
#

# On non-Windows, find and exec the real make.
case "$(uname -s)" in
    MINGW* | MSYS*) ;;
    *)
        for p in /usr/bin/make /usr/local/bin/make /opt/homebrew/bin/make; do
            [ -x "$p" ] && exec "$p" "$@"
        done
        echo "Error: could not find system 'make'" >&2
        exit 1
        ;;
esac

cmake_build_dir=build
cmake_args=()
do_install=false

# Handle VERBOSE environment variable
if [ "${VERBOSE}" = "1" ]; then
    cmake_args+=(--verbose)
fi

# Parse arguments for target
for arg in "$@"; do
    case "$arg" in
        install)
            do_install=true
            ;;
        clean)
            cmake_args+=(--target clean)
            ;;
        VERBOSE=1)
            cmake_args+=(--verbose)
            ;;
    esac
done

if [ "$do_install" = true ]; then
    cmake --install "$cmake_build_dir"
else
    cmake --build "$cmake_build_dir" "${cmake_args[@]}"
fi
