# Copyright (c) 2016 - 2018, Kai Wolf. All rights reserved.
# Use of this source code is governed by a MIT license that can be found in
# the LICENSE file.

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  set(CMAKE_INSTALL_PREFIX
    ${CMAKE_BINARY_DIR}/install CACHE PATH "Default install path" FORCE)
endif()

include(GNUInstallDirs)
set(INSTALL_RUNTIME_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "")
set(INSTALL_LIBRARY_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "")
set(INSTALL_ARCHIVE_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "")

if(UNIX)
  set(INSTALL_IMPORTED_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "")
else()
  set(INSTALL_IMPORTED_LIB_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "")
endif()

set(INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "")
set(INSTALL_DATA_DIR ${CMAKE_INSTALL_DATAROOTDIR} CACHE PATH "")
set(INSTALL_DOC_DIR ${CMAKE_INSTALL_DOCDIR} CACHE PATH "")
set(INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "")

if(UNIX AND NOT APPLE)
  set(INSTALL_XDG_APPS_DIR ${INSTALL_DATA_DIR}/applications CACHE PATH "")
  set(INSTALL_XDG_ICON_DIR ${INSTALL_DATA_DIR}/pixmaps CACHE PATH "")
endif()

# Setup relocatable path (RPATH) for the project
option(ENABLE_RPATH "Enable rpath support on macOS and Linux" ON)
if(NOT CMAKE_INSTALL_RPATH)
  set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/${INSTALL_LIBRARY_DIR})
endif()

if(APPLE AND NOT CMAKE_INSTALL_NAME_DIR)
  set(CMAKE_INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/${INSTALL_LIBRARY_DIR})
endif()

if(UNIX AND ENABLE_RPATH)
  set(CMAKE_SKIP_BUILD_RPATH FALSE)
  set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
  set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
  set(CMAKE_MACOSX_RPATH TRUE)

  # The RPATH to be used when installing, but only if it's not a system directory
  list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES ${CMAKE_INSTALL_PREFIX}/lib isSystemDir)
  if(${isSystemDir} STREQUAL "-1")
    set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
  endif()
endif()