# cmake-build-system for blinkensisters
#
# Created by Wolfgang Dautermann
#
# Usage:
# cd cmake-build  # change to the build directory
# cmake ..
# make
# make install

# Does not work with 2.4.2 (see my comment below...)
CMAKE_MINIMUM_REQUIRED(VERSION 2.4.3)
PROJECT(blinkensisters)

if(COMMAND cmake_policy)
	cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

INCLUDE_DIRECTORIES(. BlinkenBMF/ SDL_gfx/ BlinkenLUA/LuaBindings/headers/ BlinkenLUA/LuaMain/headers/ BlinkenLib/headers/)
# Fixes a problem on FreeBSD - SDL_stdinc.h includes iconv.h, but that is in 
# /usr/local/include/iconv.h, which is not searched for include files...
INCLUDE_DIRECTORIES(/usr/local/include)

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake-modules/)

OPTION( IS_OLPC    "Set to ON to default to OLPC options" OFF )
OPTION( USE_THEORA    "Set to ON to use theora code" OFF )
OPTION( USE_SDL_MIXER "Set to OFF to disable sound" ON )
OPTION( USE_SDL_NET   "Set to OFF to disable networking" ON )
OPTION( OPTIMIZE   "Set to ON to enable optimization" OFF )
OPTION( LIBRARIES_DEBUG "Build with library debug code" OFF)

IF(CMAKE_COMPILER_IS_GNUCC)
	ADD_DEFINITIONS("-Wall -ansi -pedantic")
ENDIF(CMAKE_COMPILER_IS_GNUCC)

IF(OPTIMIZE)
    ADD_DEFINITIONS("-O3")
ENDIF(OPTIMIZE)

IF(LIBRARIES_DEBUG)
	ADD_DEFINITIONS("-DLIBRARIES_DEBUG")
ENDIF(LIBRARIES_DEBUG)

FIND_PACKAGE(SDL REQUIRED)
IF(SDL_FOUND)
	MESSAGE( STATUS "SDL found: includes in ${SDL_INCLUDE_DIR}, library in ${SDL_LIBRARY}")
	INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR})
ELSE(SDL_FOUND)
	MESSAGE(FATAL_ERROR "SDL not found")
ENDIF(SDL_FOUND)

FIND_PACKAGE(SDL_image REQUIRED)
IF(SDLIMAGE_FOUND)
	MESSAGE( STATUS "SDL_image found: includes in ${SDLIMAGE_INCLUDE_DIR}, library in ${SDLIMAGE_LIBRARY}")
	INCLUDE_DIRECTORIES(${SDLIMAGE_INCLUDE_DIR})
ELSE(SDLIMAGE_FOUND)
	MESSAGE(FATAL_ERROR "SDL_image not found")
ENDIF(SDLIMAGE_FOUND)

FIND_PACKAGE(SDL_ttf REQUIRED)
IF(SDLTTF_FOUND)
	MESSAGE( STATUS "SDL_ttf found: includes in ${SDLTTF_INCLUDE_DIR}, library in ${SDLTTF_LIBRARY}")
	INCLUDE_DIRECTORIES(${SDLTTF_INCLUDE_DIR})
ELSE(SDLTTF_FOUND)
	MESSAGE(FATAL_ERROR "SDL_ttf not found")
ENDIF(SDLTTF_FOUND)

IF(NOT USE_SDL_MIXER)
	MESSAGE( STATUS "SDL_mixer disabled by command line option")
	ADD_DEFINITIONS(-DDISABLE_SOUND)
ELSE(NOT USE_SDL_MIXER)
	FIND_PACKAGE(SDL_mixer)
	IF(SDLMIXER_FOUND)
		MESSAGE( STATUS "SDL_mixer found: includes in ${SDLMIXER_INCLUDE_DIR}, library in ${SDLMIXER_LIBRARY}")
		INCLUDE_DIRECTORIES(${SDLMIXER_INCLUDE_DIR})
		ADD_DEFINITIONS(-DHAVE_SDLMIXER)
	ELSE(SDLMIXER_FOUND)
		MESSAGE( STATUS "SDL_mixer not found. The program will be compiled without sound support.")
		ADD_DEFINITIONS(-DDISABLE_SOUND)
	ENDIF(SDLMIXER_FOUND)
