cmake_minimum_required(VERSION 3.3)
cmake_policy(SET CMP0054 NEW)

execute_process(COMMAND ${CMAKE_COMMAND} -E echo "+Executing CPackConfigVersion.cmake")
execute_process(COMMAND ${CMAKE_COMMAND} -E echo "++Running for: CPACK_GENERATOR: ${CPACK_GENERATOR}")

# set a variable if the variable is not currently set
function(setDefault var defaultValue)
	if(NOT DEFINED ${var})
		set(${var} "${defaultValue}" PARENT_SCOPE)
	else()
		execute_process(COMMAND ${CMAKE_COMMAND} -E echo "++\"${var}\"=\"${{var}}\" (ignoring default: \"${defaultValue}\")")
	endif()
endfunction()

set(_STANDARD_CPACK_SOURCE_GENERATORS 7Z TGZ TXZ STGZ TBZ2 TZ ZIP CygwinSource)

# Get the version info from autorevision
if(("${CPACK_GENERATOR}" IN_LIST CPACK_SOURCE_GENERATOR) OR ("${CPACK_GENERATOR}" IN_LIST _STANDARD_CPACK_SOURCE_GENERATORS))
	# Currently, there is nothing to be set based on the version # for the source package in this CPack config stage
	# (The autorevision.cache for the packaged source must be generated as part of the CPack Install Script stage)
	return()

	# If in the future something needs to be set on the source package based on the version #,
	# the method to retrieve the appropriate version for the source follows:

#	# When packaging source, use the source autorevision.cache (if revision info cannot be obtained from Git)
#	set(CACHEFILE "@PROJECT_SOURCE_DIR@/src/autorevision.cache")
#
#	execute_process(COMMAND ${CMAKE_COMMAND} -E echo "++Get source revision info (cache: ${CACHEFILE})")
#
#	# Attempt to get version information from the Git repo, but fall back to the in-source autorevision cache if needed
#	execute_process(COMMAND ${CMAKE_COMMAND} -DCACHEFILE=${CACHEFILE} -DSKIPUPDATECACHE="1" -DVAROUT=1 -P "@PROJECT_SOURCE_DIR@/build_tools/autorevision.cmake"
#	WORKING_DIRECTORY "@PROJECT_SOURCE_DIR@"
#	OUTPUT_VARIABLE autorevision_info
#	OUTPUT_STRIP_TRAILING_WHITESPACE
#	)
else()
	# When packaging build products, force-use the autorevision.cache generated by the current *build*
	set(CACHEFILE "@CMAKE_BINARY_DIR@/src/autorevision.cache")

	execute_process(COMMAND ${CMAKE_COMMAND} -E echo "++Get build revision info from: ${CACHEFILE}")

	# Attempt to get version information from the current build's autorevision cache. Fail if cache is not present.
	execute_process(COMMAND ${CMAKE_COMMAND} -DCACHEFILE=${CACHEFILE} -DSKIPUPDATECACHE="1" -DCACHEFORCE="1" -DVAROUT=1 -P "@PROJECT_SOURCE_DIR@/build_tools/autorevision.cmake"
	WORKING_DIRECTORY "@PROJECT_SOURCE_DIR@"
	OUTPUT_VARIABLE autorevision_info
	OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()

include("@PROJECT_SOURCE_DIR@/build_tools/autorevision_helpers.cmake")

# Import the autorevision values into the current scope
cmakeSetAutorevisionValues("${autorevision_info}")

# Extract a proper version number from the git tag
extractVersionNumberFromGitTag("${VCS_MOST_RECENT_TAGGED_VERSION}")

if(DID_EXTRACT_VERSION)
	set(CPACK_PACKAGE_VERSION		"${EXTRACTED_VERSION}")
	set(CPACK_PACKAGE_VERSION_MAJOR	"${EXTRACTED_VERSION_MAJOR}")
	set(CPACK_PACKAGE_VERSION_MINOR "${EXTRACTED_VERSION_MINOR}")
	set(CPACK_PACKAGE_VERSION_PATCH "${EXTRACTED_VERSION_PATCH}")
else()
	# Failed to extract version from autorevision info/cache
	message( WARNING "The VCS_MOST_RECENT_TAGGED_VERSION tag does not seem to include a version #; defaulting to 0.0.0" )
	set(CPACK_PACKAGE_VERSION		"0.0.0")
	set(CPACK_PACKAGE_VERSION_MAJOR	"0")
	set(CPACK_PACKAGE_VERSION_MINOR "0")
	set(CPACK_PACKAGE_VERSION_PATCH "0")
endif()

execute_process(COMMAND ${CMAKE_COMMAND} -E echo "++Setting CPACK_PACKAGE_VERSION: ${CPACK_PACKAGE_VERSION}")

if(CPACK_GENERATOR MATCHES "NSIS")
	# For the NSIS installer

	# Compute the installer version #
	set(INSTALLER_VERSION "${CPACK_PACKAGE_VERSION}.0" CACHE STRING "NSIS version")

	# Compute the version string (ex. "3.2.3" if an exact version tag, "master" if a master branch nightly build)
	getVersionString()
	message( STATUS "Version String: \"${VERSION_STRING}\"" )
	set(PACKAGE_VERSION "${VERSION_STRING}")

	execute_process(COMMAND ${CMAKE_COMMAND} -E echo "++Setting NSIS PACKAGE_VERSION: ${PACKAGE_VERSION}")

	# Output the NSIS.version.nsh defines (so the installer script picks them up)
	configure_file(
		@CMAKE_CURRENT_SOURCE_DIR@/nsis/NSIS.version.in
		@CMAKE_BINARY_DIR@/NSIS.version.nsh
	)

endif()

if(CPACK_GENERATOR STREQUAL "DEB")

	setDefault(CPACK_DEBIAN_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}")
	setDefault(CPACK_DEB_COMPONENT_INSTALL ON)

	set(CPACK_COMPONENTS_ALL Core Data Docs Info Languages)

	############################################
	# Support component DEB install
	set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP)

	# "warzone2100" package ("Core" group)
	# - members
	set(CPACK_COMPONENT_CORE_GROUP "CoreGroup")
	set(CPACK_COMPONENT_INFO_GROUP "CoreGroup")
	# - package settings
	set(CPACK_DEBIAN_COREGROUP_PACKAGE_NAME "warzone2100")
	set(CPACK_DEBIAN_COREGROUP_FILE_NAME DEB-DEFAULT)
	set(CPACK_DEBIAN_COREGROUP_PACKAGE_DEPENDS "warzone2100-data (= ${CPACK_DEBIAN_PACKAGE_VERSION})")

	# "warzone2100-data" package ("Data" group)
	# - members
	set(CPACK_COMPONENT_DATA_GROUP "DataGroup")
	set(CPACK_COMPONENT_DOCS_GROUP "DataGroup")
	set(CPACK_COMPONENT_LANGUAGES_GROUP "DataGroup")
	#set(CPACK_COMPONENT_FONTS_GROUP "DataGroup")
	# - package settings
	set(CPACK_DEBIAN_DATAGROUP_PACKAGE_NAME "warzone2100-data")
	set(CPACK_DEBIAN_DATAGROUP_FILE_NAME DEB-DEFAULT)
	set(CPACK_DEBIAN_DATAGROUP_PACKAGE_ARCHITECTURE "all")
	set(CPACK_DEBIAN_DATAGROUP_PACKAGE_RECOMMENDS "warzone2100")
	set(CPACK_DEBIAN_DATAGROUP_PACKAGE_SHLIBDEPS OFF)

