# SPDX-License-Identifier: LGPL-2.1-or-later
# -----------------------------------------------------------------------------
# libcockpit-ws.a: code used in cockpit-ws and its tests

noinst_LIBRARIES += libcockpit-ws.a

libcockpit_ws_a_CPPFLAGS = \
	-DG_LOG_DOMAIN=\"cockpit-ws\" \
	$(glib_CFLAGS) \
	$(json_glib_CFLAGS) \
	$(AM_CPPFLAGS)

libcockpit_ws_a_LIBS = \
	libcockpit-ws.a \
	$(libcockpit_common_a_LIBS) \
	$(glib_LIBS) \
	$(json_glib_LIBS) \
	$(libsystemd_LIBS) \
	-lutil \
	-lcrypt \
	$(NULL)

libcockpit_ws_a_SOURCES = \
	src/ws/cockpitauth.c \
	src/ws/cockpitauth.h \
	src/ws/cockpitbranding.c \
	src/ws/cockpitbranding.h \
	src/ws/cockpitchannel.c \
	src/ws/cockpitchannel.h \
	src/ws/cockpitchannelresponse.c \
	src/ws/cockpitchannelresponse.h \
	src/ws/cockpitchannelsocket.c \
	src/ws/cockpitchannelsocket.h \
	src/ws/cockpitcontrolmessages.c \
	src/ws/cockpitcontrolmessages.h \
	src/ws/cockpitcreds.h src/ws/cockpitcreds.c \
	src/ws/cockpiterror.c \
	src/ws/cockpiterror.h \
	src/ws/cockpitflow.c \
	src/ws/cockpitflow.h \
	src/ws/cockpithandlers.c \
	src/ws/cockpithandlers.h \
	src/ws/cockpithash.c \
	src/ws/cockpithash.h \
	src/ws/cockpitjson.c \
	src/ws/cockpitjson.h \
	src/ws/cockpitlocale.c \
	src/ws/cockpitlocale.h \
	src/ws/cockpitmemfdread.c \
	src/ws/cockpitmemfdread.h \
	src/ws/cockpitpipe.c \
	src/ws/cockpitpipe.h \
	src/ws/cockpitpipetransport.c \
	src/ws/cockpitpipetransport.h \
	src/ws/cockpitsocket.c \
	src/ws/cockpitsocket.h \
	src/ws/cockpitsystem.c \
	src/ws/cockpitsystem.h \
	src/ws/cockpittemplate.c \
	src/ws/cockpittemplate.h \
	src/ws/cockpittransport.c \
	src/ws/cockpittransport.h \
	src/ws/cockpitunicode.c \
	src/ws/cockpitunicode.h \
	src/ws/cockpitwebfilter.c \
	src/ws/cockpitwebfilter.h \
	src/ws/cockpitwebinject.c \
	src/ws/cockpitwebinject.h \
	src/ws/cockpitwebrequest-private.h \
	src/ws/cockpitwebresponse.c \
	src/ws/cockpitwebresponse.h \
	src/ws/cockpitwebserver.c \
	src/ws/cockpitwebserver.h \
	src/ws/cockpitwebservice.c \
	src/ws/cockpitwebservice.h \
	src/ws/cockpitws.h \
	src/ws/websocket.c \
	src/ws/websocket.h \
	src/ws/websocketclient.c \
	src/ws/websocketclient.h \
	src/ws/websocketconnection.c \
	src/ws/websocketconnection.h \
	src/ws/websocketprivate.h \
	src/ws/websocketserver.c \
	src/ws/websocketserver.h \
	$(NULL)

# libcockpit-ws.a static-links an HTML template to use on failures
nodist_libcockpit_ws_a_SOURCES = src/ws/fail-html.c
src/ws/fail-html.c: src/common/fail.html
	@mkdir -p '$(dir $@)'
	$(AM_V_GEN) $(top_srcdir)/tools/escape-to-c cockpit_webresponse_fail_html_text < $< > $@.tmp && mv $@.tmp $@
CLEANFILES += src/ws/fail-html.c

# -----------------------------------------------------------------------------
# cockpit-ws

cockpitwsdir = $(libexecdir)
cockpitws_PROGRAMS = cockpit-ws

cockpit_ws_SOURCES = \
	src/ws/main.c \
	$(NULL)

cockpit_ws_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS)
cockpit_ws_LDADD = $(libcockpit_ws_a_LIBS)

# -----------------------------------------------------------------------------
# test-server: server for running the html/browser unit tests against

check_PROGRAMS += test-server

test_server_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_server_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)

test_server_SOURCES = \
	src/ws/mock-service.c \
	src/ws/mock-service.h \
	src/ws/test-server.c \
	$(NULL)

test_server_CPPFLAGS += -I$(top_builddir)/src/ws
nodist_test_server_SOURCES = $(GDBUS_CODEGEN_GENERATED)

BUILT_SOURCES += $(GDBUS_CODEGEN_GENERATED)
CLEANFILES += $(GDBUS_CODEGEN_GENERATED)
GDBUS_CODEGEN_GENERATED = \
	src/ws/mock-dbus-tests.h \
	src/ws/mock-dbus-tests.c \
	$(NULL)

EXTRA_DIST += $(GDBUS_CODEGEN_XML)
GDBUS_CODEGEN_XML = $(srcdir)/src/ws/com.redhat.Cockpit.DBusTests.xml

GDBUS_CODEGEN_INVOCATION = \
	$(AM_V_GEN) gdbus-codegen \
	--interface-prefix com.redhat.Cockpit.DBusTests \
	--c-namespace Test \
	--c-generate-object-manager \
	$(NULL)

