# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

cmake_minimum_required(VERSION 3.5)

set(quiet "@quiet@")
set(script_dir "@CMAKE_CURRENT_FUNCTION_LIST_DIR@/ExternalProject")
include(${script_dir}/captured_process_setup.cmake)

if("@LOCAL@" STREQUAL "")
  message(FATAL_ERROR "LOCAL can't be empty")
endif()

if(NOT EXISTS "@LOCAL@")
  message(FATAL_ERROR "File not found: @LOCAL@")
endif()

function(do_verify)
  if("@ALGO@" STREQUAL "")
    _ep_message_quiet_capture(WARNING
      "File will not be verified since no URL_HASH specified"
    )
    set(accumulated_output "${accumulated_output}" PARENT_SCOPE)
    return()
  endif()

  if("@EXPECT_VALUE@" STREQUAL "")
    _ep_message_quiet_capture(FATAL_ERROR "EXPECT_VALUE can't be empty")
  endif()

  _ep_message_quiet_capture(STATUS
"verifying file...
     file='@LOCAL@'"
  )
  set(accumulated_output "${accumulated_output}" PARENT_SCOPE)

  file("@ALGO@" "@LOCAL@" actual_value)

  if(NOT "${actual_value}" STREQUAL "@EXPECT_VALUE@")
    _ep_message_quiet_capture(FATAL_ERROR
"error: @ALGO@ hash of
  @LOCAL@
does not match expected value
  expected: '@EXPECT_VALUE@'
    actual: '${actual_value}'
")
  endif()

  _ep_message_quiet_capture(STATUS "verifying file... done")
  set(accumulated_output "${accumulated_output}" PARENT_SCOPE)
endfunction()

do_verify()

set(extract_script "@extract_script_filename@")
if(NOT "${extract_script}" STREQUAL "")
  include("${extract_script}")
endif()
