# First, set up registration functions for the kinds of resources we handle.
set(resource_root ${CMAKE_CURRENT_SOURCE_DIR}/)
set(resource_list)
if(WIN32)
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/win32/versioninfo.rc.in
                   ${CMAKE_CURRENT_BINARY_DIR}/win32/versioninfo.rc)

    set(rc_file ${CMAKE_CURRENT_BINARY_DIR}/resources.rc)
    file(WRITE  ${rc_file} "// Autogenerated; do not edit\n")
    file(APPEND ${rc_file} "#include <windows.h>\n")
    file(APPEND ${rc_file} "#include \"${CMAKE_CURRENT_BINARY_DIR}/win32/versioninfo.rc\"\n")

    function(add_resource name)
        set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})

        if(${ARGC} GREATER 1)
            set(id ${ARGV1})
        else()
            string(REPLACE ${resource_root} "" id ${source})
        endif()
        if(${ARGC} GREATER 2)
            set(type ${ARGV2})
        else()
            set(type RCDATA)
        endif()
        file(SHA512 "${source}" hash)
        file(APPEND ${rc_file} "${id} ${type} \"${source}\" // ${hash}\n")
        # CMake doesn't track file dependencies across directories, so we force
        # a reconfigure (which changes the RC file because of the hash above)
        # every time a resource is changed.
        set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${source}")
    endfunction()
elseif(APPLE)
    set(app_resource_dir ${CMAKE_BINARY_DIR}/bin/solvespace.app/Contents/Resources)
    set(cli_resource_dir ${CMAKE_BINARY_DIR}/res)

    function(add_resource name)
        set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
        set(target_app ${app_resource_dir}/${name})
        set(target_cli ${cli_resource_dir}/${name})
        set(resource_list "${resource_list};${target_app};${target_cli}" PARENT_SCOPE)

        get_filename_component(target_app_dir ${target_app} DIRECTORY)
        get_filename_component(target_cli_dir ${target_cli} DIRECTORY)
        add_custom_command(
            OUTPUT ${target_app} ${target_cli}
            COMMAND ${CMAKE_COMMAND} -E make_directory ${target_app_dir}
            COMMAND ${CMAKE_COMMAND} -E copy ${source} ${target_app}
            COMMAND ${CMAKE_COMMAND} -E make_directory ${target_cli_dir}
            COMMAND ${CMAKE_COMMAND} -E copy ${source} ${target_cli}
            COMMENT "Copying resource ${name}"
            DEPENDS ${source}
            VERBATIM)
    endfunction()

    function(add_xib name)
        set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
        get_filename_component(basename ${name} NAME_WE)
        set(target ${app_resource_dir}/${basename}.nib)
        set(resource_list "${resource_list};${target}" PARENT_SCOPE)

        add_custom_command(
            OUTPUT ${target}
            COMMAND ${CMAKE_COMMAND} -E make_directory ${app_resource_dir}
            COMMAND ibtool --errors --warnings --notices --output-format human-readable-text
                --compile ${target} ${source}
            COMMENT "Building Interface Builder file ${name}"
            DEPENDS ${source}
            VERBATIM)
    endfunction()

    function(add_iconset name)
        set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
        get_filename_component(basename ${name} NAME_WE)
        set(target ${app_resource_dir}/${basename}.icns)
        set(resource_list "${resource_list};${target}" PARENT_SCOPE)

        add_custom_command(
            OUTPUT ${target}
            COMMAND ${CMAKE_COMMAND} -E make_directory ${app_resource_dir}
            COMMAND iconutil -c icns -o ${target} ${source}
            COMMENT "Building icon set ${name}"
            DEPENDS ${source}
            VERBATIM)
    endfunction()
else() # Unix
    include(GNUInstallDirs)

    set(app_resource_dir ${CMAKE_BINARY_DIR}/res)

    function(add_resource name)
        set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
        set(target ${app_resource_dir}/${name})
        set(resource_list "${resource_list};${target}" PARENT_SCOPE)

        get_filename_component(target_dir ${target} DIRECTORY)
        add_custom_command(
            OUTPUT ${target}
            COMMAND ${CMAKE_COMMAND} -E make_directory ${target_dir}
            COMMAND ${CMAKE_COMMAND} -E copy ${source} ${target}
            COMMENT "Copying resource ${name}"
            DEPENDS ${source}
            VERBATIM)

        get_filename_component(name_dir ${name} DIRECTORY)
        install(FILES ${source}
            DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/solvespace/${name_dir})
    endfunction()
endif()

