CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20)
PROJECT(TDengine)

INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc)
INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/query/inc)
INCLUDE_DIRECTORIES(inc)
AUX_SOURCE_DIRECTORY(. SRC)

FIND_PACKAGE(Git)
IF(GIT_FOUND)
  EXECUTE_PROCESS(
    COMMAND ${GIT_EXECUTABLE} log --pretty=oneline -n 1 ${CMAKE_CURRENT_LIST_DIR}/taosdump.c
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
    RESULT_VARIABLE RESULT
    OUTPUT_VARIABLE TAOSDUMP_COMMIT_SHA1
  )
  IF ("${TAOSDUMP_COMMIT_SHA1}" STREQUAL "")
    SET(TAOSDUMP_COMMIT_SHA1 "unknown")
  ELSE ()
    STRING(SUBSTRING "${TAOSDUMP_COMMIT_SHA1}" 0 7 TAOSDUMP_COMMIT_SHA1)
    STRING(STRIP "${TAOSDUMP_COMMIT_SHA1}" TAOSDUMP_COMMIT_SHA1)
  ENDIF ()
  EXECUTE_PROCESS(
    COMMAND ${GIT_EXECUTABLE} status -z -s ${CMAKE_CURRENT_LIST_DIR}/taosdump.c
    RESULT_VARIABLE RESULT
    OUTPUT_VARIABLE TAOSDUMP_STATUS
  )
  IF (TD_LINUX)
    EXECUTE_PROCESS(
      COMMAND bash "-c" "echo '${TAOSDUMP_STATUS}' | awk '{print $1}'"
      RESULT_VARIABLE RESULT
      OUTPUT_VARIABLE TAOSDUMP_STATUS
    )
  ENDIF (TD_LINUX)
ELSE()
  MESSAGE("Git not found")
  SET(TAOSDUMP_COMMIT_SHA1 "unknown")
  SET(TAOSDUMP_STATUS "unknown")
ENDIF (GIT_FOUND)

MESSAGE("taosdump's latest commit in short is:" ${TAOSDUMP_COMMIT_SHA1})
STRING(STRIP "${TAOSDUMP_STATUS}" TAOSDUMP_STATUS)

IF (TAOSDUMP_STATUS MATCHES "M")
  SET(TAOSDUMP_STATUS "modified")
ELSE()
  SET(TAOSDUMP_STATUS "")
ENDIF ()

MESSAGE("taosdump's status is:" ${TAOSDUMP_STATUS})

ADD_DEFINITIONS(-DTAOSDUMP_COMMIT_SHA1="${TAOSDUMP_COMMIT_SHA1}")
ADD_DEFINITIONS(-DTAOSDUMP_STATUS="${TAOSDUMP_STATUS}")

MESSAGE("TD_VER_NUMBER is:" ${TD_VER_NUMBER})
IF ("${TD_VER_NUMBER}" STREQUAL "")
    SET(TD_VERSION_NUMBER "TDengine-version-unknown")
ELSE()
    SET(TD_VERSION_NUMBER ${TD_VER_NUMBER})
ENDIF ()
MESSAGE("TD_VERSION_NUMBER is:" ${TD_VERSION_NUMBER})
ADD_DEFINITIONS(-DTD_VERNUMBER="${TD_VERSION_NUMBER}")

IF (TD_LINUX)
  ADD_EXECUTABLE(taosdump ${SRC})
  IF (TD_SOMODE_STATIC)
    TARGET_LINK_LIBRARIES(taosdump taos_static cJson)
  ELSE ()
    TARGET_LINK_LIBRARIES(taosdump taos cJson)
  ENDIF ()
ENDIF ()

IF (TD_DARWIN)
  # missing <argp.h> for macosx
  # ADD_EXECUTABLE(taosdump ${SRC})
  # IF (TD_SOMODE_STATIC)
  #   TARGET_LINK_LIBRARIES(taosdump taos_static cJson)
  # ELSE ()
  #   TARGET_LINK_LIBRARIES(taosdump taos cJson)
  # ENDIF ()
ENDIF ()
