cmake_minimum_required(VERSION 2.6)

INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
INCLUDE_DIRECTORIES("${LUA_INCDIR}")
LINK_DIRECTORIES("${LUA_LIBDIR}")

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckLibraryExists)

IF (UNIX OR NOT WIN32)
  CHECK_INCLUDE_FILES(fcntl.h HAVE_FCNTL_H)
  CHECK_INCLUDE_FILES(unistd.h HAVE_UNISTD_H)
  CHECK_INCLUDE_FILES(dirent.h HAVE_DIRENT_H)
  CHECK_INCLUDE_FILES(time.h HAVE_TIME_H)
  CHECK_INCLUDE_FILES(sys/time.h HAVE_SYS_TIME_H)
  CHECK_INCLUDE_FILES(sys/ndir.h HAVE_SYS_NDIR_H)
  CHECK_INCLUDE_FILES(sys/utsname.h HAVE_SYS_UTSNAME_H)
  CHECK_INCLUDE_FILES(sys/dir.h HAVE_SYS_DIR_H)
  CHECK_INCLUDE_FILES(ndir.h HAVE_NDIR_H)
  CHECK_FUNCTION_EXISTS(getcwd HAVE_GETCWD)
  CHECK_LIBRARY_EXISTS(dl dlopen "" HAVE_DLOPEN)
ENDIF (UNIX OR NOT WIN32)

CONFIGURE_FILE("paths.h.in" "${CMAKE_CURRENT_BINARY_DIR}/paths.h")

SET(src 
  "${CMAKE_CURRENT_SOURCE_DIR}/paths.c"
  "${CMAKE_CURRENT_BINARY_DIR}/paths.h" )

SET(luasrc 
  "${CMAKE_CURRENT_SOURCE_DIR}/init.lua")

# When using MSVC
IF(MSVC)
  # we want to respect the standard, and we are bored of those **** .
  ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE=1)
ENDIF(MSVC)

ADD_LIBRARY("paths" MODULE ${src})
SET_TARGET_PROPERTIES("paths" PROPERTIES
  PREFIX "lib"
  IMPORT_PREFIX "lib")

IF(APPLE)
  SET_TARGET_PROPERTIES("paths" PROPERTIES
    LINK_FLAGS "-undefined dynamic_lookup")
ENDIF()

INSTALL(FILES ${luasrc} DESTINATION ${LUADIR}/paths)
INSTALL(TARGETS paths
  LIBRARY DESTINATION ${LIBDIR}
  RUNTIME DESTINATION ${LIBDIR})

IF(LUALIB)
  TARGET_LINK_LIBRARIES(paths ${LUALIB})
ENDIF()
