# FindPAM.cmake - Find PAM (Pluggable Authentication Modules) library
#
# This module defines:
#  PAM_FOUND - True if PAM is found
#  PAM_INCLUDE_DIRS - Include directories for PAM
#  PAM_LIBRARIES - Libraries to link against
#  PAM_VERSION - Version of PAM (if available)

# Try to find PAM using pkg-config first
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
    pkg_check_modules(PAM QUIET pam)
endif()

# If pkg-config didn't find it, try manual search
if(NOT PAM_FOUND)
    # Find the header file
    find_path(PAM_INCLUDE_DIR
        NAMES security/pam_appl.h pam/pam_appl.h
        PATHS
            /usr/include
            /usr/local/include
            /opt/local/include
            /sw/include
        DOC "PAM include directory"
    )

    # Find the library
    find_library(PAM_LIBRARY
        NAMES pam libpam
        PATHS
            /usr/lib
            /usr/local/lib
            /opt/local/lib
            /sw/lib
            /usr/lib/x86_64-linux-gnu
            /usr/lib/aarch64-linux-gnu
            /usr/lib/arm-linux-gnueabihf
        DOC "PAM library"
    )

    # Set the variables
    if(PAM_INCLUDE_DIR AND PAM_LIBRARY)
        set(PAM_FOUND TRUE)
        set(PAM_INCLUDE_DIRS ${PAM_INCLUDE_DIR})
        set(PAM_LIBRARIES ${PAM_LIBRARY})
    endif()
endif()

# Try to get version information
if(PAM_FOUND)
    # Try to extract version from header
    if(EXISTS "${PAM_INCLUDE_DIRS}/security/pam_appl.h")
        file(READ "${PAM_INCLUDE_DIRS}/security/pam_appl.h" PAM_HEADER_CONTENT)
        string(REGEX MATCH "PAM_VERSION_MAJOR[ ]+([0-9]+)" PAM_VERSION_MAJOR_MATCH "${PAM_HEADER_CONTENT}")
        string(REGEX MATCH "PAM_VERSION_MINOR[ ]+([0-9]+)" PAM_VERSION_MINOR_MATCH "${PAM_HEADER_CONTENT}")
        string(REGEX MATCH "PAM_VERSION_PATCH[ ]+([0-9]+)" PAM_VERSION_PATCH_MATCH "${PAM_HEADER_CONTENT}")

        if(PAM_VERSION_MAJOR_MATCH AND PAM_VERSION_MINOR_MATCH AND PAM_VERSION_PATCH_MATCH)
            string(REGEX REPLACE "PAM_VERSION_MAJOR[ ]+([0-9]+)" "\\1" PAM_VERSION_MAJOR "${PAM_VERSION_MAJOR_MATCH}")
            string(REGEX REPLACE "PAM_VERSION_MINOR[ ]+([0-9]+)" "\\1" PAM_VERSION_MINOR "${PAM_VERSION_MINOR_MATCH}")
            string(REGEX REPLACE "PAM_VERSION_PATCH[ ]+([0-9]+)" "\\1" PAM_VERSION_PATCH "${PAM_VERSION_PATCH_MATCH}")
            set(PAM_VERSION "${PAM_VERSION_MAJOR}.${PAM_VERSION_MINOR}.${PAM_VERSION_PATCH}")
        endif()
    endif()

    # Fallback: try to get version from pkg-config
    if(NOT PAM_VERSION AND PKG_CONFIG_FOUND)
        pkg_get_variable(PAM_VERSION pam version)
    endif()

    # If still no version, set a default
    if(NOT PAM_VERSION)
        set(PAM_VERSION "Unknown")
    endif()
endif()

# Handle the QUIETLY and REQUIRED arguments
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PAM
    REQUIRED_VARS PAM_LIBRARIES PAM_INCLUDE_DIRS
    VERSION_VAR PAM_VERSION
)

# Create imported target if found
if(PAM_FOUND AND NOT TARGET PAM::PAM)
    add_library(PAM::PAM UNKNOWN IMPORTED)
    set_target_properties(PAM::PAM PROPERTIES
        IMPORTED_LOCATION "${PAM_LIBRARIES}"
        INTERFACE_INCLUDE_DIRECTORIES "${PAM_INCLUDE_DIRS}"
    )
endif()

# Mark variables as advanced
mark_as_advanced(PAM_INCLUDE_DIR PAM_LIBRARY)