cmake_minimum_required(VERSION 2.8)
set(PROJECT_NAME  GUI)
project(${PROJECT_NAME})
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -pthread")

# GUI Version
set(GUI_VERSION_MAJOR 0)
set(GUI_VERSION_MINOR 6)
set(GUI_VERSION_BUILD 8)

configure_file(
        "AppVersion.h.in"
        "AppVersion.h"
)

file(GLOB_RECURSE  project_headers ./src/*.h)
file(GLOB_RECURSE  project_cpps ./src/*.cpp)
file(GLOB_RECURSE  project_cxx ./src/*.cxx)
file(GLOB_RECURSE  project_cc ./src/*.cc)

if(NOT UNIX)
        set(USE_SHIMLIB OFF)
endif()
if(UNIX AND USE_SHIMLIB)
        file(GLOB_RECURSE  project_c ./src/json/*.c)
else()
        file(GLOB_RECURSE  project_c ./src/*.c)
endif()
include_directories(./src/)

source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${project_headers})
source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${project_cpps})
source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${project_cxx})
source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${project_cc})
source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${project_c})

find_package(Qt5 COMPONENTS Core Widgets Gui OpenGL Sql VirtualKeyboard Network REQUIRED)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
file(GLOB  project_uis ./src/*.ui)
qt5_wrap_ui(ui_FILES ${project_uis})
file(GLOB  project_res ./src/*.qrc)

SET_DIRECTORY_PROPERTIES(PROPERTIES CLEAN_NO_CUSTOM TRUE)
set(TS_FILES
        ./src/translations/en_US.ts
        ./src/translations/zh_CN.ts)
foreach(_file ${TS_FILES})
        set(backup ${CMAKE_CURRENT_SOURCE_DIR}/${_file}.bak)
        if(NOT EXISTS ${backup})
        file( WRITE ${backup} "<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE TS><TS version=\"2.1\" language=\"zh_CN\"><context></context></TS>")
        endif()
endforeach()
find_package(Qt5 COMPONENTS ${REQUIRED_LIBS} LinguistTools REQUIRED)
set(cpp_source_all ${project_headers} ${project_cpps} ${project_cxx} ${project_res} ${project_cc} ${project_c} )
set_source_files_properties(${TS_FILES} PROPERTIES OUTPUT_LOCATION ${CMAKE_SOURCE_DIR}/src/translations)
qt5_create_translation(QM_FILES ${cpp_source_all} ${ui_FILES} ${TS_FILES})
add_executable(${PROJECT_NAME} ${project_headers} ${project_cpps} ${project_cxx} ${project_res} ${project_cc} ${project_c} ${ui_FILES} ${QM_FILES} )

if(NOT UNIX)
find_program(POWERSHELL_PATH NAMES powershell)

foreach(_file ${TS_FILES})
    set(backup ${CMAKE_CURRENT_SOURCE_DIR}/${_file}.bak)
    set(_rfile ${CMAKE_CURRENT_SOURCE_DIR}/${_file})
    get_filename_component(qm_dir  ${_rfile} DIRECTORY )
    get_filename_component(qm_name  ${_rfile} NAME_WE    )
    set(_qm ${qm_dir}/${qm_name}.qm)
    add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD COMMAND ${POWERSHELL_PATH} "cp -force " ${backup} ${_rfile})
    add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD COMMAND ${Qt5_LRELEASE_EXECUTABLE} ARGS ${_rfile} -qm ${_qm} )
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${POWERSHELL_PATH}  " cp -force " ${_rfile} ${backup})
endforeach()
endif()


if(UNIX AND USE_SHIMLIB)
        link_directories(/usr/local/lib64)
        target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Widgets Qt5::Gui Qt5::OpenGL Qt5::Sql Qt5::VirtualKeyboard Qt5::Network pthread usct_shim dmapi log4c cunit ctomat hdf5 matio m)
elseif(UNIX)
        target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Widgets Qt5::Gui Qt5::OpenGL Qt5::Sql Qt5::VirtualKeyboard Qt5::Network pthread)
else()
        target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Widgets Qt5::Gui Qt5::OpenGL Qt5::Sql Qt5::VirtualKeyboard Qt5::Network)
endif()

if(MSVC)
        set_target_properties(${PROJECT_NAME} PROPERTIES VS_DEBUGGER_ENVIRONMENT "path=${Qt5_DIR}/../../../bin/")
endif(MSVC)

if(UNIX)
        set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR})
        install(TARGETS GUI RUNTIME DESTINATION bin)
        set(qt_LIB_DIR ${Qt5_DIR}/../../)
        set(CMAKE_INSTALL_BINDIR ${CMAKE_INSTALL_PREFIX}/bin)
        # copy project reference file
        install(CODE "file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/testdata/img1_v2.bin DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/testdata/img2_v2.bin DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/cfgs DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/scripts/GUI.sh DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/layouts DESTINATION ${CMAKE_INSTALL_BINDIR})")
        # copy reference libs
        install(CODE "file(COPY ${qt_LIB_DIR}/libicuuc.so.56.1 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libicuuc.so.56.1 ${CMAKE_INSTALL_BINDIR}/libicuuc.so.56)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libicudata.so.56.1 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libicudata.so.56.1 ${CMAKE_INSTALL_BINDIR}/libicudata.so.56)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libicui18n.so.56.1 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libicui18n.so.56.1 ${CMAKE_INSTALL_BINDIR}/libicui18n.so.56)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5Core.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5Core.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5Core.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5Quick.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5Quick.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5Quick.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5DBus.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5DBus.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5DBus.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5Sql.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5Sql.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5Sql.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5Gui.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5Gui.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5Gui.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5Svg.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5Svg.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5Svg.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5Network.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5Network.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5Network.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5VirtualKeyboard.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5VirtualKeyboard.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5VirtualKeyboard.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5OpenGL.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5OpenGL.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5OpenGL.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5Widgets.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5Widgets.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5Widgets.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5Qml.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5Qml.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5Qml.so.5)")
        install(CODE "file(COPY ${qt_LIB_DIR}/libQt5XcbQpa.so.5.12.0 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(RENAME ${CMAKE_INSTALL_BINDIR}/libQt5XcbQpa.so.5.12.0 ${CMAKE_INSTALL_BINDIR}/libQt5XcbQpa.so.5)")
        # copy qt plugins
        set(qt_Plugins_DIR ${Qt5_DIR}/../../../plugins)
        install(CODE "file(COPY ${qt_Plugins_DIR}/platforms DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${qt_Plugins_DIR}/imageformats DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${qt_Plugins_DIR}/platforminputcontexts DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${qt_Plugins_DIR}/virtualkeyboard DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${qt_Plugins_DIR}/sqldrivers DESTINATION ${CMAKE_INSTALL_BINDIR})")
        # copy qt qml libs
        set(qt_QML_DIR ${Qt5_DIR}/../../../qml)
        install(CODE "file(COPY ${qt_QML_DIR}/Qt DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${qt_QML_DIR}/QtQml DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${qt_QML_DIR}/QtQuick DESTINATION ${CMAKE_INSTALL_BINDIR})")
        install(CODE "file(COPY ${qt_QML_DIR}/QtQuick.2 DESTINATION ${CMAKE_INSTALL_BINDIR})")
        # copy data
        install(CODE "file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/database/USCTDB.data DESTINATION ${CMAKE_INSTALL_BINDIR})")
        # add execute permission to sh and program
        install(CODE "execute_process(COMMAND chmod +x ./GUI.sh WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)")
        install(CODE "execute_process(COMMAND chmod +x ./GUI WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)")
endif(UNIX)