ENDIF(NOT USE_SDL_MIXER)


IF(NOT USE_SDL_NET)
	MESSAGE( STATUS "SDL_net disabled by command line option")
	ADD_DEFINITIONS(-DDISABLE_NETWORK)
ELSE(NOT USE_SDL_NET)
	FIND_PACKAGE(SDL_net)
	IF(SDLNET_FOUND)
		MESSAGE( STATUS "SDL_net found: includes in ${SDLNET_INCLUDE_DIR}, library in ${SDLNET_LIBRARY}")
		INCLUDE_DIRECTORIES(${SDLNET_INCLUDE_DIR})
		ADD_DEFINITIONS(-DHAVE_SDLNET)
	ELSE(SDLNET_FOUND)
		MESSAGE(STATUS "SDL_net not found. The program will be compiled without networking support.")
		ADD_DEFINITIONS(-DDISABLE_NETWORK)
	ENDIF(SDLNET_FOUND)
ENDIF(NOT USE_SDL_NET)


ADD_DEFINITIONS(-DRESPATH="\\"${CMAKE_INSTALL_PREFIX}/share/blinkensisters/\\"")

ADD_EXECUTABLE(bmfcompress BlinkenBMF/minilzo.cpp BlinkenBMF/bmfcompress.cpp BlinkenBMF/bmfconvert.cpp)
ADD_EXECUTABLE(bmfdecompress BlinkenBMF/minilzo.cpp BlinkenBMF/bmfdecompress.cpp BlinkenBMF/bmfconvert.cpp)

