include(CheckIncludeFiles)
include(CheckIncludeFile)
include(CheckSymbolExists)
include(CheckTypeSize)
include(CheckFunctionKeywords)
include(CheckLFS)

## Checks for header files ##
check_include_file("inttypes.h" HAVE_INTTYPES_H)
check_include_file("memory.h" HAVE_MEMORY_H)
check_include_file("stddef.h" HAVE_STDDEF_H)
check_include_file("stdint.h" HAVE_STDINT_H)
check_include_file("stdlib.h" HAVE_STDLIB_H)
check_include_file("string.h" HAVE_STRING_H)
check_include_file("strings.h" HAVE_STRINGS_H)
check_include_file("sys/types.h" HAVE_SYS_TYPES_H)
if(HAVE_INTTYPES_H)
  set(INCFILE "#include <inttypes.h>")
elseif(HAVE_STDINT_H)
  set(INCFILE "#include <stdint.h>")
else(HAVE_INTTYPES_H)
  set(INCFILE "")
endif(HAVE_INTTYPES_H)

## create configuration files from .cmake file ##
if(BUILD_EXAMPLES)
  ## Checks for WinIO ##
  if(WIN32)
    check_include_file("io.h" HAVE_IO_H)
    check_include_file("fcntl.h" HAVE_FCNTL_H)
    check_symbol_exists("_setmode" "io.h;fcntl.h" HAVE__SETMODE)
    if(NOT HAVE__SETMODE)
      check_symbol_exists("setmode" "io.h;fcntl.h" HAVE_SETMODE)
    endif(NOT HAVE__SETMODE)
    check_symbol_exists("_fileno" "stdio.h" HAVE__FILENO)
    check_symbol_exists("fopen_s" "stdio.h" HAVE_FOPEN_S)
    check_symbol_exists("_O_BINARY" "fcntl.h" HAVE__O_BINARY)
  endif(WIN32)

  ## Checks for large file support ##
  check_lfs(WITH_LFS)
  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/lfs.h.cmake" "${CMAKE_CURRENT_BINARY_DIR}/lfs.h" @ONLY)
endif(BUILD_EXAMPLES)

## generate config.h ##
check_function_keywords("inline;__inline;__inline__;__declspec(dllexport);__declspec(dllimport)")
if(HAVE_INLINE)
  set(INLINE "inline")
elseif(HAVE___INLINE)
  set(INLINE "__inline")
elseif(HAVE___INLINE__)
  set(INLINE "__inline__")
else(HAVE_INLINE)
  set(INLINE "")
endif(HAVE_INLINE)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake" "${CMAKE_CURRENT_BINARY_DIR}/config.h")

## Checks for types ##
# sauchar_t (8bit)
check_type_size("uint8_t" UINT8_T)
if(HAVE_UINT8_T)
  set(SAUCHAR_TYPE "uint8_t")
else(HAVE_UINT8_T)
  check_type_size("unsigned char" SIZEOF_UNSIGNED_CHAR)
  if("${SIZEOF_UNSIGNED_CHAR}" STREQUAL "1")
    set(SAUCHAR_TYPE "unsigned char")
  else("${SIZEOF_UNSIGNED_CHAR}" STREQUAL "1")
    message(FATAL_ERROR "Cannot find unsigned 8-bit integer type")
  endif("${SIZEOF_UNSIGNED_CHAR}" STREQUAL "1")
endif(HAVE_UINT8_T)
# saint_t (32bit)
check_type_size("int32_t" INT32_T)
if(HAVE_INT32_T)
  set(SAINT32_TYPE "int32_t")
  check_symbol_exists("PRId32" "inttypes.h" HAVE_PRID32)
  if(HAVE_PRID32)
    set(SAINT32_PRId "PRId32")
  else(HAVE_PRID32)
    set(SAINT32_PRId "\"d\"")
  endif(HAVE_PRID32)
