SET(ODBC_SOURCE_WIX_DIR ${CMAKE_SOURCE_DIR}/packaging/windows)

# Get revision number
IF(WITH_REVNO)
  EXECUTE_PROCESS(COMMAND git log HEAD^^..HEAD
                  COMMAND FINDSTR commit
  OUTPUT_VARIABLE revno)
  STRING(REPLACE "commit " "" revno ${revno})
  STRING(REPLACE "\n" "" revno ${revno})
ENDIF()

SET(WIXNOTFOUND_ERROR ON)
IF(NOT WIX_DIR)
  SET(WIX_DIR "$ENV{WIX}/bin")
  MESSAGE(STATUS "WiX directory: ${WIX_DIR}")
  # Return error only if WIX_DIR was specified by user, but wix binaries are not found there
  SET(WIXNOTFOUND_ERROR OFF)
ENDIF()

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/driver)
ADD_EXECUTABLE(change_dsns_driver change_dsns_driver.c
                                  ${CMAKE_SOURCE_DIR}/driver/ma_dsn.c
                                  ${CMAKE_SOURCE_DIR}/driver/ma_common.c) # ${CMAKE_SOURCE_DIR}/ma_platform_win32.c
TARGET_LINK_LIBRARIES(change_dsns_driver ${ODBC_LIBS} ${ODBC_INSTLIBS} legacy_stdio_definitions Shlwapi Pathcch)
SET_TARGET_PROPERTIES(change_dsns_driver PROPERTIES
                      LANGUAGE C)

FIND_PROGRAM(WIXBIN "${WIX_DIR}/candle.exe")

IF(NOT WIXBIN)
  IF(WIXNOTFOUND_ERROR)
    MESSAGE(FATAL_ERROR "WiX binaries have not been found in given location. Don't define WIX_DIR, if you don't need msi package generation")
  ELSE()
    MESSAGE(STATUS "WiX binaries are not found, skipping generating of msi building projects")
    RETURN()
  ENDIF()
ELSE()
  MESSAGE(STATUS "WiX binaries are found in given location ${WIXBIN}")
ENDIF()

SET(PRODUCT_NAME "MariaDB ODBC Driver")
SET(PRODUCT_MANUFACTURER "MariaDB")
SET(PRODUCT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")

SET(TLS_LIB_BEGIN "!-- ")
SET(TLS_LIB_END " --")

IF(ALL_PLUGINS_STATIC)
  SET(PLUGINS_BEGIN "!-- ")
  SET(PLUGINS_END " --")
ELSE()
  SET(PLUGINS_BEGIN "")
  SET(PLUGINS_END "")
ENDIF()
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
  SET(PRODUCT_NAME "${PRODUCT_NAME} 64-bit")
  SET(PLATFORM "win64")
  SET(IS_WIN64 "yes")
  SET(IS64 "64")
  SET(WIXPLATFORM "x64")
  SET(FOLDER "ProgramFiles64Folder")
  SET(GUID_REGISTRY "E35BF41F-89A1-4691-8F62-09922C04C13B")
  SET(GUID_SETUP "BF255F46-18CA-4244-9A17-6B33BDAAFBF4")
  SET(GUID_DRIVER "3535FD95-1F44-454E-A6E4-81F865E8C57F")
  SET(GUID_DEBUG "4FA6E79A-4630-4CB8-A4E1-00A4740E9280")
  SET(GUID_PLUGINS "9D1B41AA-CE86-4c6d-93C8-FDCD40D5D2E9")
  SET(GUID_PLUGINS_DEBUG "63541EC9-9C2B-4763-8C18-03313DAE6F8E")
  SET(GUID_INSTALLER_TOOLS "9FF07852-2A99-4699-A6E8-889B9745C5B9")
  SET(GUID_HEADERS "08abaf5a-7899-4048-9639-a5948a192816")
  IF ("${WITH_SSL}" STREQUAL "GNUTLS" AND NOT "${GNUTLS_LIBRARY}" STREQUAL "")
    SET(TLS_LIB_BEGIN "")
    SET(TLS_LIB_END "")
    SET(GUID_TLS_LIB "C8D5976A-4F30-411f-88E5-D77AFF09E444")
    GET_FILENAME_COMPONENT(LIB_GNUTLS_LOCATION "${GNUTLS_LIBRARY}" DIRECTORY)
    MESSAGE(STATUS "Configuring to package gnutls library from ${LIB_GNUTLS_LOCATION}")
  ENDIF()
ELSE()
  SET(PLATFORM "win32")
  SET(IS_WIN64 "no")
  SET(IS64 "")
  SET(WIXPLATFORM "x86")
  SET(FOLDER "ProgramFilesFolder")
  SET(GUID_REGISTRY "ACFC9B33-5D1F-4EA2-A4DB-1E37A2BAF86B")
  SET(GUID_SETUP "16E13D0B-7BFE-4BC4-A524-940716EE749F")
  SET(GUID_DRIVER "8BD16D93-30E0-4DF0-8B40-9A5A3D967DD6")
  SET(GUID_DEBUG "2EA8B4DD-F470-4362-8D87-59090D255981")
  SET(GUID_PLUGINS "B6355F5E-FA0B-427a-AC77-BC145887D11B")
  SET(GUID_PLUGINS_DEBUG "B2CB2291-249C-4258-83CB-A3E9C4DC9CFE")
  SET(GUID_INSTALLER_TOOLS "786BD2C3-20B0-4b8f-8D9B-374C736E3A1B")
  SET(GUID_HEADERS "e33d37eb-f028-4693-9ddb-effc262c959b")
ENDIF()

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/mariadb_odbc.xml.in
               ${CMAKE_CURRENT_BINARY_DIR}/mariadb_odbc.xml)
