
if(POLICY CMP0012)
  cmake_policy(SET CMP0012 NEW)
endif()

set(VIRTUALENV_ARGS --clear --no-pip)
if ( "@SimpleITK_FORBID_DOWNLOADS@" )
  set(VIRTUALENV_ARGS ${VIRTUALENV_ARGS} --never-download)
endif()
if ( NOT "@SimpleITK_PYTHON_WHEEL@" )
  if ("@PYTHON_VIRTUALENV_VERSION_STRING@" VERSION_GREATER "13")
    set(VIRTUALENV_ARGS ${VIRTUALENV_ARGS} --no-wheel)
  endif()
  if ( NOT "@SimpleITK_PYTHON_EGG@" )
    set(VIRTUALENV_ARGS ${VIRTUALENV_ARGS} --no-setuptools)
  endif()
endif()

#
# Create the Python virtual environment.
#
execute_process(
  COMMAND ${CMAKE_COMMAND}
    "-E"
    "remove_directory"
    "@PythonVirtualenvHome@"
  )
execute_process(
  COMMAND "@PYTHON_EXECUTABLE@"
    "@PYTHON_VIRTUALENV_SCRIPT@"
    "--python=@PYTHON_EXECUTABLE@"
    ${VIRTUALENV_ARGS}
    "@PythonVirtualenvHome@"
  WORKING_DIRECTORY "@SimpleITK_SOURCE_DIR@/Wrapping/Python"
  RESULT_VARIABLE failed
  ERROR_VARIABLE error
  )

if ( failed )
  message( FATAL_ERROR "Creation of Python virtual enviroment failed.\n${error}" )
endif()

#
# Install the SimpleITK Package into the virtual environment.
#
message(STATUS "Installing Python Package: SimpleITK")

# The working directory for the installation and packaging with
# setup.py is very important. As if the setup is run from another
# directory files will not be correctly execute_process.
execute_process(
  COMMAND "@VIRTUAL_PYTHON_EXECUTABLE@" "@CMAKE_CURRENT_BINARY_DIR@/Packaging/setup.py" install
  WORKING_DIRECTORY "@SimpleITK_Python_BINARY_DIR@"
  RESULT_VARIABLE failed
  ERROR_VARIABLE error
  )

if ( failed )
  message( FATAL_ERROR "Installation of SimpleITK into Python virutal enviroment failed.\n${error}" )
endif()


if(@BUILD_TESTING@)
  #
  # Get Python library directory.
  #
  execute_process(
    COMMAND "@VIRTUAL_PYTHON_EXECUTABLE@" -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
    OUTPUT_VARIABLE PythonVirtualenvLib
    ERROR_VARIABLE error
    RESULT_VARIABLE failed
    OUTPUT_STRIP_TRAILING_WHITESPACE
    )

  if ( failed )
    message( SEND_ERROR "Determining python library path failed.\n${error}" )
  endif()



  #
  # Manual copy the numpy installation into the virtual environment.
  #
  execute_process(
    COMMAND "@PYTHON_EXECUTABLE@" -c "import numpy; import os; print(os.path.dirname(numpy.__file__))"
    OUTPUT_VARIABLE numpy_dir
    ERROR_VARIABLE error
    RESULT_VARIABLE failed
    OUTPUT_STRIP_TRAILING_WHITESPACE
    )

  if( failed )
    message( WARNING "Failed to determine numpy.__file__ path: \"${error}\"" )
  else()

    message( STATUS "Installing numpy from ${numpy_dir} to ${PythonVirtualenvLib}/numpy")

    if ( UNIX )
      execute_process( COMMAND ${CMAKE_COMMAND} -E create_symlink "${numpy_dir}" "${PythonVirtualenvLib}/numpy"
        RESULT_VARIABLE failed
        ERROR_VARIABLE error )
      if ( failed )
        message( SEND_ERROR ${error} )
      endif()
    else ()
      execute_process( COMMAND ${CMAKE_COMMAND} -E copy_directory "${numpy_dir}" "${PythonVirtualenvLib}/numpy"
        RESULT_VARIABLE failed
        ERROR_VARIABLE error )
      if ( failed )
        message( SEND_ERROR ${error} )
      endif()
    endif ()
  endif()
endif()