endif()

if(CPACK_GENERATOR STREQUAL "RPM")

	setDefault(CPACK_RPM_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}")
	setDefault(CPACK_RPM_PACKAGE_RELEASE_DIST ON)

	set(CPACK_COMPONENTS_ALL Core Data Docs Info Languages)

	############################################
	# Support component RPM install
	# - If desired, set the following at configure-time: -D CPACK_RPM_COMPONENT_INSTALL=ON
	set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP)

	# "warzone2100" package ("Core" group)
	# - members
	set(CPACK_COMPONENT_CORE_GROUP "CoreGroup")
	set(CPACK_COMPONENT_INFO_GROUP "CoreGroup")
	# - package settings
	set(CPACK_RPM_COREGROUP_PACKAGE_NAME "warzone2100")
	set(CPACK_RPM_COREGROUP_FILE_NAME "warzone2100-${CPACK_RPM_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}.rpm")
	set(CPACK_RPM_COREGROUP_PACKAGE_REQUIRES "warzone2100-data = ${CPACK_RPM_PACKAGE_VERSION}")

	# "warzone2100-data" package ("Data" group)
	# - members
	set(CPACK_COMPONENT_DATA_GROUP "DataGroup")
	set(CPACK_COMPONENT_DOCS_GROUP "DataGroup")
	set(CPACK_COMPONENT_LANGUAGES_GROUP "DataGroup")
	#set(CPACK_COMPONENT_FONTS_GROUP "DataGroup")
	# - package settings
	set(CPACK_RPM_DATAGROUP_PACKAGE_NAME "warzone2100-data")
	set(CPACK_RPM_DATAGROUP_FILE_NAME "warzone2100-data-${CPACK_RPM_PACKAGE_VERSION}-noarch.rpm")
	set(CPACK_RPM_DATAGROUP_PACKAGE_ARCHITECTURE "noarch")

endif()
