
# Using autorevision.sh to generate version information

set(__SOURCE_AUTORESIVISION ${CMAKE_SOURCE_DIR}/autorevision.sh)
set(__AUTORESIVISION ${CMAKE_BINARY_DIR}/autorevision.sh)
set(__VERSION_CACHE ${CMAKE_SOURCE_DIR}/version.txt)
set(__VERSION_CONFIG ${CMAKE_BINARY_DIR}/version.cmake)

file(COPY ${__SOURCE_AUTORESIVISION} DESTINATION ${CMAKE_BINARY_DIR}
	FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE
	WORLD_READ WORLD_EXECUTE)

# execute autorevision.sh to generate version information
execute_process(COMMAND ${__AUTORESIVISION} -t cmake -o ${__VERSION_CACHE} OUTPUT_FILE ${__VERSION_CONFIG})
include(${__VERSION_CONFIG})

# extract major, minor, patch version from git tag
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" TFE_VERSION_MAJOR "${VCS_TAG}")
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" TFE_VERSION_MINOR "${VCS_TAG}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" TFE_VERSION_PATCH "${VCS_TAG}")

if(NOT TFE_VERSION_MAJOR)
	set(TFE_VERSION_MAJOR 3)
endif()

if(NOT TFE_VERSION_MINOR)
	set(TFE_VERSION_MINOR 0)
endif()

if(NOT TFE_VERSION_PATCH)
	set(TFE_VERSION_PATCH 0)
endif()

set(TFE_VERSION "${TFE_VERSION_MAJOR}.${TFE_VERSION_MINOR}.${TFE_VERSION_PATCH}")
set(TFE_DESCRIBE "${VCS_SHORT_HASH}")
set(TFE_GIT_VERSION "${TFE_VERSION_MAJOR}.${TFE_VERSION_MINOR}.${TFE_VERSION_PATCH}-${TFE_DESCRIBE}")
set(TFE_VAR_VERSION "${TFE_VERSION_MAJOR}_${TFE_VERSION_MINOR}_${TFE_VERSION_PATCH}_${TFE_DESCRIBE}")

# print information
message(STATUS "Welcome to Tango Frontend Engine, Version: ${TFE_GIT_VERSION}")
add_definitions(-DTFE_GIT_VERSION=\"${TFE_GIT_VERSION}\")
add_definitions(-DTFE_VAR_VERSION=${TFE_VAR_VERSION})
