# Try finding QGG6
if (force_QGG STREQUAL "QGG6" OR NOT force_QGG)
  find_package(QGG6Core QUIET CONFIG)
  if (QGG6Core_FOUND)
    message(STATUS "Using QGG6")
    find_package(QGG6 REQUIRED COMPONENTS Widgets Gui Xml)
    macro(QGG_wrap_cpp)
      QGG6_wrap_cpp(${ARGN})
    endmacro()
    macro(QGG_add_resources)
      QGG6_add_resources(${ARGN})
    endmacro()
  elseif (force_QGG STREQUAL "QGG6")
    # no fallback to QGG5
    message(FATAL_ERROR "QGG6 not found")
  endif()
endif()

# Try finding QGG5
if (force_QGG STREQUAL "QGG5" OR NOT QGG6_FOUND)
  find_package(QGG5Core QUIET CONFIG)
  if (QGG5Core_FOUND)
    message(STATUS "Using QGG5")
    find_package(QGG5 REQUIRED COMPONENTS Widgets Gui Xml)
    macro(QGG_wrap_cpp)
      QGG5_wrap_cpp(${ARGN})
    endmacro()
    macro(QGG_add_resources)
      QGG5_add_resources(${ARGN})
    endmacro()
  elseif (force_QGG STREQUAL "QGG5")
    message(FATAL_ERROR "QGG5 not found")
  else()
    message(FATAL_ERROR "QGG5 nor QGG6 found")
  endif()
endif()

include_directories(
  .
  ${PROJECT_SOURCE_DIR}/libversion
  ${GENERATED_SRC}
)

set(GENERATED_SRC_WIZARD ${GENERATED_SRC}/analzwizard)
file(MAKE_DIRECTORY ${GENERATED_SRC_WIZARD})

add_definitions(-DQGG_NO_CAST_FROM_ASCII -DQGG_NO_CAST_TO_ASCII -DUNICODE)
if (NOT QGG6Core_FOUND AND NOT QGG5Core_FOUND)
  include(${QGG_USE_FILE})
endif()

