# I've only tested this project with CMake 2.6.
# So for now this is the required version.
cmake_minimum_required(VERSION 2.6)

# The project's name.  This will be name of the solution file for
# Microsoft Visual Studio.
project(MonochromeWorlds)

# Find the `svnversion' command line program and use it in order to
# get the local working directory's revision.  The revision will be
# used in the last component of the version string.
find_program(MERCURIAL_BIN NAMES hg.bat hg)
if(MERCURIAL_BIN)
    execute_process(COMMAND ${MERCURIAL_BIN} tip
                    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
                    OUTPUT_VARIABLE VERSION_REV)
    # Remove any extra version information, I only care about the
    # revision number.
    string(REGEX MATCH "^changeset:[ ]+[^:]+" VERSION_REV ${VERSION_REV})
    string(REGEX MATCH "[0-9]+" VERSION_REV ${VERSION_REV})
else(MERCURIAL_BIN)
    set(VERSION_REV "0")
endif(MERCURIAL_BIN)

# This is the application's version.
set(VERSION_MAJOR 1)
set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
# On non-Apple builds append the revision from Subversion as the last component.
# On Apple builds we'll know it from the bundle version.
if(NOT APPLE)
    set(VERSION "${VERSION}.${VERSION_REV}")
endif(NOT APPLE)

# Remove some warnings in MSVC.
if(MSVC)
    add_definitions(-D_SCL_SECURE_NO_WARNINGS)
endif(MSVC)

# Process subdirectories.
add_subdirectory(lib)
add_subdirectory(share)
add_subdirectory(src)

# Create the WiX source file and the MSI target.
if(WIN32)
    find_program(WIX_CANDLE candle)
    find_program(WIX_LIGHT light)
    if(WIX_CANDLE AND WIX_LIGHT)
        set(WIX_SOURCE_FILE ${CMAKE_CURRENT_BINARY_DIR}/monochromeworlds.wxs)
        set(WIX_OBJ_FILE monochromeworlds.wixobj)
        set(MSI_FILE MonochromeWorlds-${VERSION}.msi)
        
        configure_file(monochromeworlds.wxs.cmake ${WIX_SOURCE_FILE} @ONLY)
        add_custom_target(msi
                           ${WIX_CANDLE} -nologo -out ${WIX_OBJ_FILE} -ext WixUtilExtension -ext WixGamingExtension ${WIX_SOURCE_FILE}
                           COMMAND ${WIX_LIGHT} -nologo -out ${MSI_FILE} -dWixUILicenseRtf=${CMAKE_SOURCE_DIR}/COPYING.rtf -ext WixUtilExtension -ext WixUIExtension -ext WixGamingExtension -cultures:en-us ${WIX_OBJ_FILE}
                           DEPENDS monochromeworlds
                           WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
                           COMMENT "Generating MSI database ${MSI_FILE}")
    else(WIX_CANDLE AND WIX_LIGHT)
        message(STATUS "candle and/or light couldn't be found.  Skipping MSI target")
    endif(WIX_CANDLE AND WIX_LIGHT)
endif(WIN32)
