# Prevent in-source builds
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
	message(FATAL_ERROR "In-source builds prohibited. Call CMake from a build/<platform> directory.")
endif()

cmake_minimum_required(VERSION 3.7)

# Create superbuild project
project(antkeeper-superbuild VERSION "0.0.1")

# Setup package variables
set(PACKAGE_NAME ${PROJECT_NAME})
set(PACKAGE_VERSION ${PROJECT_VERSION})
set(PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
get_filename_component(PACKAGE_PLATFORM ${CMAKE_BINARY_DIR} NAME CACHE)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
	set(PACKAGE_BUILD_TYPE debug)
else()
	set(PACKAGE_BUILD_TYPE release)
endif()
set(PACKAGE_FILENAME ${PACKAGE_NAME}-${PACKAGE_VERSION}-${PACKAGE_PLATFORM})
set(PACKAGE_INSTALL_DIR ${CMAKE_SOURCE_DIR}/bin/${PACKAGE_BUILD_TYPE}/${PACKAGE_FILENAME})
set(PACKAGE_DIST_DIR ${CMAKE_SOURCE_DIR}/dist/${PACKAGE_BUILD_TYPE})

# Create output directories
file(MAKE_DIRECTORY ${PACKAGE_INSTALL_DIR})
file(MAKE_DIRECTORY ${PACKAGE_DIST_DIR})

# Check for architecture mismatches
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4")
	if (PACKAGE_PLATFORM MATCHES "64$")
		message(FATAL_ERROR "Compiler architecture is 32-bit but target architecture is 64-bit.")
	endif()
else()
	if (PACKAGE_PLATFORM MATCHES "32$")
		message(FATAL_ERROR "Compiler architecture is 64-bit but target architecture is 32-bit.")
	endif()
endif()

# Options
set(BLENDER "blender" CACHE FILEPATH "Path to Blender executable")
set(INKSCAPE "inkscape" CACHE FILEPATH "Path to Inkscape executable")

# Set C++ compiler flags
if(CMAKE_COMPILER_IS_GNUCC)
	set(CMAKE_CXX_FLAGS "-Wall -Wextra")
	set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g")
	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3")
elseif(MSVC)
	set(CMAKE_CXX_FLAGS "/W3 /MP /MT")
	set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS}")
	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} /Ox")
endif()

# Set C compiler flags
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")

# Setup module directories
set(MODULE_SOURCE_DIR "${PROJECT_SOURCE_DIR}/modules")
set(MODULE_BUILD_DIR "${PROJECT_BINARY_DIR}/modules/build")
set(MODULE_INSTALL_DIR "${CMAKE_BINARY_DIR}/modules/install")

# Include ExternalProject_Add macro
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)

# Build SDL2 module
ExternalProject_Add(SDL2
	SOURCE_DIR ${MODULE_SOURCE_DIR}/SDL2
	BINARY_DIR ${MODULE_BUILD_DIR}/SDL2
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
		"-DSDL_SHARED=OFF"
		"-DSDL_STATIC=ON"
		"-LIBC=ON"
		"-DHAVE_LIBC=ON"
		"-DFORCE_STATIC_VCRT=ON"
	BUILD_ALWAYS 0)

# Build OpenAL Soft module
ExternalProject_Add(openal-soft
	SOURCE_DIR ${MODULE_SOURCE_DIR}/openal-soft
	BINARY_DIR ${MODULE_BUILD_DIR}/openal-soft
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
		"-DLIBTYPE=STATIC"
		"-DFORCE_STATIC_VCRT=ON"
		"-DALSOFT_UTILS=OFF"
		"-DALSOFT_EXAMPLES=OFF"
		"-DALSOFT_TESTS=OFF"
	BUILD_ALWAYS 0)

# Build EnTT module
ExternalProject_Add(entt
	SOURCE_DIR ${MODULE_SOURCE_DIR}/entt
	BINARY_DIR ${MODULE_BUILD_DIR}/entt
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
	BUILD_ALWAYS 0)

# Build nlohmann-json module
ExternalProject_Add(nlohmann-json
	SOURCE_DIR ${MODULE_SOURCE_DIR}/nlohmann
	BINARY_DIR ${MODULE_BUILD_DIR}/nlohmann
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
	BUILD_ALWAYS 0)

# Build dr_wav module
ExternalProject_Add(dr_wav
	SOURCE_DIR ${MODULE_SOURCE_DIR}/dr_wav
	BINARY_DIR ${MODULE_BUILD_DIR}/dr_wav
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
		"-DCMAKE_C_FLAGS='${CMAKE_C_FLAGS}'"
		"-DCMAKE_C_FLAGS_DEBUG='${CMAKE_C_FLAGS_DEBUG}'"
		"-DCMAKE_C_FLAGS_RELEASE='${CMAKE_C_FLAGS_RELEASE}'"
	BUILD_ALWAYS 0)

