cmake_minimum_required(VERSION 2.4 FATAL_ERROR)

# Set build type. Do this *before* we set the project name
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
      "Choose the type of build, options are: None Debug Release RelWithDebInfo Profile."
      FORCE)
endif(NOT CMAKE_BUILD_TYPE)

set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}" CACHE INTERNAL "internal")

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

project(EggDialog C)

# Search in the `cmake' directory for additional CMake modules.
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

include(CMakeDependentOption)

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
if (UNIX)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe")
endif(UNIX)

option(WANT_WARN   "Enable compiler warnings" on)
option(WANT_NATIVE "Optimise for the current machine" off)
option(WANT_ASSERT "Enable assertions in the code (for debugging)" off)
option(WANT_LTO    "Link time optimisation on/off" on)
option(WANT_OPT    "Standard optimisations on/off" on)
option(WANT_FAST   "Agressive optimisations on/off" on)
option(WANT_SSE41  "Switch on SSE 4.1" off)
option(WANT_SSE42  "Switch on SSE 4.2" on)
option(WANT_SSE3   "Switch on SSE 3" off)
option(WANT_SSE2   "Switch on SSE 2" off)
option(WANT_32BIT  "Force compiler to generate 32 bit code" off)
option(WANT_64BIT  "Force compiler to generate 64 bit code" off)
cmake_dependent_option(WANT_WERROR "Treat warnings as errors" on "WANT_WARN" on)
option(WANT_STATIC  "Wether you want to static link to standard libraries" off)
option(WANT_PROFILE "Generate executable suitable for running through a profiler" off)
option(WANT_PROFILE_GENERATE "Generate executable suitable for running profile-guided optimisation" off)
option(WANT_PROFILE_USE "Generate executable using data from profile run" off)


