# -*- cmake -*-

SET(QLUA_VERSION 0.1)
SET(QLUA_DEFINITIONS)
SET(QLUA_INCLUDE_DIR 
  "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")

#FIND_PACKAGE(Lua REQUIRED)

ADD_DEFINITIONS(${QLUA_DEFINITIONS} ${QTLUA_DEFINITIONS} ${LUA_DEFINITIONS})
INCLUDE_DIRECTORIES(${QLUA_INCLUDE_DIR} ${QTLUA_INCLUDE_DIR} ${LUA_INCLUDE_DIR})
INCLUDE(${QT_USE_FILE})		     

# -- checks

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckSymbolExists)
INCLUDE(CheckFunctionExists)

IF (NOT WITHOUT_READLINE)
  FIND_PACKAGE(Readline)
  IF(READLINE_FOUND)
    SET(HAVE_READLINE 1)
    INCLUDE_DIRECTORIES(${READLINE_INCLUDE_DIR})
    SET(CMAKE_REQUIRED_INCLUDES ${READLINE_INCLUDE_DIR})
    SET(CMAKE_REQUIRED_LIBRARIES ${READLINE_LIBRARIES})
    CHECK_SYMBOL_EXISTS(rl_completion_matches 
                        "stdio.h;readline/readline.h" HAVE_RL_COMPLETION_MATCHES)
    SET(CMAKE_REQUIRED_INCLUDES)
    SET(CMAKE_REQUIRED_LIBRARIES)
  ENDIF(READLINE_FOUND)
ENDIF (NOT WITHOUT_READLINE)
  
IF (WIN32 AND NOT UNIX)
  CHECK_INCLUDE_FILES(io.h HAVE_IO_H)
  CHECK_SYMBOL_EXISTS(_cgets_s "conio.h" HAVE_CGETS_S)
ELSE (WIN32 AND NOT UNIX)
  CHECK_INCLUDE_FILES(fcntl.h HAVE_FCNTL_H)
  CHECK_INCLUDE_FILES(pthread.h HAVE_PTHREAD_H)
  CHECK_INCLUDE_FILES(signal.h HAVE_SIGNAL_H)
  CHECK_INCLUDE_FILES(unistd.h HAVE_UNISTD_H)
  CHECK_INCLUDE_FILES(errno.h HAVE_ERRNO_H)
  CHECK_INCLUDE_FILES(sys/select.h HAVE_SYS_SELECT_H)
  CHECK_INCLUDE_FILES(sys/time.h HAVE_SYS_TIME_H)
  CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H)
  CHECK_SYMBOL_EXISTS(isatty "unistd.h" HAVE_ISATTY)
  CHECK_SYMBOL_EXISTS(sigprocmask "signal.h" HAVE_SIGPROCMASK)
  CHECK_SYMBOL_EXISTS(sigaction "signal.h" HAVE_SIGACTION)
  CHECK_SYMBOL_EXISTS(signal "signal.h" HAVE_SIGNAL)
ENDIF(WIN32 AND NOT UNIX)

SET(CMAKE_REQUIRED_LIBRARIES ${QT_LIBRARIES})
CHECK_SYMBOL_EXISTS(pthread_sigmask "pthread.h;signal.h" HAVE_PTHREAD_SIGMASK)
CHECK_FUNCTION_EXISTS(XInitThreads HAVE_XINITTHREADS)
SET(CMAKE_REQUIRED_LIBRARIES)

# --- configure

CONFIGURE_FILE("qluaconf.h.in" "${CMAKE_CURRENT_BINARY_DIR}/qluaconf.h")
  
# --- Compile libqlua

SET(QLUA_LIBRARIES libqlua)

SET(libqlua_SRC 
  "qluaapplication.h" "qluaapplication.cpp"
  "qlua.qrc" "qluaconsole.h" )
IF (WIN32 AND NOT UNIX)
  SET(libqlua_SRC ${libqlua_SRC} "qluaconsole_win.cpp")
ELSE (WIN32 AND NOT UNIX)
  SET(libqlua_SRC ${libqlua_SRC} "qluaconsole_unix.cpp")
ENDIF(WIN32 AND NOT UNIX)

MACRO_QT4_AUTOGEN(libqlua_GEN ${libqlua_SRC})

ADD_LIBRARY(libqlua SHARED ${libqlua_SRC} ${libqlua_GEN})
SET_TARGET_PROPERTIES(libqlua PROPERTIES OUTPUT_NAME "qlua")
SET_TARGET_PROPERTIES(libqlua PROPERTIES PREFIX "lib" IMPORT_PREFIX "lib")

IF(APPLE)
  SET_TARGET_PROPERTIES(libqlua PROPERTIES COMPILE_FLAGS "-DAPPLE")
ENDIF(APPLE)

TARGET_LINK_LIBRARIES(libqlua 
  ${QTLUA_LIBRARIES} 
  ${LUA_LIBRARIES}
  ${READLINE_LIBRARIES} 
  ${QT_LIBRARIES})


# --- config for internal use

SET(QLUA_LIBRARIES "libqlua")
SET(QLUA_DEFINITIONS)
CONFIGURE_FILE(QLuaConfig.cmake.in 
  "${QtLua_INSTALL_FINDLUA_DIR}/QLuaConfig.cmake")
