CMake_Minimum_Required (VERSION 3.10)
Project (alchemyquest)

#Path options
Set (BINDIR "bin" CACHE STRING "Where to install binaries")
Set (DATAROOTDIR "${CMAKE_INSTALL_PREFIX}/share" CACHE STRING "Sets the root of data directories to a non-default location")
Set (ICONDIR "${DATAROOTDIR}/icons" CACHE STRING "Sets the icon directory for desktop entry to a non-default location.")
Set (DESKTOPDIR "${DATAROOTDIR}/applications" CACHE STRING "Sets the desktop file directory for desktop entry to a non-default location.")

add_definitions(-DDATA_DIR=\"${DATAROOTDIR}/openalchemist\")

INCLUDE(FindPkgConfig)

#Find the required libraries.
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2_IMAGE REQUIRED SDL2_image)
PKG_SEARCH_MODULE(SDL2_MIXER REQUIRED SDL2_mixer)
PKG_SEARCH_MODULE(EXPAT REQUIRED expat)
PKG_SEARCH_MODULE(LIBZIP REQUIRED libzip)

# Find LibGamerzilla library
if (NOT APPLE)
    pkg_search_module(GAMERZILLA OPTIONAL gamerzilla)

    if (GAMERZILLA_LIBRARIES)
        message(STATUS "Gamerzilla found")
        add_definitions(-DHAVE_GAMERZILLA)
    endif()
endif()


#Add the include directories of the (found) libraries.
Include_Directories(
	${SDL2_INCLUDE_DIRS}
	${SDL2_IMAGE_INCLUDE_DIRS}
	${SDL2_MIXER_INCLUDE_DIRS}
	${EXPAT_INCLUDE_DIRS}
	${LIBZIP_INCLUDE_DIRS}
	${GAMERZILLA_INCLUDE_DIRS}
)