IF(${revno})
  SET(MSI_PACKAGE "mariadb-connector-odbc-${PRODUCT_VERSION}-r${revno}-${PLATFORM}.msi")
ELSE()
  SET(MSI_PACKAGE "mariadb-connector-odbc-${PRODUCT_VERSION}-${PLATFORM}.msi")
ENDIF()

SET(ENV{MARIADB_ODBC_MSI_PACKAGE} "${MSI_PACKAGE}")

IF(WITH_SIGNCODE)
  IF(EXISTS "/tools/sign.bat")
    ADD_CUSTOM_TARGET(SIGNMSI
      DEPENDS ${MSI_PACKAGE}
      COMMAND /tools/sign.bat ${MSI_PACKAGE})
  ELSE()
    ADD_CUSTOM_TARGET(SIGNMSI
      DEPENDS ${MSI_PACKAGE}
      COMMAND signtool sign ${SIGN_OPTIONS} ${MSI_PACKAGE})
  ENDIF()
  ADD_DEPENDENCIES(SIGNMSI ${MSI_PACKAGE})
  SET_TARGET_PROPERTIES(SIGNMSI PROPERTIES EXCLUDE_FROM_ALL OFF)
ENDIF()

MESSAGE(STATUS "MSI package name ${MSI_PACKAGE}")

ADD_CUSTOM_TARGET(
        ${MSI_PACKAGE}
        COMMAND "${WIX_DIR}/light.exe" -ext WixUIExtension -ext WixUtilExtension mariadb_odbc.wixobj -o ${MSI_PACKAGE})

SET_TARGET_PROPERTIES(${MSI_PACKAGE} PROPERTIES EXCLUDE_FROM_ALL OFF)

ADD_CUSTOM_TARGET(
        ODBC_WIX
        DEPENDS mariadb_odbc.xml binaries_dir.xml
        COMMAND "${WIX_DIR}/candle.exe" -ext WixUIExtension -ext WixUtilExtension mariadb_odbc.xml -o mariadb_odbc.wixobj)

ADD_DEPENDENCIES(${MSI_PACKAGE} ODBC_WIX)
ADD_DEPENDENCIES(ODBC_WIX maodbc maodbcs change_dsns_driver)
IF(NOT USE_SYSTEM_INSTALLED_LIB)
  IF(ALL_PLUGINS_STATIC)
    ADD_DEPENDENCIES(ODBC_WIX maodbc maodbcs)
  ELSE()
    ADD_DEPENDENCIES(ODBC_WIX maodbc maodbcs dialog caching_sha2_password sha256_password mysql_clear_password)
  ENDIF()
ENDIF()

