# CMakeLists.txt for the ZoneMinder Perl modules.

# Create configured .pm files from .pm.in templates
configure_file(lib/ZoneMinder/Base.pm.in
  "${CMAKE_CURRENT_BINARY_DIR}/lib/ZoneMinder/Base.pm" @ONLY)
configure_file(lib/ZoneMinder/Config.pm.in
  "${CMAKE_CURRENT_BINARY_DIR}/lib/ZoneMinder/Config.pm" @ONLY)
configure_file(lib/ZoneMinder/Memory.pm.in
  "${CMAKE_CURRENT_BINARY_DIR}/lib/ZoneMinder/Memory.pm" @ONLY)
configure_file(lib/ZoneMinder/ConfigData.pm.in
  "${CMAKE_CURRENT_BINARY_DIR}/lib/ZoneMinder/ConfigData.pm" @ONLY)
configure_file(lib/ZoneMinder/ONVIF.pm.in
  "${CMAKE_CURRENT_BINARY_DIR}/lib/ZoneMinder/ONVIF.pm" @ONLY)

# Install static .pm files from source tree
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib/"
  DESTINATION "${ZM_PERL_INSTALL_PATH}"
  FILES_MATCHING PATTERN "*.pm")

# Install configured .pm files from build tree
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib/"
  DESTINATION "${ZM_PERL_INSTALL_PATH}"
  FILES_MATCHING PATTERN "*.pm")

# Generate man3 pages for modules with verified documentation.
# Only modules whose POD was audited and confirmed accurate are listed.
# Memory.pm is generated from .pm.in so its source is in the build tree.
if(BUILD_MAN)
  find_program(POD2MAN pod2man)
  find_program(GZIP gzip)
  if(POD2MAN AND GZIP)
    set(_man3_modules
      ZoneMinder/Memory.pm
      ZoneMinder/Logger.pm
      ZoneMinder/Control/Dahua.pm
      ZoneMinder/Control/Reolink_HTTP.pm
      ZoneMinder/Control/Amcrest_HTTP.pm
      ZoneMinder/Control/Trendnet.pm
      ZoneMinder/Control/Instar720p.pm)

    # Build output uses "/" (safe for GNU make); installed name uses "::" (Perl convention).
    # GNU make treats "::" as double-colon rule syntax, so build-time filenames must avoid it.
    set(_man3_outputs)
    foreach(_pm_rel IN LISTS _man3_modules)
      # Derive Perl man page name: ZoneMinder/Control/Dahua.pm -> ZoneMinder::Control::Dahua
      string(REPLACE "/" "::" _man_name "${_pm_rel}")
      string(REPLACE ".pm" "" _man_name "${_man_name}")

      # Build-time filename uses "/" separators (make-safe)
      string(REPLACE ".pm" "" _build_stem "${_pm_rel}")

      # Memory.pm is generated by configure_file; others are static source files
      if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/lib/${_pm_rel}")
        set(_pm_src "${CMAKE_CURRENT_SOURCE_DIR}/lib/${_pm_rel}")
      else()
        set(_pm_src "${CMAKE_CURRENT_BINARY_DIR}/lib/${_pm_rel}")
      endif()

      set(_man_dir "${CMAKE_CURRENT_BINARY_DIR}/man3")
      set(_man_out "${_man_dir}/${_build_stem}.3pm")
      set(_man_gz "${_man_out}.gz")

      get_filename_component(_man_out_dir "${_man_out}" DIRECTORY)
      file(MAKE_DIRECTORY "${_man_out_dir}")

      add_custom_command(OUTPUT "${_man_out}"
        DEPENDS "${_pm_src}"
        COMMAND ${POD2MAN} --section 3pm --center "${CMAKE_PROJECT_NAME}"
          --release --stderr --name "${_man_name}"
          "${_pm_src}" > "${_man_out}")

      add_custom_command(OUTPUT "${_man_gz}"
        DEPENDS "${_man_out}"
        COMMAND ${GZIP} -n --best -c "${_man_out}" > "${_man_gz}")

      list(APPEND _man3_outputs "${_man_gz}")

      # Install with Perl :: convention in filename
      install(FILES "${_man_gz}"
        DESTINATION "${ZM_MANPAGE_DEST_PREFIX}/man3"
        RENAME "${_man_name}.3pm.gz")
    endforeach()

    add_custom_target(perl-man3 ALL DEPENDS ${_man3_outputs})
  endif()
endif()