#Set the output path and the source path.
Set (EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
Set (SRC_DIR ${PROJECT_SOURCE_DIR}/src)

#List the source files.
File (GLOB SOURCES ${SRC_DIR}/*.cpp ${SRC_DIR}/*/*.cpp ${SRC_DIR}/*/*/*.cpp)

Add_Executable (${PROJECT_NAME} ${SOURCES})

if ("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
	Set (BOOST_LIBRARIES "-lboost_filesystem -lboost_system")
endif ("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
if(MINGW)
	Set (BOOST_LIBRARIES "-lboost_filesystem-mt-x32 -lboost_system-mt-x32")
	SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} -mwindows")
endif()

Target_Link_Libraries (
	${PROJECT_NAME}
	${SDL2_LIBRARIES}
	${SDL2_IMAGE_LIBRARIES}
	${SDL2_MIXER_LIBRARIES}
	${SDL2MAIN_LIBRARIES}
	${EXPAT_LIBRARIES}
	${LIBZIP_LIBRARIES}
	${GAMERZILLA_LIBRARIES}
	${BOOST_LIBRARIES}
)

function(create_zip output_file input_files working_dir)
    add_custom_command(
        COMMAND ${CMAKE_COMMAND} -E tar "cf" "${output_file}" --format=zip -- ${input_files}
        WORKING_DIRECTORY "${working_dir}"
        OUTPUT  "${output_file}"
        DEPENDS ${input_files}
        COMMENT "Zipping to ${output_file}."
    )
endfunction()

file(GLOB AQUA_ZIP_FILES "${PROJECT_SOURCE_DIR}/skins/aqua/*")
create_zip("${PROJECT_SOURCE_DIR}/skins/aqua.zip" "${AQUA_ZIP_FILES}" "${PROJECT_SOURCE_DIR}/skins/aqua")

file(GLOB BRUSHED_ZIP_FILES "${PROJECT_SOURCE_DIR}/skins/brushed/*")
create_zip("${PROJECT_SOURCE_DIR}/skins/brushed.zip" "${BRUSHED_ZIP_FILES}" "${PROJECT_SOURCE_DIR}/skins/brushed")

file(GLOB VECTORIEL_ZIP_FILES "${PROJECT_SOURCE_DIR}/skins/vectoriel/*")
create_zip("${PROJECT_SOURCE_DIR}/skins/vectoriel.zip" "${VECTORIEL_ZIP_FILES}" "${PROJECT_SOURCE_DIR}/skins/vectoriel")
add_custom_target("openalchemist-data" ALL DEPENDS "${PROJECT_SOURCE_DIR}/skins/aqua.zip" "${PROJECT_SOURCE_DIR}/skins/brushed.zip" "${PROJECT_SOURCE_DIR}/skins/vectoriel.zip")

file(GLOB AQUA2_ZIP_FILES "${PROJECT_SOURCE_DIR}/alchemyquest-skins/aqua/*")
create_zip("${PROJECT_SOURCE_DIR}/alchemyquest-skins/aqua.zip" "${AQUA2_ZIP_FILES}" "${PROJECT_SOURCE_DIR}/alchemyquest-skins/aqua")

file(GLOB BALL_ZIP_FILES "${PROJECT_SOURCE_DIR}/alchemyquest-elements/ball/*")
create_zip("${PROJECT_SOURCE_DIR}/alchemyquest-elements/ball.zip" "${BALL_ZIP_FILES}" "${PROJECT_SOURCE_DIR}/alchemyquest-elements/ball")

file(GLOB POTION_ZIP_FILES "${PROJECT_SOURCE_DIR}/alchemyquest-elements/potion/*")
create_zip("${PROJECT_SOURCE_DIR}/alchemyquest-elements/potion.zip" "${POTION_ZIP_FILES}" "${PROJECT_SOURCE_DIR}/alchemyquest-elements/potion")

file(GLOB ORB_ZIP_FILES "${PROJECT_SOURCE_DIR}/alchemyquest-elements/orb/*")
create_zip("${PROJECT_SOURCE_DIR}/alchemyquest-elements/orb.zip" "${ORB_ZIP_FILES}" "${PROJECT_SOURCE_DIR}/alchemyquest-elements/orb")
add_custom_target("alchemyquest-data" ALL DEPENDS "${PROJECT_SOURCE_DIR}/alchemyquest-skins/aqua.zip" "${PROJECT_SOURCE_DIR}/alchemyquest-elements/ball.zip" "${PROJECT_SOURCE_DIR}/alchemyquest-elements/potion.zip" "${PROJECT_SOURCE_DIR}/alchemyquest-elements/orb.zip")

#Install locations
Install (DIRECTORY ${PROJECT_SOURCE_DIR}/data DESTINATION ${DATAROOTDIR}/alchemyquest/)
Install (DIRECTORY ${PROJECT_SOURCE_DIR}/music DESTINATION ${DATAROOTDIR}/alchemyquest/)
Install (DIRECTORY ${PROJECT_SOURCE_DIR}/sounds DESTINATION ${DATAROOTDIR}/alchemyquest/)
Install (DIRECTORY ${PROJECT_SOURCE_DIR}/skins DESTINATION ${DATAROOTDIR}/alchemyquest/ FILES_MATCHING PATTERN "*.zip" PATTERN aqua EXCLUDE PATTERN brushed EXCLUDE PATTERN vectoriel EXCLUDE)
Install (DIRECTORY ${PROJECT_SOURCE_DIR}/alchemyquest-skins DESTINATION ${DATAROOTDIR}/alchemyquest/ FILES_MATCHING PATTERN "*.zip" PATTERN aqua EXCLUDE)
Install (DIRECTORY ${PROJECT_SOURCE_DIR}/alchemyquest-elements DESTINATION ${DATAROOTDIR}/alchemyquest/ FILES_MATCHING PATTERN "*.zip" PATTERN ball EXCLUDE PATTERN potion EXCLUDE PATTERN orb EXCLUDE)
Install (DIRECTORY ${PROJECT_SOURCE_DIR}/icons/16x16 DESTINATION ${ICONDIR}/hicolor/)
Install (DIRECTORY ${PROJECT_SOURCE_DIR}/icons/32x32 DESTINATION ${ICONDIR}/hicolor/)
Install (DIRECTORY ${PROJECT_SOURCE_DIR}/icons/48x48 DESTINATION ${ICONDIR}/hicolor/)
Install (TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${BINDIR})
Install (FILES ${PROJECT_SOURCE_DIR}/CODE-LICENSE ${PROJECT_SOURCE_DIR}/GRAPHICS-LICENSE DESTINATION ${DATAROOTDIR}/alchemyquest/)

if ("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
	Install (FILES data/logo_svg.svg DESTINATION ${ICONDIR}/hicolor/scalable/apps/ RENAME openalchemist.svg)
endif ("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