SET(QLua_DIR "${QtLua_INSTALL_FINDLUA_DIR}" CACHE PATH 
  "Directory containing QLuaConfig.cmake")
MARK_AS_ADVANCED(QLua_DIR)


# --- Compile qlua

IF(LUA_JIT AND APPLE)
  set ( CMAKE_EXE_LINKER_FLAGS "-pagezero_size 10000 -image_base 100000000 ${CMAKE_EXE_LINKER_FLAGS}" )
ENDIF(LUA_JIT AND APPLE)

SET(qlua_SRC "qlua.cpp")

ADD_EXECUTABLE(qlua ${qlua_SRC})

TARGET_LINK_LIBRARIES(qlua libqlua)

IF(Q_WS_X11)
  FIND_PACKAGE(X11 REQUIRED)
  TARGET_LINK_LIBRARIES(qlua ${X11_LIBRARIES})
ENDIF(Q_WS_X11)

IF(WITH_QTLUA_IDE AND WIN32)
  # winqlua has only the ide
  ADD_EXECUTABLE(winqlua WIN32 ${qlua_SRC} qlua.rc)
  TARGET_LINK_LIBRARIES(winqlua ${QLUA_LIBRARIES} ${QT_QTMAIN_LIBRARY})
  SET_TARGET_PROPERTIES(winqlua PROPERTIES COMPILE_FLAGS "-DWINQLUA")
  # wintorch loads torch-env with both console and ide
  ADD_EXECUTABLE(wintorch ${qlua_SRC} qlua.rc)  
  TARGET_LINK_LIBRARIES(wintorch ${QLUA_LIBRARIES} ${QT_QTMAIN_LIBRARY})
  SET_TARGET_PROPERTIES(wintorch PROPERTIES COMPILE_FLAGS "-DWINQLUA -DTORCH7")
ENDIF(WITH_QTLUA_IDE AND WIN32)

IF(WITH_QTLUA_IDE AND APPLE)
  ADD_EXECUTABLE(macqlua ${qlua_SRC})
  TARGET_LINK_LIBRARIES(macqlua ${QLUA_LIBRARIES})
  SET_TARGET_PROPERTIES(macqlua PROPERTIES COMPILE_FLAGS "-DMACQLUA")
ENDIF(WITH_QTLUA_IDE AND APPLE)

IF(APPLE)
  SET_TARGET_PROPERTIES(qlua PROPERTIES COMPILE_FLAGS "-DAPPLE")
ENDIF(APPLE)

# --- install


INSTALL(TARGETS libqlua qlua
  RUNTIME DESTINATION ${QtLua_INSTALL_BIN_SUBDIR} 
  LIBRARY DESTINATION ${QtLua_INSTALL_LIB_SUBDIR} 
  ARCHIVE DESTINATION ${QtLua_INSTALL_LIB_SUBDIR} )

IF (WITH_QTLUA_IDE AND WIN32)
  INSTALL(TARGETS winqlua
    RUNTIME DESTINATION ${QtLua_INSTALL_BIN_SUBDIR} 
    LIBRARY DESTINATION ${QtLua_INSTALL_LIB_SUBDIR} 
    ARCHIVE DESTINATION ${QtLua_INSTALL_LIB_SUBDIR} )
  INSTALL(TARGETS wintorch
    RUNTIME DESTINATION ${QtLua_INSTALL_BIN_SUBDIR} 
    LIBRARY DESTINATION ${QtLua_INSTALL_LIB_SUBDIR} 
    ARCHIVE DESTINATION ${QtLua_INSTALL_LIB_SUBDIR} )
ENDIF(WITH_QTLUA_IDE AND WIN32)

IF (WITH_QTLUA_IDE AND APPLE)
  OPTION(GENERATE_MACQLUA_BUNDLE 
    "Install a Mac OS bundle (set CMAKE_INSTALL_PREFIX=.../MacQLua.app/Contents)" ON)
  IF(GENERATE_MACQLUA_BUNDLE)
    CONFIGURE_FILE("qlua.plist.in" "${CMAKE_CURRENT_BINARY_DIR}/Info.plist")
    INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/Info.plist"
      DESTINATION "${QtLua_INSTALL_PREFIX}" 
      PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
    INSTALL(TARGETS macqlua 
      RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/MacOS" )
    INSTALL(FILES 
         "${CMAKE_CURRENT_SOURCE_DIR}/images/torch.icns"      
         "${CMAKE_CURRENT_SOURCE_DIR}/images/luadoc.icns"
         DESTINATION "${QtLua_INSTALL_PREFIX}/Resources" 
         PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
  ENDIF(GENERATE_MACQLUA_BUNDLE)
  INSTALL(TARGETS macqlua
    RUNTIME DESTINATION ${QtLua_INSTALL_BIN_SUBDIR} 
    LIBRARY DESTINATION ${QtLua_INSTALL_LIB_SUBDIR} 
    ARCHIVE DESTINATION ${QtLua_INSTALL_LIB_SUBDIR} )
ENDIF(WITH_QTLUA_IDE AND APPLE)

