# SDL is required in order to build and run.
find_package(SDL REQUIRED)
include_directories(${SDL_INCLUDE_DIR})
find_package(SDL_mixer REQUIRED)
include_directories(${SDLMIXER_INCLUDE_DIR})

# And Boost.
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIR})

# Add benzaiten's source directory as an include directory.
include_directories(${CMAKE_SOURCE_DIR}/lib/benzaiten/src)

# This is the information stored in Info.plist for Mac OS X.
set(MACOSX_BUNDLE_INFO_STRING "The White and Black Puzzle Game")
set(MACOSX_BUNDLE_ICON_FILE "monochromeworlds.icns")
set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.geishastudios.monochromeworlds")
set(MACOSX_BUNDLE_LONG_VERSION_STRING "${VERSION}")
set(MACOSX_BUNDLE_BUNDLE_NAME "Monochrome Worlds")
set(MACOSX_BUNDLE_SHORT_VERSION_STRING "${VERSION}")
set(MACOSX_BUNDLE_BUNDLE_VERSION "${VERSION_REV}")
set(MACOSX_BUNDLE_COPYRIGHT "Copyright (c) 2008, 2009 Geisha Studios")

# The application's source for all platforms.
set(monochromeworlds_HEADERS
    GameAction.hpp
    MonochromeWorldsSettings.hpp
    Stage.hpp
    StageClearState.hpp
    StageLabel.hpp
    StageMenuState.hpp
    StageSelectState.hpp
    StageState.hpp
    StageView.hpp
    TitleState.hpp
    Undo.hpp
    )
set(monochromeworlds_SOURCES
    main.cpp
    MonochromeWorldsSettings.cpp
    Stage.cpp
    StageClearState.cpp
    StageLabel.cpp
    StageMenuState.cpp
    StageSelectState.cpp
    StageState.cpp
    StageView.cpp
    TitleState.cpp
    Undo.cpp
    )

# Mac OS X sources.
if(APPLE)
    list(APPEND monochromeworlds_HEADERS MonochromeWorlds.h)
    list(APPEND monochromeworlds_SOURCES MonochromeWorlds.m)
endif(APPLE)

# Windows sources.
if(WIN32)
    # Generate the resource file (rc) from the CMake template (rc.cmake)
    set(win32_rc_file ${CMAKE_CURRENT_BINARY_DIR}/monochromeworlds.rc)
    configure_file(monochromeworlds.rc.cmake ${win32_rc_file} @ONLY)
    # Under MinGW we need to compile it manually.
    if(MINGW)
        set(win32_res_file ${CMAKE_CURRENT_BINARY_DIR}/monochromeworlds_res.obj)
        add_custom_command(OUTPUT ${win32_res_file}
                           COMMAND windres.exe -I${CMAKE_CURRENT_SOURCE_DIR}
                                               -o${win32_res_file}
                                                ${win32_rc_file})
        set_source_files_properties(${win32_res_file} PROPERTIES GENERATED ON)
    endif(MINGW)
    list(APPEND monochromeworlds_SOURCES ${win32_res_file} ${win32_rc_file})
endif(WIN32)

# Tell that the header files are actually headers.
set_source_files_properties(${monochomeworlds_HEADERS}
    PROPERTIES HEADER_FILE_ONLY ON)

# Create the config.h file, add this directory to the include directories in
# order to use find it, and tell the compiler to use it.
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake
               ${CMAKE_CURRENT_BINARY_DIR}/config.h @ONLY)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_definitions(-DHAVE_CONFIG_H)

# Add the executable creating a bundle in Mac OS X and a GUI application
# in Windows.
add_executable(monochromeworlds WIN32 MACOSX_BUNDLE
    ${monochromeworlds_HEADERS}
    ${monochromeworlds_SOURCES})

# Tell which libraries need to link against.
target_link_libraries(monochromeworlds benzaiten)

# Make the executable be .gpe in GP2X
if(GP2X)
    set_target_properties(monochromeworlds PROPERTIES SUFFIX ".gpe")
endif(GP2X)

# With MinGW we need to strip the debug information away from the executable.
if(MINGW AND "${CMAKE_BUILD_TYPE}" MATCHES Release)
    # USe the strip program from MinGW.
    find_program(STRIP_BIN strip)
    if(STRIP_BIN)
        # Get where the executable will be located.
        get_target_property(MONOCHROMEWORLDS_EXE_LOCATION monochromeworlds LOCATION)
        add_custom_command(TARGET monochromeworlds POST_BUILD
            COMMAND ${STRIP_BIN} ARGS -S ${MONOCHROMEWORLDS_EXE_LOCATION}
            COMMENT "Removing debug data from CXX executable monochromeworlds.exe"
            )
    endif(STRIP_BIN)
endif(MINGW AND "${CMAKE_BUILD_TYPE}" MATCHES Release)