src/ws/mock-dbus-tests.h: $(GDBUS_CODEGEN_XML)
	$(GDBUS_CODEGEN_INVOCATION) --header --output $@ $<

src/ws/mock-dbus-tests.c: $(GDBUS_CODEGEN_XML)
	$(GDBUS_CODEGEN_INVOCATION) --body --output $@ $<

# -----------------------------------------------------------------------------
# Unit tests

dist_check_DATA += src/ws/mock-content
check_SCRIPTS += src/ws/mock-cat-with-init

check_PROGRAMS += frob-websocket
frob_websocket_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
frob_websocket_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
frob_websocket_SOURCES = src/ws/frob-websocket.c

TEST_PROGRAM += test-websocket
test_websocket_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_websocket_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_websocket_SOURCES = src/ws/test-websocket.c

TEST_PROGRAM += test-channel
test_channel_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_channel_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_channel_SOURCES = src/ws/test-channel.c

TEST_PROGRAM += test-hash
test_hash_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_hash_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_hash_SOURCES = src/ws/test-hash.c

TEST_PROGRAM += test-json
test_json_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_json_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_json_SOURCES = src/ws/test-json.c

TEST_PROGRAM += test-jsonfds
test_jsonfds_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_jsonfds_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_jsonfds_SOURCES = src/ws/test-jsonfds.c

TEST_PROGRAM += test-locale
test_locale_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_locale_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_locale_SOURCES = src/ws/test-locale.c

TEST_PROGRAM += test-pipe
test_pipe_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_pipe_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_pipe_SOURCES = src/ws/test-pipe.c

TEST_PROGRAM += test-template
test_template_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_template_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_template_SOURCES = src/ws/test-template.c

TEST_PROGRAM += test-transport
test_transport_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_transport_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_transport_SOURCES = src/ws/test-transport.c

TEST_PROGRAM += test-unicode
test_unicode_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_unicode_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_unicode_SOURCES = src/ws/test-unicode.c

TEST_PROGRAM += test-webresponse
test_webresponse_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_webresponse_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_webresponse_SOURCES = src/ws/test-webresponse.c

TEST_PROGRAM += test-webserver
test_webserver_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_webserver_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_webserver_SOURCES = src/ws/test-webserver.c

check_PROGRAMS += mock-echo
mock_echo_CPPFLAGS = $(glib_CFLAGS) $(AM_CPPFLAGS)
mock_echo_LDADD = $(glib_LIBS)
mock_echo_SOURCES = src/ws/mock-echo.c

check_PROGRAMS += mock-auth-command
mock_auth_command_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
mock_auth_command_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
mock_auth_command_SOURCES = src/ws/mock-auth-command.c src/session/session-utils.c

TEST_PROGRAM += test-auth
test_auth_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_auth_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_auth_SOURCES = src/ws/test-auth.c

TEST_PROGRAM += test-creds
test_creds_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_creds_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_creds_SOURCES = src/ws/test-creds.c

TEST_PROGRAM += test-kerberos
test_kerberos_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_kerberos_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS) $(krb5_LIBS)
test_kerberos_SOURCES = src/ws/test-kerberos.c

TEST_PROGRAM += test-branding
test_branding_CPPFLAGS = $(libcockpit_ws_a_CPPFLAGS) $(TEST_CPP)
test_branding_LDADD = $(libcockpit_ws_a_LIBS) $(TEST_LIBS)
test_branding_SOURCES = src/ws/test-branding.c

noinst_PROGRAMS += mock-pam-conv-mod.so
mock_pam_conv_mod_so_SOURCES = src/ws/mock-pam-conv-mod.c
mock_pam_conv_mod_so_CFLAGS = -fPIC $(AM_CFLAGS)
mock_pam_conv_mod_so_LDFLAGS = -shared
mock_pam_conv_mod_so_LDADD = $(PAM_LIBS)

install-tests::
	mkdir -p $(DESTDIR)$(pamdir)
	$(INSTALL_PROGRAM) mock-pam-conv-mod.so $(DESTDIR)$(pamdir)/

install-exec-hook::
	mkdir -p $(DESTDIR)$(sysconfdir)/cockpit/ws-certs.d $(DESTDIR)$(sysconfdir)/cockpit/machines.d
	chmod 755 $(DESTDIR)$(sysconfdir)/cockpit/ws-certs.d $(DESTDIR)$(sysconfdir)/cockpit/machines.d

dist_check_DATA += \
	src/ws/mock-combined.crt \
	src/ws/mock-cat-with-init \
	src/ws/mock-kdc \
	src/ws/mock-krb5.conf.in \
	src/ws/mock-kdc.conf.in \
	src/ws/mock-static \
	src/ws/mock-pipes/exit127 \
	src/ws/mock-pipes/someprogram \
	src/ws/mock-pipes/cockpit-session \
	$(NULL)

appdatadir = $(datadir)/metainfo
nodist_appdata_DATA = src/ws/org.cockpit_project.cockpit.appdata.xml
appdata_in = src/ws/org.cockpit_project.cockpit.appdata.xml.in
$(nodist_appdata_DATA): $(appdata_in) $(PO_FILES)
	$(AM_V_GEN) msgfmt --xml -d $(top_srcdir)/po --template $< --output $@
CLEANFILES += $(nodist_appdata_DATA)
EXTRA_DIST += $(appdata_in)

icons128dir = ${datarootdir}/icons/hicolor/128x128/apps
dist_icons128_DATA = src/ws/cockpit.png

libexec_SCRIPTS += src/ws/cockpit-desktop
