# SPDX-License-Identifier: GPL-3.0-or-later
# myMPD (c) 2018-2026 Juergen Mang <mail@jcgames.de>
# https://github.com/jcorporation/mympd

# Install startup script
function(install_systemd)
  file(INSTALL "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/system/mympd.service"
       DESTINATION "/usr/lib/systemd/system"
       FILE_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
  file(INSTALL "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/user/mympd.service"
       DESTINATION "/usr/lib/systemd/user"
       FILE_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
endfunction()

function(install_openrc)
  file(RENAME "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/mympd.openrc"
       "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/mympd")
  file(INSTALL "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/mympd"
       DESTINATION "/etc/init.d"
       FILE_PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
endfunction()

function(install_sysvinit)
  file(RENAME "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/mympd.sysVinit"
       "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/mympd")
  file(INSTALL "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/mympd"
       DESTINATION "/etc/init.d"
       FILE_PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
endfunction()

function(install_freebsd)
  file(RENAME "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/mympd.freebsdrc"
       "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/mympd")
  file(INSTALL "@CMAKE_CURRENT_BINARY_DIR@/contrib/initscripts/mympd"
       DESTINATION "/usr/local/etc/rc.d"
       FILE_PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
endfunction()

if("@MYMPD_STARTUP_SCRIPT@" STREQUAL "ON")
  # Try to detect installes init system
  if(EXISTS "/lib/systemd/system" OR EXISTS "/usr/lib/systemd/system")
    install_systemd()
  elseif(EXISTS "/sbin/openrc-run" OR EXISTS "/usr/sbin/openrc-run")
    install_openrc()
  elseif(EXISTS "/etc/init.d")
    install_sysvinit()
  elseif(EXISTS "/bin/freebsd-version")
    install_freebsd()
  else()
    message("No supported init system found, no startup script was installed")
  endif()
elseif("@MYMPD_STARTUP_SCRIPT@" STREQUAL "SYSTEMD")
  install_systemd()
elseif("@MYMPD_STARTUP_SCRIPT@" STREQUAL "OPENRC")
  install_openrc()
elseif("@MYMPD_STARTUP_SCRIPT@" STREQUAL "SYSVINIT")
  install_sysvinit()
elseif("@MYMPD_STARTUP_SCRIPT@" STREQUAL "FREEBSD")
  install_freebsd()
endif()

# Install and compress manpages
if("@MYMPD_MANPAGES@" STREQUAL "ON")
  execute_process(COMMAND gzip -n -c -9 "@CMAKE_SOURCE_DIR@/contrib/man/mympd.1" OUTPUT_FILE "@CMAKE_CURRENT_BINARY_DIR@/contrib/mympd.1.gz")
  file(INSTALL "@CMAKE_CURRENT_BINARY_DIR@/contrib/mympd.1.gz"
       DESTINATION "@CMAKE_INSTALL_FULL_MANDIR@/man1"
       FILE_PERMISSIONS PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)

  execute_process(COMMAND gzip -n -c -9 "@CMAKE_SOURCE_DIR@/contrib/man/mympd-config.1" OUTPUT_FILE "@CMAKE_CURRENT_BINARY_DIR@/contrib/mympd-config.1.gz")
  file(INSTALL "@CMAKE_CURRENT_BINARY_DIR@/contrib/mympd-config.1.gz"
       DESTINATION "@CMAKE_INSTALL_FULL_MANDIR@/man1"
       FILE_PERMISSIONS PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)

  execute_process(COMMAND gzip -n -c -9 "@CMAKE_SOURCE_DIR@/contrib/man/mympd-script.1" OUTPUT_FILE "@CMAKE_CURRENT_BINARY_DIR@/contrib/mympd-script.1.gz")
  file(INSTALL "@CMAKE_CURRENT_BINARY_DIR@/contrib/mympd-script.1.gz"
       DESTINATION "@CMAKE_INSTALL_FULL_MANDIR@/man1"
       FILE_PERMISSIONS PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
endif()

# Install documentation
if("@MYMPD_DOC@" STREQUAL "ON")
  file(INSTALL "@CMAKE_SOURCE_DIR@/CHANGELOG.md" "@CMAKE_SOURCE_DIR@/LICENSE.md" "@CMAKE_SOURCE_DIR@/README.md" "@CMAKE_SOURCE_DIR@/SECURITY.md"
       DESTINATION "@CMAKE_INSTALL_FULL_DOCDIR@"
       FILE_PERMISSIONS PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
endif()

# Install html documentation
if("@MYMPD_DOC_HTML@" STREQUAL "ON")
  file(INSTALL "@CMAKE_CURRENT_BINARY_DIR@/htmldoc"
       DESTINATION "@CMAKE_INSTALL_FULL_DOCDIR@/html"
       FILE_PERMISSIONS PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
endif()