if(APPLE)
    set(BUNDLE_CONTENTS_DIR
        ${CMAKE_CURRENT_BINARY_DIR}/monochromeworlds.app/Contents)
    # Mac OS X needs the Frameworks inside the bundle's framework directory.
    list(GET SDL_LIBRARY 0 SDL_FRAMEWORK)
    list(GET SDLMIXER_LIBRARY 0 SDLMIXER_FRAMEWORK)

    set(FRAMEWORKS_DIR ${BUNDLE_CONTENTS_DIR}/Frameworks)
    # FindSDL* always puts the framework first in the library's list.
    add_custom_command(TARGET monochromeworlds POST_BUILD
        # SDL
        COMMAND ${CMAKE_COMMAND} ARGS -E make_directory ${FRAMEWORKS_DIR}/SDL.framework/Versions/A
        COMMAND ${CMAKE_COMMAND} ARGS -E copy ${SDL_FRAMEWORK}/Versions/Current/SDL ${FRAMEWORKS_DIR}/SDL.framework/Versions/A
        COMMAND ${CMAKE_COMMAND} ARGS -E create_symlink A ${FRAMEWORKS_DIR}/SDL.framework/Versions/Current
        COMMAND ${CMAKE_COMMAND} ARGS -E create_symlink Versions/Current/SDL ${FRAMEWORKS_DIR}/SDL.framework/SDL
        # SDL_mixer
        COMMAND ${CMAKE_COMMAND} ARGS -E make_directory ${FRAMEWORKS_DIR}/SDL_mixer.framework/Versions/A
        COMMAND ${CMAKE_COMMAND} ARGS -E copy ${SDLMIXER_FRAMEWORK}/Versions/Current/SDL_mixer ${FRAMEWORKS_DIR}/SDL_mixer.framework/Versions/A
        COMMAND ${CMAKE_COMMAND} ARGS -E create_symlink A ${FRAMEWORKS_DIR}/SDL_mixer.framework/Versions/Current
        COMMAND ${CMAKE_COMMAND} ARGS -E create_symlink Versions/Current/SDL_mixer ${FRAMEWORKS_DIR}/SDL_mixer.framework/SDL_mixer
        COMMENT "Copying frameworks into the bundle's private directory")



    # We also need to create the `Resources' directory in the application's
    # bundle.  In there we'll create the individual resource directories
    # and then we'll copy the actual resources files from the source directory.
    set(RESOURCES_DIR ${BUNDLE_CONTENTS_DIR}/Resources)

    # Graphic resources.
    set(GFX_DIR ${RESOURCES_DIR}/gfx)
    foreach(GFX_FILE ${GFX_FILES}) # from gfx's CMakeLists.txt file
        get_filename_component(FILENAME ${GFX_FILE} NAME)
        add_custom_command(TARGET monochromeworlds POST_BUILD
            COMMAND ${CMAKE_COMMAND} ARGS -E make_directory ${GFX_DIR}
            COMMAND ${CMAKE_COMMAND} ARGS -E copy ${GFX_FILE} ${GFX_DIR}
            COMMENT "Copying graphic file ${FILENAME} into resources directory")
    endforeach(GFX_FILE)
    # as well as the icon.
    add_custom_command(TARGET monochromeworlds POST_BUILD
        COMMAND ${CMAKE_COMMAND} ARGS -E copy ${CMAKE_SOURCE_DIR}/share/monochromeworlds/gfx/monochromeworlds.icns ${RESOURCES_DIR}
        COMMENT "Copying icons file monochromeworlds.icns into resources directory")

    # Stage resources.
    set(STAGES_DIR ${RESOURCES_DIR}/stages)
    foreach(STAGE_FILE ${STAGE_FILES}) # from stages' CMakeLists.txt file.
        get_filename_component(FILENAME ${STAGE_FILE} NAME)
        add_custom_command(TARGET monochromeworlds POST_BUILD
            COMMAND ${CMAKE_COMMAND} ARGS -E make_directory ${STAGES_DIR}
            COMMAND ${CMAKE_COMMAND} ARGS -E copy ${STAGE_FILE} ${STAGES_DIR}
            COMMENT "Copying stage file ${FILENAME} into resources directory")
    endforeach(STAGE_FILE)

    # Music resources.
    set(MUSIC_DIR ${RESOURCES_DIR}/music)
    foreach(MUSIC_FILE ${MUSIC_FILES}) # from music's CMakeLists.txt file.
        get_filename_component(FILENAME ${MUSIC_FILE} NAME)
        add_custom_command(TARGET monochromeworlds POST_BUILD
            COMMAND ${CMAKE_COMMAND} ARGS -E make_directory ${MUSIC_DIR}
            COMMAND ${CMAKE_COMMAND} ARGS -E copy ${MUSIC_FILE} ${MUSIC_DIR}
            COMMENT "Copying music file ${FILENAME} into resources directory")
    endforeach(MUSIC_FILE)

    # Sound resources.
    set(SFX_DIR ${RESOURCES_DIR}/sfx)
    foreach(SFX_FILE ${SFX_FILES}) # from sfx's CMakeLists.txt file.
        get_filename_component(FILENAME ${SFX_FILE} NAME)
        add_custom_command(TARGET monochromeworlds POST_BUILD
            COMMAND ${CMAKE_COMMAND} ARGS -E make_directory ${SFX_DIR}
            COMMAND ${CMAKE_COMMAND} ARGS -E copy ${SFX_FILE} ${SFX_DIR}
            COMMENT "Copying sound file ${FILENAME} into resources directory")
    endforeach(SFX_FILE)
endif(APPLE)

# Install the binary to the bin directory.
install(TARGETS monochromeworlds RUNTIME DESTINATION bin
                                 BUNDLE DESTINATION Applications)