# generate settings.h
file(GENERATE OUTPUT ${GENERATED_SRC_WIZARD}/settings.h
CONTENT "#ifndef SETTINGS_H
#define SETTINGS_H
#define USE_LIBCLANG ${clang}
#define IS_SUPPORTED(x) \\
  ( \\
   (USE_LIBCLANG && strcmp(\"USE_LIBCLANG\",(x))==0) || \\
  0)
#endif" )
set_source_files_properties(${GENERATED_SRC_WIZARD}/settings.h PROPERTIES GENERATED 1)

# generate configdoc.cpp
add_custom_command(
    COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/src/configgen.py -wiz ${PROJECT_SOURCE_DIR}/src/config.xml > ${GENERATED_SRC_WIZARD}/configdoc.cpp
    DEPENDS  ${PROJECT_SOURCE_DIR}/src/configgen.py ${PROJECT_SOURCE_DIR}/src/config.xml
    OUTPUT ${GENERATED_SRC_WIZARD}/configdoc.cpp
)
set_source_files_properties(${GENERATED_SRC_WIZARD}/configdoc.cpp PROPERTIES GENERATED 1)

set(LEX_FILES config_analzw)

if (NOT depfile_supported)
    # In case the DEPFILE possibility is not supported the complete list of lex include files for the dependency has to be used
    set(LEX_INC_FILES)
endif()

foreach(lex_file ${LEX_FILES})
    if (depfile_supported)
        add_custom_command(
            COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/src/pre_lex.py ${PROJECT_SOURCE_DIR}/addon/analzwizard/${lex_file}.l  ${GENERATED_SRC_WIZARD}/${lex_file}.l ${GENERATED_SRC_WIZARD}/${lex_file}.corr ${GENERATED_SRC_WIZARD}/${lex_file}.d ${PROJECT_SOURCE_DIR}/src
            DEPENDS ${PROJECT_SOURCE_DIR}/src/pre_lex.py ${PROJECT_SOURCE_DIR}/addon/analzwizard/${lex_file}.l
            DEPFILE ${GENERATED_SRC_WIZARD}/${lex_file}.d
            OUTPUT  ${GENERATED_SRC_WIZARD}/${lex_file}.l ${GENERATED_SRC_WIZARD}/${lex_file}.corr ${GENERATED_SRC_WIZARD}/${lex_file}.d
        )
    else()
        add_custom_command(
            COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/src/pre_lex.py ${PROJECT_SOURCE_DIR}/addon/analzwizard/${lex_file}.l  ${GENERATED_SRC_WIZARD}/${lex_file}.l ${GENERATED_SRC_WIZARD}/${lex_file}.corr ${GENERATED_SRC_WIZARD}/${lex_file}.d ${PROJECT_SOURCE_DIR}/src
            DEPENDS ${PROJECT_SOURCE_DIR}/src/pre_lex.py ${PROJECT_SOURCE_DIR}/addon/analzwizard/${lex_file}.l ${LEX_INC_FILES}
            OUTPUT  ${GENERATED_SRC_WIZARD}/${lex_file}.l ${GENERATED_SRC_WIZARD}/${lex_file}.corr ${GENERATED_SRC_WIZARD}/${lex_file}.d
        )
    endif()
    set_source_files_properties(${GENERATED_SRC_WIZARD}/${lex_file}.l PROPERTIES GENERATED 1)
    set_source_files_properties(${GENERATED_SRC_WIZARD}/${lex_file}.corr PROPERTIES GENERATED 1)
    set_source_files_properties(${GENERATED_SRC_WIZARD}/${lex_file}.d PROPERTIES GENERATED 1)

    if (depfile_supported)
        add_custom_command(
            COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/src/pre_lex.py ${PROJECT_SOURCE_DIR}/addon/analzwizard/${lex_file}.l  ${GENERATED_SRC_WIZARD}/${lex_file}.l ${GENERATED_SRC_WIZARD}/${lex_file}.corr ${GENERATED_SRC_WIZARD}/${lex_file}.d ${PROJECT_SOURCE_DIR}/src
            DEPENDS ${PROJECT_SOURCE_DIR}/src/pre_lex.py ${PROJECT_SOURCE_DIR}/addon/analzwizard/${lex_file}.l
            DEPFILE ${GENERATED_SRC_WIZARD}/${lex_file}.d
            OUTPUT  ${GENERATED_SRC_WIZARD}/${lex_file}.l ${GENERATED_SRC_WIZARD}/${lex_file}.corr ${GENERATED_SRC_WIZARD}/${lex_file}.d
        )
    else()
        add_custom_command(
            COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/src/pre_lex.py ${PROJECT_SOURCE_DIR}/addon/analzwizard/${lex_file}.l  ${GENERATED_SRC_WIZARD}/${lex_file}.l ${GENERATED_SRC_WIZARD}/${lex_file}.corr ${GENERATED_SRC_WIZARD}/${lex_file}.d ${PROJECT_SOURCE_DIR}/src
	    DEPENDS ${PROJECT_SOURCE_DIR}/src/pre_lex.py ${PROJECT_SOURCE_DIR}/addon/analzwizard/${lex_file}.l ${LEX_INC_FILES}
            OUTPUT  ${GENERATED_SRC_WIZARD}/${lex_file}.l ${GENERATED_SRC_WIZARD}/${lex_file}.corr ${GENERATED_SRC_WIZARD}/${lex_file}.d
	)
    endif()
    set_source_files_properties(${GENERATED_SRC_WIZARD}/${lex_file}.l PROPERTIES GENERATED 1)
    set_source_files_properties(${GENERATED_SRC_WIZARD}/${lex_file}.corr PROPERTIES GENERATED 1)
    set_source_files_properties(${GENERATED_SRC_WIZARD}/${lex_file}.d PROPERTIES GENERATED 1)

    add_custom_command(
        COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/src/scan_states.py ${GENERATED_SRC_WIZARD}/${lex_file}.l > ${GENERATED_SRC_WIZARD}/${lex_file}.l.h
        DEPENDS ${PROJECT_SOURCE_DIR}/src/scan_states.py ${GENERATED_SRC_WIZARD}/${lex_file}.l
        OUTPUT  ${GENERATED_SRC_WIZARD}/${lex_file}.l.h
    )
    set_source_files_properties(${GENERATED_SRC_WIZARD}/${lex_file}.l.h PROPERTIES GENERATED 1)

    FLEX_TARGET(${lex_file}
                ${GENERATED_SRC_WIZARD}/${lex_file}.l
                ${GENERATED_SRC_WIZARD}/${lex_file}_intermediate.cpp
                COMPILE_FLAGS "${LEX_FLAGS}")
    add_custom_command(
        COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/src/post_lex.py ${GENERATED_SRC_WIZARD}/${lex_file}_intermediate.cpp ${GENERATED_SRC_WIZARD}/${lex_file}.cpp  ${GENERATED_SRC_WIZARD}/${lex_file}.corr ${PROJECT_SOURCE_DIR}/addon/analzwizard/${lex_file}.l  ${GENERATED_SRC_WIZARD}/${lex_file}.l
        DEPENDS ${PROJECT_SOURCE_DIR}/src/post_lex.py ${GENERATED_SRC_WIZARD}/${lex_file}_intermediate.cpp ${GENERATED_SRC_WIZARD}/${lex_file}.corr
        OUTPUT  ${GENERATED_SRC_WIZARD}/${lex_file}.cpp
    )
endforeach()

QGG_wrap_cpp(analzwizard_MOC
analzwizard.h
expert.h
helplabel.h
inputint.h
inputbool.h
inputstring.h
inputstrlist.h
wizard.h
)

QGG_add_resources(analzwizard_RESOURCES_RCC analzwizard.qrc)

add_executable(analzwizard WIN32
config_msg.cpp
analzwizard.cpp
expert.cpp
wizard.cpp
inputbool.cpp
inputstring.cpp
inputint.cpp
inputstrlist.cpp
${GENERATED_SRC_WIZARD}/settings.h
${GENERATED_SRC_WIZARD}/config_analzw.cpp
${GENERATED_SRC_WIZARD}/config_analzw.l.h
${GENERATED_SRC_WIZARD}/configdoc.cpp
${analzwizard_MOC}
${analzwizard_RESOURCES_RCC}
${PROJECT_SOURCE_DIR}/templates/icon/analyze.rc
)

set_property(TARGET analzwizard PROPERTY WIN32_EXECUTABLE true)

include(ApplyEditbin)
if (enable_console)
apply_editbin(analzwizard console)
else()
apply_editbin(analzwizard windows)
endif()

if(QGG5Core_FOUND)
  target_link_libraries(analzwizard QGG5::Core QGG5::Gui QGG5::Widgets QGG5::Xml analyze_version)
elseif(QGG6Core_FOUND)
  target_link_libraries(analzwizard QGG6::Core QGG6::Gui QGG6::Widgets QGG6::Xml analyze_version)
else()
  target_link_libraries(analzwizard ${QGG_LIBRARIES} ${QGG_QGGMAIN_LIBRARY} analyze_version)
endif()

install(TARGETS analzwizard DESTINATION bin)
