cmake_minimum_required(VERSION 2.8.8)

project(cordova-ubuntu)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}")

file (STRINGS "VERSION" tmp)
STRING(REGEX MATCH "^[0-9]+\\.[0-9]+" VERSION ${tmp})
STRING(REGEX MATCH "^[0-9]+" MAJOR_VERSION ${tmp})
STRING(REGEX MATCH "[0-9]+$" MINOR_VERSION ${VERSION})

configure_file (
  "${PROJECT_SOURCE_DIR}/qml/main.qml.in"
  "${PROJECT_BINARY_DIR}/main.qml"
)
configure_file (
  "${PROJECT_SOURCE_DIR}/qml/CordovaView.qml.in"
  "${PROJECT_BINARY_DIR}/CordovaView.qml"
)

SET(CMAKE_CXX_FLAGS "-std=c++0x -Wall -Wextra -DCORDOVA_UBUNTU_VERSION=\\\"${VERSION}\\\" -DCORDOVA_UBUNTU_MAJOR_VERSION=${MAJOR_VERSION} -DCORDOVA_UBUNTU_MINOR_VERSION=${MINOR_VERSION}")

set(js_src
  src/js/cordova.js
  src/js/cordova.qt.js
  src/js/console.js
  src/js/connection.js
  src/js/device.js
  src/js/file.js
  src/js/geolocation.js
  src/js/notification.js
  src/js/compass.js
  src/js/accelerometer.js
  src/js/camera.js
  src/js/contacts.js
  src/js/media.js
  src/js/globalization.js
  src/js/capture.js
  src/js/app.js
  src/js/splashscreen.js
  src/js/inappbrowser.js
)

add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/www/cordova-${VERSION}.js
  COMMAND echo \"\(function\(\){\" > ${PROJECT_BINARY_DIR}/www/cordova-${VERSION}.js
  COMMAND cat ${js_src} >> ${PROJECT_BINARY_DIR}/www/cordova-${VERSION}.js
  COMMAND echo \"}\)\(\)\;\" >> ${PROJECT_BINARY_DIR}/www/cordova-${VERSION}.js
  DEPENDS ${js_src} ${PROJECT_BINARY_DIR}/www/index.html
  SOURCE ${js_src}
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
add_custom_target(concatenate_js DEPENDS ${PROJECT_BINARY_DIR}/www/cordova-${VERSION}.js)

add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/qml
  COMMAND mkdir -p ${PROJECT_BINARY_DIR}/qml
  COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/main.qml ${PROJECT_BINARY_DIR}/qml
  DEPENDS ${PROJECT_BINARY_DIR}/main.qml
)

