# TODO: simplify it.

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)  
  if(NOT CMAKE_TOOLCHAIN_FILE)
    if(WIN32)
      set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory" FORCE)
    else()
      set(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Installation Directory" FORCE)
    endif()
  else()
    # any cross-compiling
    set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory" FORCE)
  endif()
endif()

if(WIN32 AND CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
  if(DEFINED TBPLAS_RUNTIME AND DEFINED TBPLAS_ARCH)
    tbp_update(TBPLAS_INSTALL_BINARIES_PREFIX "${CMAKE_INSTALL_PREFIX}/${TBPLAS_ARCH}/${TBPLAS_RUNTIME}")
  else()
    message(STATUS "Can't detect runtime and/or arch")
    tbp_update(TBPLAS_INSTALL_BINARIES_PREFIX "${CMAKE_INSTALL_PREFIX}")
  endif()
  if(TBPLAS_STATIC)
    tbp_update(TBPLAS_INSTALL_BINARIES_SUFFIX "staticlib")
  else()
    tbp_update(TBPLAS_INSTALL_BINARIES_SUFFIX "lib")
  endif()
  tbp_update(TBPLAS_BIN_INSTALL_PATH           "${TBPLAS_INSTALL_BINARIES_PREFIX}/bin")
  tbp_update(TBPLAS_PYTHON_INSTALL_PATH        "${TBPLAS_INSTALL_BINARIES_PREFIX}/python")
  tbp_update(TBPLAS_SAMPLES_BIN_INSTALL_PATH   "${TBPLAS_INSTALL_BINARIES_PREFIX}/samples")
  tbp_update(TBPLAS_LIB_INSTALL_PATH           "${TBPLAS_INSTALL_BINARIES_PREFIX}/${TBPLAS_INSTALL_BINARIES_SUFFIX}")
  tbp_update(TBPLAS_LIB_ARCHIVE_INSTALL_PATH   "${TBPLAS_LIB_INSTALL_PATH}")
  tbp_update(TBPLAS_INCLUDE_INSTALL_PATH       "include")
  tbp_update(TBPLAS_SAMPLES_SRC_INSTALL_PATH   "samples")
  tbp_update(TBPLAS_TEST_DATA_INSTALL_PATH     "testdata")
  tbp_update(TBPLAS_DOC_INSTALL_PATH           "doc")
else() # UNIX
  include(GNUInstallDirs)
  tbp_update(TBPLAS_BIN_INSTALL_PATH           "bin")
  tbp_update(TBPLAS_PYTHON_INSTALL_PATH        "python")
  tbp_update(TBPLAS_SAMPLES_BIN_INSTALL_PATH   "${TBPLAS_BIN_INSTALL_PATH}")
  tbp_update(TBPLAS_LIB_INSTALL_PATH           "${CMAKE_INSTALL_LIBDIR}")
  tbp_update(TBPLAS_LIB_ARCHIVE_INSTALL_PATH   "${TBPLAS_LIB_INSTALL_PATH}")
  tbp_update(TBPLAS_INCLUDE_INSTALL_PATH       "include")
  tbp_update(TBPLAS_SAMPLES_SRC_INSTALL_PATH   "samples")
  tbp_update(TBPLAS_TEST_DATA_INSTALL_PATH     "testdata")
  tbp_update(TBPLAS_DOC_INSTALL_PATH           "doc")
endif()

tbp_update(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${TBPLAS_LIB_INSTALL_PATH}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

if(INSTALL_TO_MANGLED_PATHS)
  foreach(v
      TBPLAS_INCLUDE_INSTALL_PATH
      TBPLAS_SAMPLES_SRC_INSTALL_PATH
      TBPLAS_DOC_INSTALL_PATH
      TBPLAS_TEST_DATA_INSTALL_PATH
    )
    string(REGEX REPLACE "TBPLAS[0-9]*" "TBPLAS-${TBPLAS_VERSION}" ${v} "${${v}}")
  endforeach()
endif()

# Install 3rdparty header-only libraries to tbplas-cpp/3rdparty.
# Otherwise there will be missing header errors.
install(DIRECTORY ${PROJECT_SOURCE_DIR}/3rdparty DESTINATION ${CMAKE_INSTALL_PREFIX})
