find_path(HTTPD_INCLUDE_DIR
  NAMES httpd.h
  PATH_SUFFIXES
    include
)

find_library(HTTPD_LIBRARY
  NAMES libhttpd
  PATH_SUFFIXES lib
)

find_file(HTTPD_DLL
  NAMES libhttpd.dll
  PATH_SUFFIXES bin
)

find_library(MOD_DAV_LIBRARY
  NAMES mod_dav
  PATH_SUFFIXES lib
)

if (HTTPD_INCLUDE_DIR AND EXISTS "${HTTPD_INCLUDE_DIR}/ap_release.h")
  file(
    STRINGS "${HTTPD_INCLUDE_DIR}/ap_release.h" VERSION_STRINGS
    REGEX "#define (AP_SERVER_MAJORVERSION_NUMBER|AP_SERVER_MINORVERSION_NUMBER|AP_SERVER_PATCHLEVEL_NUMBER)"
  )

  string(REGEX REPLACE ".*AP_SERVER_MAJORVERSION_NUMBER +([0-9]+).*" "\\1" HTTPD_VERSION_MAJOR ${VERSION_STRINGS})
  string(REGEX REPLACE ".*AP_SERVER_MINORVERSION_NUMBER +([0-9]+).*" "\\1" HTTPD_VERSION_MINOR ${VERSION_STRINGS})
  string(REGEX REPLACE ".*AP_SERVER_PATCHLEVEL_NUMBER +([0-9]+).*" "\\1" HTTPD_VERSION_PATCH ${VERSION_STRINGS})

  set(HTTPD_VERSION "${HTTPD_VERSION_MAJOR}.${HTTPD_VERSION_MINOR}.${HTTPD_VERSION_PATCH}")
endif()

FIND_PACKAGE_HANDLE_STANDARD_ARGS(
  Httpd
  REQUIRED_VARS
    HTTPD_LIBRARY
    HTTPD_INCLUDE_DIR
    MOD_DAV_LIBRARY
  VERSION_VAR
    HTTPD_VERSION
)

if(HTTPD_FOUND)
  if(NOT TARGET httpd::httpd)
    add_library(httpd::httpd SHARED IMPORTED)
    set_target_properties(httpd::httpd PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES ${HTTPD_INCLUDE_DIR}
      IMPORTED_LOCATION ${HTTPD_DLL}
      IMPORTED_IMPLIB ${HTTPD_LIBRARY}
    )
  endif()

  if(NOT TARGET httpd::mod_dav)
    add_library(httpd::mod_dav STATIC IMPORTED)
    set_target_properties(httpd::mod_dav PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES ${HTTPD_INCLUDE_DIR}
      IMPORTED_LOCATION ${MOD_DAV_LIBRARY}
    )
  endif()
endif()