else(HAVE_INT32_T)
  check_type_size("int" SIZEOF_INT)
  check_type_size("long" SIZEOF_LONG)
  check_type_size("short" SIZEOF_SHORT)
  check_type_size("__int32" SIZEOF___INT32)
  if("${SIZEOF_INT}" STREQUAL "4")
    set(SAINT32_TYPE "int")
    set(SAINT32_PRId "\"d\"")
  elseif("${SIZEOF_LONG}" STREQUAL "4")
    set(SAINT32_TYPE "long")
    set(SAINT32_PRId "\"ld\"")
  elseif("${SIZEOF_SHORT}" STREQUAL "4")
    set(SAINT32_TYPE "short")
    set(SAINT32_PRId "\"d\"")
  elseif("${SIZEOF___INT32}" STREQUAL "4")
    set(SAINT32_TYPE "__int32")
    set(SAINT32_PRId "\"d\"")
  else("${SIZEOF_INT}" STREQUAL "4")
    message(FATAL_ERROR "Cannot find 32-bit integer type")
  endif("${SIZEOF_INT}" STREQUAL "4")
endif(HAVE_INT32_T)
# saint64_t (64bit)
if(BUILD_DIVSUFSORT64)
  check_type_size("int64_t" INT64_T)
  if(HAVE_INT64_T)
    set(SAINT64_TYPE "int64_t")
    check_symbol_exists("PRId64" "inttypes.h" HAVE_PRID64)
    if(HAVE_PRID64)
      set(SAINT64_PRId "PRId64")
    else(HAVE_PRID64)
      set(SAINT64_PRId "\"lld\"")
    endif(HAVE_PRID64)
  else(HAVE_INT64_T)
    check_type_size("int" SIZEOF_INT)
    check_type_size("long" SIZEOF_LONG)
    check_type_size("long long" SIZEOF_LONG_LONG)
    check_type_size("__int64" SIZEOF___INT64)
    if("${SIZEOF_INT}" STREQUAL "8")
      set(SAINT64_TYPE "int")
      set(SAINT64_PRId "\"d\"")
    elseif("${SIZEOF_LONG}" STREQUAL "8")
      set(SAINT64_TYPE "long")
      set(SAINT64_PRId "\"ld\"")
    elseif("${SIZEOF_LONG_LONG}" STREQUAL "8")
      set(SAINT64_TYPE "long long")
      set(SAINT64_PRId "\"lld\"")
    elseif("${SIZEOF___INT64}" STREQUAL "8")
      set(SAINT64_TYPE "__int64")
      set(SAINT64_PRId "\"I64d\"")
    else("${SIZEOF_INT}" STREQUAL "8")
      message(SEND_ERROR "Cannot find 64-bit integer type")
      set(BUILD_DIVSUFSORT64 OFF)
    endif("${SIZEOF_INT}" STREQUAL "8")
  endif(HAVE_INT64_T)
endif(BUILD_DIVSUFSORT64)

## generate divsufsort.h ##
set(DIVSUFSORT_IMPORT "")
set(DIVSUFSORT_EXPORT "")
if(BUILD_SHARED_LIBS)
  if(HAVE___DECLSPEC_DLLIMPORT_)
    set(DIVSUFSORT_IMPORT "__declspec(dllimport)")
  endif(HAVE___DECLSPEC_DLLIMPORT_)
  if(HAVE___DECLSPEC_DLLEXPORT_)
    set(DIVSUFSORT_EXPORT "__declspec(dllexport)")
  endif(HAVE___DECLSPEC_DLLEXPORT_)
endif(BUILD_SHARED_LIBS)
set(W64BIT "")
set(SAINDEX_TYPE "${SAINT32_TYPE}")
set(SAINDEX_PRId "${SAINT32_PRId}")
set(SAINT_PRId "${SAINT32_PRId}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/divsufsort.h.cmake"
               "${CMAKE_CURRENT_BINARY_DIR}/divsufsort.h" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/divsufsort.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
if(BUILD_DIVSUFSORT64)
  set(W64BIT "64")
  set(SAINDEX_TYPE "${SAINT64_TYPE}")
  set(SAINDEX_PRId "${SAINT64_PRId}")
  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/divsufsort.h.cmake"
                 "${CMAKE_CURRENT_BINARY_DIR}/divsufsort64.h" @ONLY)
  install(FILES "${CMAKE_CURRENT_BINARY_DIR}/divsufsort64.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif(BUILD_DIVSUFSORT64)
