# Optional CUPS command line utility support for SMArT WebUI.
#
# This module intentionally searches for the CUPS command line utilities
# used by the WebUI, not for libcups development headers/libraries.
#
# Variables exported:
#
#   WITH_CUPSUTILS
#       User option to enable/disable CUPS utility discovery.
#
#   HAVE_CUPSUTILS
#       ON when the required CUPS utilities were found.
#
#   SMART_CUPS_ENABLE
#       1 when HAVE_CUPSUTILS is ON, otherwise 0.  Intended for configure_file()
#       into Perl config files.
#
#   CUPS_LPSTAT_EXECUTABLE
#       Path to lpstat.
#
#   CUPS_LP_EXECUTABLE
#       Path to lp.
#
#   CUPS_LPR_EXECUTABLE
#       Optional path to lpr, if available.

option(WITH_CUPSUTILS "Enable CUPS utility support for SMArT WebUI" ON)

set(HAVE_CUPSUTILS OFF)
set(SMART_CUPS_ENABLE 0)

if(WITH_CUPSUTILS)
    find_program(CUPS_LPSTAT_EXECUTABLE NAMES lpstat)
    find_program(CUPS_LP_EXECUTABLE NAMES lp)
    find_program(CUPS_LPR_EXECUTABLE NAMES lpr)

    if(CUPS_LPSTAT_EXECUTABLE AND CUPS_LP_EXECUTABLE)
        set(HAVE_CUPSUTILS ON)
        set(SMART_CUPS_ENABLE 1)
        message(STATUS "Found CUPS utilities: lpstat=${CUPS_LPSTAT_EXECUTABLE}, lp=${CUPS_LP_EXECUTABLE}")
        if(CUPS_LPR_EXECUTABLE)
            message(STATUS "Found optional CUPS lpr utility: ${CUPS_LPR_EXECUTABLE}")
        endif()
    else()
        message(STATUS "CUPS utilities not found; SMArT WebUI CUPS printer discovery disabled")
    endif()
else()
    message(STATUS "CUPS utility support disabled")
endif()

# Provide stable fallback strings for configure_file() consumers.  These are
# only used when SMART_CUPS_ENABLE is 0 or when the admin later overrides the
# paths in smart.conf.
if(NOT CUPS_LPSTAT_EXECUTABLE)
    set(CUPS_LPSTAT_EXECUTABLE "/usr/bin/lpstat")
endif()

if(NOT CUPS_LP_EXECUTABLE)
    set(CUPS_LP_EXECUTABLE "/usr/bin/lp")
endif()

if(NOT CUPS_LPR_EXECUTABLE)
    set(CUPS_LPR_EXECUTABLE "/usr/bin/lpr")
endif()
