#  Copyright (C) 2026 ChiPass Team <contact@chipass.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 or (at your option)
#  version 3 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

# While freedesktop.org makes no such requirement, Flatpak requires all public files to be
# prefixed with the full application name. This also works outside of Flatpak, so for simplicity
# we use the prefixed names everywhere.

# Changelog
file(READ ${CMAKE_SOURCE_DIR}/CHANGELOG.md changelog_md)
string(REPLACE "~SNAPSHOT" "~git-${CHIPASS_CHECKOUT_INFO}" changelog_md "${changelog_md}")
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/CHANGELOG.md CONTENT "${changelog_md}")

# Application manifest
find_program(APPSTREAMCLI appstreamcli REQUIRED)
add_custom_target(generate_metainfo ALL
  COMMAND ${APPSTREAMCLI} news-to-metainfo
    ${CMAKE_CURRENT_BINARY_DIR}/CHANGELOG.md
    ${CMAKE_CURRENT_SOURCE_DIR}/ChiPass.metainfo.xml.in
    ${CMAKE_CURRENT_BINARY_DIR}/ChiPass.metainfo.xml
  VERBATIM
  COMMENT "Generating AppStream metadata"
)
install(
  FILES ${CMAKE_CURRENT_BINARY_DIR}/ChiPass.metainfo.xml
  DESTINATION ${CMAKE_INSTALL_DATADIR}/metainfo
  RENAME ${PROJECT_APP_ID}.metainfo.xml
)

# Desktop file and its associated icon
install(
  FILES ChiPass.desktop
  DESTINATION ${CMAKE_INSTALL_DATADIR}/applications/
  RENAME ${PROJECT_APP_ID}.desktop
)
install(
  FILES ../icons/application/256x256/apps/chipass.png
  DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/256x256/apps/
  RENAME ${PROJECT_APP_ID}.png
)

# MIME type file and its associated icon
install(
  FILES mimetype.xml
  DESTINATION ${CMAKE_INSTALL_DATADIR}/mime/packages/
  RENAME ${PROJECT_APP_ID}-application-x-chipass.xml
)
install(
  FILES ../icons/application/scalable/mimetypes/application-x-chipass.svg
  DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/mimetypes/
  RENAME ${PROJECT_APP_ID}-application-x-chipass.svg
)

# Status notifier icons
file(GLOB status_icons LIST_DIRECTORIES false "../icons/application/scalable/apps/chipass*.svg")
foreach (status_icon_path ${status_icons})
  cmake_path(GET status_icon_path FILENAME status_icon_filename)
  install(
    FILES ${status_icon_path}
    DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps/
    RENAME ${PROJECT_APP_ID}-${status_icon_filename}
  )
endforeach()