# Build stb module
ExternalProject_Add(stb
	SOURCE_DIR ${MODULE_SOURCE_DIR}/stb
	BINARY_DIR ${MODULE_BUILD_DIR}/stb
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
		"-DCMAKE_C_FLAGS='${CMAKE_C_FLAGS}'"
		"-DCMAKE_C_FLAGS_DEBUG='${CMAKE_C_FLAGS_DEBUG}'"
		"-DCMAKE_C_FLAGS_RELEASE='${CMAKE_C_FLAGS_RELEASE}'"
	BUILD_ALWAYS 0)

# Build glad module
ExternalProject_Add(glad
	SOURCE_DIR ${MODULE_SOURCE_DIR}/glad
	BINARY_DIR ${MODULE_BUILD_DIR}/glad
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
		"-DCMAKE_C_FLAGS='${CMAKE_C_FLAGS}'"
		"-DCMAKE_C_FLAGS_DEBUG='${CMAKE_C_FLAGS_DEBUG}'"
		"-DCMAKE_C_FLAGS_RELEASE='${CMAKE_C_FLAGS_RELEASE}'"
	BUILD_ALWAYS 0)
	
# Build dirent module
ExternalProject_Add(dirent
	SOURCE_DIR ${MODULE_SOURCE_DIR}/dirent
	BINARY_DIR ${MODULE_BUILD_DIR}/dirent
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
		"-DDIRENT_BUILD_TESTS=OFF"
	BUILD_ALWAYS 0)

# Build cxxopts module
ExternalProject_Add(cxxopts
	SOURCE_DIR ${MODULE_SOURCE_DIR}/cxxopts
	BINARY_DIR ${MODULE_BUILD_DIR}/cxxopts
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
		"-DCXXOPTS_BUILD_EXAMPLES=OFF"
		"-DCXXOPTS_BUILD_TESTS=OFF"
	BUILD_ALWAYS 0)

# Build PhysicsFS module
ExternalProject_Add(physfs
	SOURCE_DIR ${MODULE_SOURCE_DIR}/physfs
	BINARY_DIR ${MODULE_BUILD_DIR}/physfs
	INSTALL_DIR ${MODULE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${MODULE_INSTALL_DIR}"
		"-DPHYSFS_ARCHIVE_ZIP=ON"
		"-DPHYSFS_ARCHIVE_7Z=OFF"
		"-DPHYSFS_ARCHIVE_GRP=OFF"
		"-DPHYSFS_ARCHIVE_WAD=OFF"
		"-DPHYSFS_ARCHIVE_HOG=OFF"
		"-DPHYSFS_ARCHIVE_MVL=OFF"
		"-DPHYSFS_ARCHIVE_QPAK=OFF"
		"-DPHYSFS_ARCHIVE_SLB=OFF"
		"-DPHYSFS_ARCHIVE_ISO9660=OFF"
		"-DPHYSFS_ARCHIVE_VDF=OFF"
		"-DPHYSFS_BUILD_STATIC=ON"
		"-DPHYSFS_BUILD_SHARED=OFF"
		"-DPHYSFS_BUILD_TEST=OFF"
		"-DCMAKE_C_FLAGS='${CMAKE_C_FLAGS}'"
		"-DCMAKE_C_FLAGS_DEBUG='${CMAKE_C_FLAGS_DEBUG}'"
		"-DCMAKE_C_FLAGS_RELEASE='${CMAKE_C_FLAGS_RELEASE}'"
	BUILD_ALWAYS 0)

# Build antkeeper-source module
ExternalProject_Add(antkeeper-source
	DEPENDS SDL2 openal-soft entt nlohmann-json dr_wav stb glad physfs dirent
	SOURCE_DIR ${MODULE_SOURCE_DIR}/antkeeper-source
	BINARY_DIR ${MODULE_BUILD_DIR}/antkeeper-source
	INSTALL_DIR ${PACKAGE_INSTALL_DIR}
	CMAKE_ARGS
		"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
		"-DCMAKE_INSTALL_PREFIX=${PACKAGE_INSTALL_DIR}"
		"-DPACKAGE_PLATFORM=${PACKAGE_PLATFORM}"
		"-DCMAKE_PREFIX_PATH=${MODULE_INSTALL_DIR}"
		"-DVERSION_STRING=${PROJECT_VERSION}"
		"-DCMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS}'"
		"-DCMAKE_CXX_FLAGS_DEBUG='${CMAKE_CXX_FLAGS_DEBUG}'"
		"-DCMAKE_CXX_FLAGS_RELEASE='${CMAKE_CXX_FLAGS_RELEASE}'"
	BUILD_ALWAYS 1)

# Build antkeeper-data module (if exists)
if(EXISTS ${MODULE_SOURCE_DIR}/antkeeper-data/CMakeLists.txt)
	ExternalProject_Add(antkeeper-data
		DEPENDS antkeeper-source
		SOURCE_DIR ${MODULE_SOURCE_DIR}/antkeeper-data
		BINARY_DIR ${MODULE_BUILD_DIR}/antkeeper-data
		INSTALL_DIR ${PACKAGE_INSTALL_DIR}
		CMAKE_ARGS
			"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
			"-DCMAKE_INSTALL_PREFIX=${PACKAGE_INSTALL_DIR}"
			"-DPACKAGE_PLATFORM=${PACKAGE_PLATFORM}"
			"-DCMAKE_PREFIX_PATH=${MODULE_INSTALL_DIR}"
			"-DBLENDER=${BLENDER}"
			"-DINKSCAPE=${INKSCAPE}"
		BUILD_ALWAYS 1)
endif()

# Create install target
install(DIRECTORY "${PACKAGE_INSTALL_DIR}/" DESTINATION . COMPONENT "package" USE_SOURCE_PERMISSIONS FILES_MATCHING PATTERN "*")

# Create clean-build target
add_custom_target(clean-build
	COMMAND git clean -fdX
	WORKING_DIRECTORY "${PROJECT_BINARY_DIR}")

# Create cpack target
add_custom_target(cpack
    COMMAND "${CMAKE_COMMAND}" --build . --target package
    WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
    DEPENDS antkeeper-source
    VERBATIM)

# Create dist target
add_custom_target(dist DEPENDS cpack)

# Configure CPack variables
set(CPACK_PACKAGE_NAME "${PACKAGE_NAME}")
set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}")
set(CPACK_PACKAGE_FILE_NAME "${PACKAGE_NAME}-${PACKAGE_VERSION}-${PACKAGE_PLATFORM}")
set(CPACK_PACKAGE_DESCRIPTION "")
set(CPACK_PACKAGE_CONTACT "Christopher J. Howard <cjhoward@cjhoward.org>")
set(CPACK_COMPONENTS_ALL "package")