if(WANT_WARN)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Winline -Wno-deprecated-declarations -Wuninitialized")
endif(WANT_WARN)
if(WANT_OPT)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -finline")
endif(WANT_OPT)
if(WANT_LTO)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto")
   set (CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -flto")
endif(WANT_LTO)
if(WANT_WERROR)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
endif(WANT_WERROR)
if(WANT_NATIVE)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native -mtune=native")
endif(WANT_NATIVE)
if(WANT_FAST)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fast")
endif(WANT_FAST)
if(WANT_SSE41)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.1 -march=core2")
endif(WANT_SSE41)
if(WANT_SSE42)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2")
endif(WANT_SSE42)
if(WANT_SSE3)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse3")
endif(WANT_SSE3)
if(WANT_SSE2)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2")
endif(WANT_SSE2)
if(WANT_PROFILE_GENERATE)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg -fprofile-generate")
   set (CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -pg -fprofile-generate")
endif(WANT_PROFILE_GENERATE)
if(WANT_PROFILE_USE)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-use")
   set (CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -fprofile-use")
endif(WANT_PROFILE_USE)
if(WANT_ASSERT)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUGMODE")
endif(WANT_ASSERT)
if(WANT_32BIT)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
endif(WANT_32BIT)
if(WANT_64BIT)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
endif(WANT_64BIT)
if(WANT_STATIC)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static")
endif(WANT_STATIC)
if(WANT_PROFILE)
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg -g")
endif(WANT_PROFILE)


# Search for C header files in these directories.
include_directories(
   ${CMAKE_SOURCE_DIR}/include
   ${CMAKE_BINARY_DIR}/include
)

# Find zlib
find_package(ZLIB)
if (ZLIB_FOUND)
   list(APPEND PLATFORM_LIBS ${ZLIB_LIBRARIES})
   include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS})
else (ZLIB_FOUND)
   message(FATAL_ERROR "Can't find zlib library")
endif(ZLIB_FOUND)


# Find Allegro
set(ALLEGRO5_VERSION "5")
find_package(Allegro5)

if (ALLEGRO5_FOUND)
   list(APPEND PLATFORM_LIBS ${ALLEGRO5_LIBRARIES})
   include_directories(SYSTEM ${ALLEGRO5_INCLUDE_DIRS})
else (ALLEGRO5_FOUND)
   message(FATAL_ERROR "Can't find Allegro library")
endif(ALLEGRO5_FOUND)

# Find Allegro addons
find_package(Allegro5Main)
if (ALLEGRO5MAIN_FOUND)
   list(APPEND PLATFORM_LIBS ${ALLEGRO5MAIN_LIBRARIES})
   include_directories(SYSTEM ${ALLEGRO5MAIN_INCLUDE_DIRS})
else (ALLEGRO5MAIN_FOUND)
   message(FATAL_ERROR "Can't find Allegro main addon")
endif(ALLEGRO5MAIN_FOUND)

find_package(Allegro5Primitives)
if (ALLEGRO5PRIMITIVES_FOUND)
   list(APPEND PLATFORM_LIBS ${ALLEGRO5PRIMITIVES_LIBRARIES})
   include_directories(SYSTEM ${ALLEGRO5PRIMITIVES_INCLUDE_DIRS})
else (ALLEGRO5PRIMITIVES_FOUND)
   message(FATAL_ERROR "Can't find Allegro Primitives addon")
endif(ALLEGRO5PRIMITIVES_FOUND)

find_package(Allegro5Font)
if (ALLEGRO5FONT_FOUND)
   list(APPEND PLATFORM_LIBS ${ALLEGRO5FONT_LIBRARIES})
   include_directories(SYSTEM ${ALLEGRO5FONT_INCLUDE_DIRS})
else (ALLEGRO5FONT_FOUND)
   message(FATAL_ERROR "Can't find Allegro font addon")
endif(ALLEGRO5FONT_FOUND)

find_package(Allegro5Audio)
if (ALLEGRO5AUDIO_FOUND)
   list(APPEND PLATFORM_LIBS ${ALLEGRO5AUDIO_LIBRARIES})
   include_directories(SYSTEM ${ALLEGRO5AUDIO_INCLUDE_DIRS})
else (ALLEGRO5AUDIO_FOUND)
   message(FATAL_ERROR "Can't find Allegro audio addon")
endif(ALLEGRO5AUDIO_FOUND)

find_package(Allegro5AudioCodec)
if (ALLEGRO5AUDIOCODEC_FOUND)
   list(APPEND PLATFORM_LIBS ${ALLEGRO5AUDIOCODEC_LIBRARIES})
   include_directories(SYSTEM ${ALLEGRO5AUDIOCODEC_INCLUDE_DIRS})
else (ALLEGRO5AUDIOCODEC_FOUND)
   message(FATAL_ERROR "Can't find Allegro codec addon")
endif(ALLEGRO5AUDIOCODEC_FOUND)

find_package(Allegro5TTF)
if (ALLEGRO5TTF_FOUND)
   list(APPEND PLATFORM_LIBS ${ALLEGRO5TTF_LIBRARIES})
   include_directories(SYSTEM ${ALLEGRO5TTF_INCLUDE_DIRS})
else (ALLEGRO5TTF_FOUND)
   message(FATAL_ERROR "Can't find Allegro TTF addon")
endif(ALLEGRO5TTF_FOUND)

find_package(Allegro5Image)
if (ALLEGRO5IMAGE_FOUND)
   list(APPEND PLATFORM_LIBS ${ALLEGRO5IMAGE_LIBRARIES})
   include_directories(SYSTEM ${ALLEGRO5IMAGE_INCLUDE_DIRS})
else (ALLEGRO5IMAGE_FOUND)
   message(FATAL_ERROR "Can't find Allegro image addon")
endif(ALLEGRO5IMAGE_FOUND)

find_package(Allegro5Dialog)
if (ALLEGRO5DIALOG_FOUND)
   list(APPEND PLATFORM_LIBS ${ALLEGRO5DIALOG_LIBRARIES})
   include_directories(SYSTEM ${ALLEGRO5DIALOG_INCLUDE_DIRS})
else (ALLEGRO5DIALOG_FOUND)
   message(FATAL_ERROR "Can't find Allegro DIALOG addon")
endif(ALLEGRO5DIALOG_FOUND)

find_package(OpenGL)
if(OPENGL_FOUND)
   list(APPEND PLATFORM_LIBS ${OPENGL_LIBRARIES})
else(OPENGL_FOUND)
   message(FATAL_ERROR "Can't find OpenGL development libraries")
endif(OPENGL_FOUND)

# code source files
include(FileList)

add_library ("eggdialog" STATIC ${DIALOG_SRC_FILES})
set_target_properties(eggdialog PROPERTIES COMPILE_FLAGS "-fPIC" )

add_executable ("dialog_test" src/dialog_test.c)
target_link_libraries("dialog_test" ${PLATFORM_LIBS} eggdialog)

add_executable ("hello_dialog" src/hello_dialog.c)
target_link_libraries("hello_dialog" ${PLATFORM_LIBS} eggdialog)

install (TARGETS "eggdialog" ARCHIVE DESTINATION "lib")
install (FILES "${CMAKE_SOURCE_DIR}/include/egg_dialog/egg_dialog.h" DESTINATION "include/egg_dialog/")
install (FILES "${CMAKE_SOURCE_DIR}/include/egg_dialog/nine_patch.h" DESTINATION "include/egg_dialog/")