function(add_resources)
    foreach(name ${ARGN})
        add_resource(${name})
        set(resource_list "${resource_list}" PARENT_SCOPE)
    endforeach()
endfunction()

# Second, register all resources.
if(WIN32)
    add_resource(win32/icon.ico     4000 ICON)
    add_resource(win32/manifest.xml 2    RT_MANIFEST)
elseif(APPLE)
    add_iconset (cocoa/AppIcon.iconset)
    add_xib     (cocoa/MainMenu.xib)
    add_xib     (cocoa/SaveFormatAccessory.xib)
else()
    add_resource(freedesktop/solvespace-48x48.png)

    configure_file(
        ${CMAKE_CURRENT_SOURCE_DIR}/freedesktop/solvespace.desktop.in
        ${CMAKE_CURRENT_BINARY_DIR}/freedesktop/solvespace.desktop)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/freedesktop/solvespace.desktop
        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications)

    foreach(SIZE 16x16 24x24 32x32 48x48)
        install(FILES freedesktop/solvespace-${SIZE}.png
            DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/${SIZE}/apps
            RENAME      solvespace.png)
        install(FILES freedesktop/solvespace-${SIZE}.png
            DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/${SIZE}/mimetypes
            RENAME      application.x-solvespace.png)
    endforeach()

    foreach(SIZE 16x16 24x24 32x32 48x48)
        install(FILES freedesktop/solvespace-${SIZE}.xpm
            DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pixmaps)
    endforeach()
endif()

add_resources(
    banner.txt
    icons/graphics-window/angle.png
    icons/graphics-window/arc.png
    icons/graphics-window/assemble.png
    icons/graphics-window/bezier.png
    icons/graphics-window/circle.png
    icons/graphics-window/construction.png
    icons/graphics-window/equal.png
    icons/graphics-window/extrude.png
    icons/graphics-window/horiz.png
    icons/graphics-window/in3d.png
    icons/graphics-window/lathe.png
    icons/graphics-window/length.png
    icons/graphics-window/line.png
    icons/graphics-window/ontoworkplane.png
    icons/graphics-window/other-supp.png
    icons/graphics-window/parallel.png
    icons/graphics-window/perpendicular.png
    icons/graphics-window/pointonx.png
    icons/graphics-window/point.png
    icons/graphics-window/rectangle.png
    icons/graphics-window/ref.png
    icons/graphics-window/same-orientation.png
    icons/graphics-window/sketch-in-3d.png
    icons/graphics-window/sketch-in-plane.png
    icons/graphics-window/step-rotate.png
    icons/graphics-window/step-translate.png
    icons/graphics-window/symmetric.png
    icons/graphics-window/tangent-arc.png
    icons/graphics-window/text.png
    icons/graphics-window/trim.png
    icons/graphics-window/vert.png
    icons/text-window/constraint.png
    icons/text-window/edges.png
    icons/text-window/faces.png
    icons/text-window/occluded-visible.png
    icons/text-window/occluded-stippled.png
    icons/text-window/occluded-invisible.png
    icons/text-window/mesh.png
    icons/text-window/normal.png
    icons/text-window/outlines.png
    icons/text-window/point.png
    icons/text-window/shaded.png
    icons/text-window/workplane.png
    locales.txt
    locales/en_US.po
    locales/uk_UA.po
    fonts/unifont.hex.gz
    fonts/private/0-check-false.png
    fonts/private/1-check-true.png
    fonts/private/2-radio-false.png
    fonts/private/3-radio-true.png
    fonts/private/4-stipple-dot.png
    fonts/private/5-stipple-dash-long.png
    fonts/private/6-stipple-dash.png
    fonts/private/7-stipple-zigzag.png
    fonts/unicode.lff.gz
    shaders/imesh.frag
    shaders/imesh.vert
    shaders/imesh_point.frag
    shaders/imesh_point.vert
    shaders/imesh_tex.frag
    shaders/imesh_texa.frag
    shaders/imesh_tex.vert
    shaders/mesh.frag
    shaders/mesh.vert
    shaders/mesh_fill.frag
    shaders/mesh_fill.vert
    shaders/edge.frag
    shaders/edge.vert
    shaders/outline.vert
    threejs/three-r76.js.gz
    threejs/hammer-2.0.8.js.gz
    threejs/SolveSpaceControls.js)

# Third, distribute the resources.
add_custom_target(resources
    DEPENDS ${resource_list})
if(WIN32)
    set_property(TARGET resources PROPERTY EXTRA_SOURCES ${rc_file})
endif()