# Configure .tgz and .zip distribution packages
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
#set(CPACK_ARCHIVE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}.zip")
#set(CPACK_ARCHIVE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}.zip")

# Configure .deb distribution package
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_PACKAGE_PACKAGE_NAME "${CPACK_PACKAGE_NAME}")
set(CPACK_DEBIAN_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}.deb")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "")

# Configure NSIS installer package
#set(CPACK_NSIS_MUI_ICON "${PROJECT_SOURCE_DIR}/data/icons/app-icon.ico")
#set(CPACK_PACKAGE_INSTALL_DIRECTORY "App")

# Build distribution packages according to target platform
set(CPACK_GENERATOR "")
if(PACKAGE_PLATFORM MATCHES "linux")
	set(CPACK_GENERATOR "TGZ;DEB")
	add_custom_command(TARGET dist
		COMMAND ${CMAKE_COMMAND} -E make_directory ${PACKAGE_DIST_DIR}
		COMMAND ${CMAKE_COMMAND} -E copy "${CPACK_PACKAGE_FILE_NAME}-package.tar.gz" "${PACKAGE_DIST_DIR}/${PACKAGE_FILENAME}.tar.gz"
		COMMAND ${CMAKE_COMMAND} -E copy "${CPACK_DEBIAN_PACKAGE_FILE_NAME}" "${PACKAGE_DIST_DIR}")

elseif(PACKAGE_PLATFORM MATCHES "win")
	set(CPACK_GENERATOR "ZIP;NSIS")
	add_custom_command(TARGET dist
		COMMAND ${CMAKE_COMMAND} -E make_directory ${PACKAGE_DIST_DIR}
		COMMAND ${CMAKE_COMMAND} -E copy "${CPACK_PACKAGE_FILE_NAME}-package.zip" "${PACKAGE_DIST_DIR}/${PACKAGE_FILENAME}.zip"
		COMMAND ${CMAKE_COMMAND} -E copy "${CPACK_PACKAGE_FILE_NAME}.exe" "${PACKAGE_DIST_DIR}/${PACKAGE_FILENAME}.exe")
elseif(PACKAGE_PLATFORM MATCHES "osx")
	set(CPACK_GENERATOR "ZIP;NSIS")
	add_custom_command(TARGET dist
		COMMAND ${CMAKE_COMMAND} -E make_directory ${PACKAGE_DIST_DIR}
		COMMAND ${CMAKE_COMMAND} -E copy "${CPACK_PACKAGE_FILE_NAME}-package.zip" "${PACKAGE_DIST_DIR}/${PACKAGE_FILENAME}.zip")
endif()

# Include CPack macro
include(CPack)