FILE(GLOB lostpixels_source  *.cpp SDL_gfx/*.cpp BlinkenLUA/LuaBindings/source/*.cpp BlinkenLUA/LuaMain/source/*.cpp  BlinkenBMF/minilzo.cpp BlinkenBMF/bmfconvert.cpp BlinkenLib/source/*.cpp)
FILE(GLOB lostpixels_header *.h BlinkenLUA/source/*.h BlinkenBMF/*.h SDL_gfx/*.h BlinkenLUA/LuaBindings/headers/*.h BlinkenLUA/LuaMain/headers/*.h BlinkenLib/headers/*.h)
FILE(GLOB fontfiles ${CMAKE_CURRENT_SOURCE_DIR}/BASEDATA/TTF/*.ttf)

#FILE(GLOB blinkenplayer_source BlinkenPlayer/*.cpp)
#FILE(GLOB blinkenplayer_header BlinkenPlayer/*.h osdef.h globals.h)

IF(APPLE)
    MESSAGE( STATUS "APPLE MACOSX found. Adding SDLMain.m/SDLMain.h to LostPixels")
    SET( lostpixels_source ${lostpixels_source} OSX/LostPixels/SDLMain.m )
    SET( lostpixels_header ${lostpixels_header} OSX/LostPixels/SDLMain.h )
    # Compile SDLMain.m with C Compiler (not C++ as Cmake does by default)
    SET_SOURCE_FILES_PROPERTIES(OSX/LostPixels/SDLMain.m PROPERTIES LANGUAGE C)
ENDIF(APPLE)

ADD_EXECUTABLE(LostPixels ${lostpixels_source} ${lostpixels_header})

IF(IS_OLPC)
	ADD_DEFINITIONS(-DOLPCBUILD)
ENDIF(IS_OLPC)

IF(USE_THEORA)
	FIND_PACKAGE(Theora)
	IF(THEORA_FOUND)
		ADD_DEFINITIONS(-DHASOGGSUPPORT)
		INCLUDE_DIRECTORIES(${THEORA_INCLUDE_DIR})
		TARGET_LINK_LIBRARIES(LostPixels ${THEORA_LIBRARY})
	ENDIF(THEORA_FOUND)
ENDIF(USE_THEORA)

TARGET_LINK_LIBRARIES(LostPixels ${SDL_LIBRARY})
TARGET_LINK_LIBRARIES(LostPixels ${SDLIMAGE_LIBRARY})
TARGET_LINK_LIBRARIES(LostPixels ${SDLTTF_LIBRARY})

IF(SDLNET_FOUND)
	TARGET_LINK_LIBRARIES(LostPixels ${SDLNET_LIBRARY})
	MESSAGE( STATUS "Will also build BLPViewers")
	ADD_SUBDIRECTORY("TOOLS/BLPViewer/")
ELSE(SDLNET_FOUND)
	MESSAGE( STATUS "SDL_net not found - will not build BLPViewers and not include network functionality.")
ENDIF(SDLNET_FOUND)

IF(SDLMIXER_FOUND)
	TARGET_LINK_LIBRARIES(LostPixels ${SDLMIXER_LIBRARY})
	MESSAGE( STATUS "Will also build BlinkenPlayer")
	ADD_SUBDIRECTORY("BlinkenPlayer/")
ELSE(SDLMIXER_FOUND)
	MESSAGE( STATUS "SDL_mixer not found - will not build BlinkenPlayer and not include sound support.")
ENDIF(SDLMIXER_FOUND)

# currently not used 
IF(THEORA_FOUND)
	TARGET_LINK_LIBRARIES(LostPixels ${THEORA_LIBRARY})
ENDIF(THEORA_FOUND)

GET_TARGET_PROPERTY(BMFCOMPRESS_EXE bmfcompress LOCATION)

# Does not work with cmake 2.4.2: /bin/sh: ./bmfcompress: No such file or directory
# Dont know why the correct Path is not used. Require at least 2.4.3...
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/basedata.bmf
                   COMMAND ${BMFCOMPRESS_EXE} META config ${CMAKE_CURRENT_BINARY_DIR}/basedata.bmf
                   DEPENDS ${BMFCOMPRESS_EXE}
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/BASEDATA
		   COMMENT "Building basedata.bmf"
)
ADD_CUSTOM_TARGET(basedata ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/basedata.bmf)

IF (UNIX)
  SET(CPACK_CMAKE_GENERATOR "Unix Makefiles")
  SET(CPACK_SOURCE_GENERATOR "TGZ")
  SET(CPACK_GENERATOR "TGZ")
  SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Blinkensisters - Hunt for the lost pixels")
  SET(CPACK_PACKAGE_VENDOR "The Blinkensisters Team")
  SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/DOC/README")
  SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/DOC/LICENSE")
  SET(CPACK_PACKAGE_NAME blinkensisters)
  SET(CPACK_SYSTEM_NAME ${CMAKE_SYSTEM_NAME})
  SET(CPACK_PACKAGE_VERSION_MAJOR "0")
  SET(CPACK_PACKAGE_VERSION_MINOR "5.3")
  SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}")
  SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION}")
  SET(CPACK_SOURCE_IGNORE_FILES "/.svn/;/cmake-build/")
  INCLUDE(CPack)
  INCLUDE(UseRPMTools OPTIONAL)
  IF(RPMTools_FOUND)
    RPMTools_ADD_RPM_TARGETS(${PROJECT_NAME} ${PROJECT_NAME}.spec.in)
  ENDIF(RPMTools_FOUND)
ENDIF (UNIX)

####################################################################
# INSTALL                                                          #
####################################################################
INSTALL(TARGETS LostPixels bmfcompress bmfdecompress RUNTIME DESTINATION bin )
INSTALL(FILES DOC/AUTHORS DOC/HOWTOPLAY DOC/INSTALL DOC/LICENSE DOC/README DESTINATION share/doc/blinkensisters )
INSTALL(FILES BASEDATA/GFX/splash.jpg DESTINATION share/blinkensisters )
INSTALL(FILES ${fontfiles} DESTINATION share/blinkensisters )
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/basedata.bmf DESTINATION share/blinkensisters )

#####################################
# uninstall - from the cmake-faq: http://www.cmake.org/Wiki/CMake_FAQ#Can_I_do_.22make_uninstall.22_with_CMake.3F
#####################################
CONFIGURE_FILE(
  "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
  "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
  IMMEDIATE @ONLY)

ADD_CUSTOM_TARGET(uninstall
  "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
