# -*- mode: cmake -*-
# vi: set ft=cmake :

find_program(Bazel_EXECUTABLE NAMES bazel bazelisk)

execute_process(COMMAND "${Bazel_EXECUTABLE}" version
  WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
  RESULT_VARIABLE _BAZEL_VERSION_EXECUTE_PROCESS_RESULT_VARIABLE
  OUTPUT_VARIABLE _BAZEL_VERSION_EXECUTE_PROCESS_OUTPUT_VARIABLE
  ERROR_QUIET
)

set(Bazel_VERSION)

if(_BAZEL_VERSION_EXECUTE_PROCESS_RESULT_VARIABLE EQUAL 0)
  string(REGEX MATCH "Build label: ([0-9a-zA-Z.]+)"
    _BAZEL_VERSION_REGEX_MATCH_OUTPUT_VARIABLE
    "${_BAZEL_VERSION_EXECUTE_PROCESS_OUTPUT_VARIABLE}"
  )

  if(CMAKE_MATCH_1)
    set(Bazel_VERSION "${CMAKE_MATCH_1}")
  endif()

  unset(_BAZEL_VERSION_REGEX_MATCH_OUTPUT_VARIABLE)
endif()

unset(_BAZEL_VERSION_EXECUTE_PROCESS_OUTPUT_VARIABLE)
unset(_BAZEL_VERSION_EXECUTE_PROCESS_RESULT_VARIABLE)

include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(Bazel
  FOUND_VAR Bazel_FOUND
  REQUIRED_VARS Bazel_EXECUTABLE
  VERSION_VAR Bazel_VERSION
)