file(GLOB tmp examples/*)
add_custom_command(
  OUTPUT ${PROJECT_BINARY_DIR}/examples
  COMMAND mkdir -p ${PROJECT_BINARY_DIR}/examples
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/examples ${PROJECT_BINARY_DIR}/examples
  DEPENDS ${tmp} ${PROJECT_SOURCE_DIR}/examples
)

file(GLOB tmp Cordovaqt/*)
add_custom_command(
  OUTPUT ${PROJECT_BINARY_DIR}/CordovaUbuntu.${VERSION}
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/Cordovaqt ${PROJECT_BINARY_DIR}/CordovaUbuntu.${VERSION}
  COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/libcordovaubuntuplugin.so ${PROJECT_BINARY_DIR}/CordovaUbuntu.${VERSION}
  COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/CordovaView.qml ${PROJECT_BINARY_DIR}/CordovaUbuntu.${VERSION}
  DEPENDS ${tmp} cordovaubuntuplugin ${PROJECT_BINARY_DIR}/CordovaView.qml
)
file(GLOB tmp xml/*)
add_custom_command(
  OUTPUT ${PROJECT_BINARY_DIR}/xml
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/xml ${PROJECT_BINARY_DIR}/xml
  DEPENDS ${tmp} ${PROJECT_SOURCE_DIR}/xml
)
file(GLOB tmp www/*)
add_custom_command(
  OUTPUT ${PROJECT_BINARY_DIR}/www/index.html
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/www ${PROJECT_BINARY_DIR}/www
  COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/libechoplugin.so ${PROJECT_BINARY_DIR}/www
  COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_BINARY_DIR}/libcoreplugins.so ${PROJECT_BINARY_DIR}/www
  COMMAND sed "s/\@VERSION\@/${VERSION}/" "${PROJECT_SOURCE_DIR}/www/index.html.in" > "${PROJECT_BINARY_DIR}/www/index.html"
  DEPENDS ${tmp} ${PROJECT_SOURCE_DIR}/www/index.html.in echoplugin coreplugins
)
add_custom_target(copy_wwwqmlxml DEPENDS ${PROJECT_BINARY_DIR}/www/index.html ${PROJECT_BINARY_DIR}/CordovaUbuntu.${VERSION} ${PROJECT_BINARY_DIR}/xml ${PROJECT_BINARY_DIR}/qml)

add_custom_target(copy_examples DEPENDS ${PROJECT_BINARY_DIR}/examples)

find_package(Qt5Widgets)
find_package(Qt5Core)

add_executable(cordova-ubuntu-${VERSION}
  main.cpp
)
qt5_use_modules(cordova-ubuntu-${VERSION} Widgets Quick)
add_dependencies(cordova-ubuntu-${VERSION} concatenate_js copy_wwwqmlxml copy_examples)

ADD_LIBRARY(cordovaubuntuplugin SHARED
  src/cplugin.cpp
  src/cordova.cpp
  src/qmlplugin.cpp

  src/cordova.h
  src/cplugin.h
  src/qmlplugin.h
)
qt5_use_modules(cordovaubuntuplugin Widgets Quick)

ADD_LIBRARY(coreplugins SHARED
  src/plugins/accelerometer.h
  src/plugins/cameraresolution.h
  src/plugins/capture.h
  src/plugins/camera.h
  src/plugins/compass.h
  src/plugins/connection.h
  src/plugins/console.h
  src/plugins/contacts.h
  src/plugins/device.h
  src/plugins/events.h
  src/plugins/fileapi.h
  src/plugins/geolocation.h
  src/plugins/globalization.h
  src/plugins/media.h
  src/plugins/notification.h
  src/plugins/app.h
  src/plugins/splashscreen.h
  src/plugins/inappbrowser.h

  src/plugins/accelerometer.cpp
  src/plugins/capture.cpp
  src/plugins/camera.cpp
  src/plugins/compass.cpp
  src/plugins/connection.cpp
  src/plugins/console.cpp
  src/plugins/contacts.cpp
  src/plugins/device.cpp
  src/plugins/events.cpp src/cordova.cpp
  src/plugins/fileapi.cpp
  src/plugins/geolocation.cpp
  src/plugins/globalization.cpp
  src/plugins/media.cpp
  src/plugins/notification.cpp
  src/plugins/app.cpp
  src/plugins/splashscreen.cpp
  src/plugins/inappbrowser.cpp

  src/coreplugins.cpp
)
qt5_use_modules(coreplugins Widgets Location Sensors Feedback SystemInfo Contacts Multimedia Quick MultimediaWidgets)

ADD_LIBRARY(echoplugin SHARED
  echoplugin/echoplugin.cpp
  echoplugin/echoplugin.h
)
qt5_use_modules(echoplugin Core)

find_library(XCB_LIB xcb)
target_link_libraries(cordova-ubuntu-${VERSION} ${XCB_LIB} cordovaubuntuplugin)
target_link_libraries(coreplugins cordovaubuntuplugin)
target_link_libraries(echoplugin cordovaubuntuplugin)

# Qt5's cmake does not export QT_IMPORTS_DIR, lets query qmake on our own for now
get_target_property(QMAKE_EXECUTABLE Qt5::qmake LOCATION)
function(QUERY_QMAKE VAR RESULT)
 exec_program(${QMAKE_EXECUTABLE} ARGS "-query ${VAR}" RETURN_VALUE return_code OUTPUT_VARIABLE output )
 if(NOT return_code)
 file(TO_CMAKE_PATH "${output}" output)
 set(${RESULT} ${output} PARENT_SCOPE)
 endif(NOT return_code)
endfunction(QUERY_QMAKE)
query_qmake(QT_INSTALL_QML QT_IMPORTS_DIR)

INCLUDE(InstallSymlink)

install (TARGETS cordova-ubuntu-${VERSION} DESTINATION bin)
install (TARGETS echoplugin DESTINATION share/cordova-ubuntu-${VERSION}/www)
install (TARGETS coreplugins DESTINATION lib/cordova-ubuntu-${VERSION}/)
install (DIRECTORY ${PROJECT_BINARY_DIR}/www ${PROJECT_BINARY_DIR}/xml DESTINATION share/cordova-ubuntu-${VERSION})
install (FILES ${PROJECT_BINARY_DIR}/www/cordova-${VERSION}.js DESTINATION share/cordova-ubuntu-${VERSION}/www)
install (FILES src/cordova.h src/cplugin.h DESTINATION include/cordova-ubuntu-${VERSION}/)
install (DIRECTORY ${PROJECT_BINARY_DIR}/qml DESTINATION share/cordova-ubuntu-${VERSION})
install (DIRECTORY ${PROJECT_BINARY_DIR}/CordovaUbuntu.${VERSION} DESTINATION ${QT_IMPORTS_DIR}/ PATTERN assets EXCLUDE)
install (DIRECTORY ${PROJECT_BINARY_DIR}/CordovaUbuntu.${VERSION}/assets DESTINATION share/cordova-ubuntu-${VERSION})
install (DIRECTORY ${PROJECT_BINARY_DIR}/examples DESTINATION share/cordova-ubuntu-${VERSION} PATTERN *.desktop EXCLUDE)
install (FILES examples/qrcode-scanner/CordovaExampleQRCodeScanner.desktop DESTINATION share/applications)
InstallSymlink(/usr/share/cordova-ubuntu-${VERSION}/assets ${QT_IMPORTS_DIR}/CordovaUbuntu.${VERSION}/assets)

set (CPACK_PACKAGE_NAME cordova-ubuntu-runtime-${VERSION})
set (CPACK_PACKAGE_VERSION ${VERSION})
set (CPACK_DEBIAN_PACKAGE_DEPENDS "libqt5webkit5-qmlwebkitplugin, qtdeclarative5-qtfeedback-plugin, qtdeclarative5-qtmultimedia-plugin, qtdeclarative5-qtquick2-plugin")
set (CPACK_DEBIAN_PACKAGE_MAINTAINER "Maxim Ermilov(maxim.ermilov@canonical.com)")
set (CPACK_DEBIAN_PACKAGE_DESCRIPTION "Cordova is a free and open source framework that allows you to create mobile apps using standardized web APIs for the platforms you care about.")
INCLUDE(CPack)
