#
# libwebsockets - small server side websockets and web server implementation
#
# Copyright (C) 2010 - 2026 Andy Green <andy@warmcat.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#

# The folded-in jwt auth used for the "hls:2" grant check needs JOSE

set(requirements 1)
require_lws_config(LWS_WITH_JOSE 1 requirements)

if (requirements)

	find_package(PkgConfig QUIET)
	pkg_check_modules(PC_AVFORMAT libavformat)
	pkg_check_modules(PC_AVCODEC libavcodec)
	pkg_check_modules(PC_AVUTIL libavutil)
	pkg_check_modules(PC_SWSCALE libswscale)
	pkg_check_modules(PC_SWRESAMPLE libswresample)

	if (PC_AVFORMAT_FOUND AND PC_AVCODEC_FOUND AND PC_AVUTIL_FOUND AND PC_SWSCALE_FOUND AND PC_SWRESAMPLE_FOUND)
		set(PLUGIN_NAME "protocol_lws_hls")
		set(PLUGIN_SRCS
			protocol_lws_hls.c
			hls-atrans.c
			hls-av.c
			hls-dir.c
			hls-index.c
			hls-media.c
			hls-sub.c
		)
		set(PLUGIN_HDR private-lws-hls.h)

		# ffmpeg libs and includes, for the standalone plugin target and
		# for the lib itself when we are built into it (create_plugin
		# forwards these to the lib build in that case)
		set(PLUGIN_LINK_LIBS
			${PC_AVFORMAT_LIBRARIES}
			${PC_AVCODEC_LIBRARIES}
			${PC_AVUTIL_LIBRARIES}
			${PC_SWSCALE_LIBRARIES}
			${PC_SWRESAMPLE_LIBRARIES})
		set(PLUGIN_INCLUDE "")
		foreach(_d ${PC_AVFORMAT_INCLUDE_DIRS} ${PC_AVCODEC_INCLUDE_DIRS}
			   ${PC_AVUTIL_INCLUDE_DIRS} ${PC_SWSCALE_INCLUDE_DIRS}
			   ${PC_SWRESAMPLE_INCLUDE_DIRS})
			list(APPEND PLUGIN_INCLUDE ${_d})
		endforeach()

		# Create the plugin target using the macro from plugins/CMakeLists.txt
		create_plugin(${PLUGIN_NAME}
			      "${PLUGIN_INCLUDE}"
			      "${PLUGIN_SRCS}"
			      ""
			      ""
		)

		if (TARGET ${PLUGIN_NAME})
			target_compile_definitions(${PLUGIN_NAME} PRIVATE LWS_BUILDING_SHARED)
			target_include_directories(${PLUGIN_NAME} PRIVATE
				${PC_AVFORMAT_INCLUDE_DIRS}
				${PC_AVCODEC_INCLUDE_DIRS}
				${PC_AVUTIL_INCLUDE_DIRS}
				${PC_SWSCALE_INCLUDE_DIRS}
				${PC_SWRESAMPLE_INCLUDE_DIRS})
			target_link_libraries(${PLUGIN_NAME} ${PLUGIN_LINK_LIBS})
		endif()
	endif()

endif()
