
COCKPIT_BUILD_INFO = "Built on $(shell date)"

LOGIN_PO_FILES = $(patsubst %,dist/static/login.po.%.html,$(LINGUAS))

staticdir = $(datadir)/cockpit/static
nodist_static_DATA = dist/static/login.min.html dist/static/login.po.html $(LOGIN_PO_FILES)

dist/static/login.js: src/ws/login.js
	$(ESHINT_RULE)
	$(COPY_RULE)
dist/static/login.min.js: dist/static/login.js
	$(MIN_JS_RULE)
dist/static/login.css: src/ws/login.css
	$(COPY_RULE)
dist/static/login.min.css: dist/static/login.css
	$(MIN_CSS_RULE)
dist/static/login.html: src/ws/login.html dist/static/login.js dist/static/login.css
	$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \
	sed -e '/<script>/ r dist/static/login.js' \
	    -e '/<style>/ r dist/static/login.css' $< > $@.tmp && \
	$(MV) $@.tmp $@
dist/static/login.min.html: src/ws/login.html dist/static/login.min.js dist/static/login.min.css
	$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \
	sed -e '/<script>/ r dist/static/login.min.js' \
	    -e '/<style>/ r dist/static/login.min.css' $< > $@.tmp && \
	$(MV) $@.tmp $@

dist/static/login.po.html: src/ws/po.empty.html
	$(COPY_RULE)

dist/static/login.po.%.html: src/ws/%.po src/ws/po.empty.html
	$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \
	$(srcdir)/tools/missing $(srcdir)/po/po2json -m $(srcdir)/src/ws/po.empty.html -o $@.tmp src/ws/$*.po && \
	$(MV) $@.tmp $@

staticfontsdir = $(staticdir)/fonts
staticfonts_DATA = $(OPENSANS_FONTS)

CLEANFILES += \
	dist/static/login.html \
	dist/static/login.min.html \
	dist/static/login.js \
	dist/static/login.min.js \
	dist/static/login.css \
	dist/static/login.min.css \
	$(NULL)

EXTRA_DIST += \
	dist/static/login.html \
	dist/static/login.min.html \
	dist/static/login.js \
	dist/static/login.min.js \
	dist/static/login.css \
	dist/static/login.min.css \
	src/ws/login.html \
	src/ws/login.js \
	src/ws/login.css \
	src/ws/po.empty.html \
	$(LOGIN_PO_FILES) \
	$(staticfonts_DATA) \
	$(NULL)

# ----------------------------------------------------------------------------------------------------

noinst_LIBRARIES += libcockpit-ws.a

libcockpit_ws_a_SOURCES = \
	src/ws/cockpitws.h				\
	src/ws/cockpithandlers.h	src/ws/cockpithandlers.c	\
	src/ws/cockpitauth.h		src/ws/cockpitauth.c		\
	src/ws/cockpitcertificate.c \
	src/ws/cockpitcertificate.h \
	src/ws/cockpitcompat.c \
	src/ws/cockpitcompat.h \
	src/ws/cockpitbranding.h \
	src/ws/cockpitbranding.c \
	src/ws/cockpitchannelresponse.h \
	src/ws/cockpitchannelresponse.c \
	src/ws/cockpitchannelsocket.h \
	src/ws/cockpitchannelsocket.c \
	src/ws/cockpitcreds.h src/ws/cockpitcreds.c \
	src/ws/cockpitwebservice.h \
	src/ws/cockpitwebservice.c \
	$(NULL)

libcockpit_ws_a_CFLAGS = \
	-I$(top_srcdir)/src/ws \
	-DCOCKPIT_BUILD_INFO=\"$(COCKPIT_BUILD_INFO)\"	\
	-DG_LOG_DOMAIN=\"cockpit-ws\" \
	$(COCKPIT_WS_CFLAGS) \
	$(NULL)

libcockpit_ws_LIBS = \
	libcockpit-ws.a \
	libcockpit-common.a \
	libwebsocket.a \
	$(COCKPIT_WS_LIBS) \
	$(NULL)

EXTRA_DIST += \
	src/ws/mock_cert \
	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-config \
	src/ws/mock-pipes/exit127 \
	src/ws/mock-pipes/someprogram \
	src/ws/mock-pipes/cockpit-session \
	$(NULL)

# ----------------------------------------------------------------------------------------------------

sbin_PROGRAMS += remotectl

libexec_PROGRAMS += cockpit-ws cockpit-session

cockpit_session_SOURCES = \
	src/common/cockpitauthorize.c \
	src/common/cockpitauthorize.h \
	src/common/cockpitbase64.c \
	src/common/cockpitbase64.h \
	src/common/cockpitframe.c \
	src/common/cockpitframe.h \
	src/common/cockpitmemory.c \
	src/common/cockpitmemory.h \
	src/ws/session.c \
	$(NULL)
cockpit_session_LDADD = $(COCKPIT_SESSION_LIBS)

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

cockpit_ws_CFLAGS = 					\
	-DG_LOG_DOMAIN=\"cockpit-ws\"			\
	-I$(top_srcdir)/src/ws				\
	$(COCKPIT_WS_CFLAGS)				\
	$(NULL)

cockpit_ws_LDADD = 					\
	$(libcockpit_ws_LIBS) \
	$(COCKPIT_WS_LIBS)					\
	$(NULL)

remotectl_SOURCES = \
	src/ws/cockpitcertificate.c \
	src/ws/cockpitcertificate.h \
	src/ws/remotectl-certificate.c \
	src/ws/remotectl.c \
	src/ws/remotectl.h \
	$(NULL)

remotectl_CFLAGS = \
	$(COCKPIT_CFLAGS) \
	$(NULL)

remotectl_LDADD = \
	libcockpit-common.a \
	$(COCKPIT_LIBS) \
	$(NULL)

nodist_systemdunit_DATA += \
	src/ws/cockpit.socket \
	src/ws/cockpit.service \
	src/ws/cockpit-motd.service \
	$(NULL)

firewalldir = $(prefix)/lib/firewalld/services
firewall_DATA = src/ws/cockpit.xml

motddir = $(datadir)/$(PACKAGE)/motd/
dist_motd_DATA = src/ws/inactive.motd
dist_motd_SCRIPTS = src/ws/update-motd

# Automake: 'Variables using ... ‘sysconf’ ... are installed by install-exec.'
install-exec-hook::
	$(MKDIR_P) $(DESTDIR)$(sysconfdir)/motd.d
	$(LN_S) -nf /run/cockpit/motd $(DESTDIR)$(sysconfdir)/motd.d/cockpit
	$(MKDIR_P) $(DESTDIR)$(sysconfdir)/issue.d
	$(LN_S) -nf /run/cockpit/motd $(DESTDIR)$(sysconfdir)/issue.d/cockpit.issue

tempconfdir = $(prefix)/lib/tmpfiles.d
nodist_tempconf_DATA = src/ws/cockpit-tempfiles.conf
tempconf_in = src/ws/cockpit-tempfiles.conf.in

$(nodist_tempconf_DATA): $(tempconf_in)
	$(SUBST_RULE)

# If running cockpit-ws as a non-standard user, we also set up
# cockpit-session to be setuid root, but only runnable by cockpit-session
install-exec-hook::
	chown -f root:$(COCKPIT_GROUP) $(DESTDIR)$(libexecdir)/cockpit-session || true
	test "$(COCKPIT_USER)" != "root" && chmod -f 4750 $(DESTDIR)$(libexecdir)/cockpit-session || true

libexec_SCRIPTS = cockpit-desktop

cockpit-desktop: src/ws/cockpit-desktop.in
	$(SUBST_RULE)

EXTRA_DIST += \
	src/ws/cockpit-motd.service.in \
	src/ws/cockpit.service.in \
	src/ws/cockpit.socket.in \
	src/ws/cockpit-desktop.in \
	$(firewall_DATA) \
	$(tempconf_in) \
	$(NULL)

CLEANFILES += \
	src/ws/cockpit.socket \
	src/ws/cockpit.service \
	src/ws/cockpit-motd.service \
	$(nodist_tempconf_DATA) \
	cockpit-desktop \
	$(NULL)

appdatadir = $(datadir)/metainfo
nodist_appdata_DATA = src/ws/cockpit.appdata.xml
appdata_in = src/ws/cockpit.appdata.xml.in

$(nodist_appdata_DATA): $(appdata_in) $(PO_FILES)
	$(AM_V_GEN) LC_ALL=C $(INTLTOOL_MERGE) -q -x -u $(top_srcdir)/po $< $@

pixmapdir = $(datadir)/pixmaps
pixmap_DATA = src/ws/cockpit.png

CLEANFILES += \
	$(nodist_appdata_DATA) \
	$(NULL)

EXTRA_DIST += \
	$(appdata_in) \
	$(pixmap_DATA) \
	$(NULL)

# ----------------------------------------------------------------------------------------------------

noinst_PROGRAMS += test-server
check_PROGRAMS += test-server

test_server_SOURCES = src/ws/test-server.c $(mock_dbus_sources)
nodist_test_server_SOURCES = $(test_built_sources)
test_server_CFLAGS = 					\
	-I$(top_srcdir)/src/ws \
	-DG_LOG_DOMAIN=\"test-server\"			\
	$(GIO_CFLAGS)					\
	$(COCKPIT_WS_CFLAGS) \
	$(NULL)

test_server_LDADD = 					\
	$(libcockpit_ws_LIBS) \
	$(GIO_LIBS)					\
	-lpam 						\
	$(NULL)

WS_CHECKS = \
	test-base64 \
	test-creds \
	test-auth \
	test-compat \
	test-webservice \
	test-channelresponse \
	test-handlers \
	test-kerberos \
	test-remotectlcertificate \
	$(NULL)

test_auth_CFLAGS = $(cockpit_ws_CFLAGS)

test_auth_SOURCES = \
	src/ws/test-auth.c \
	src/ws/mock-auth.c \
	src/ws/mock-auth.h \
	$(NULL)

test_auth_LDADD = \
	libcockpit-ws.a \
	$(cockpit_ws_LDADD) \
	$(NULL)

test_channelresponse_CFLAGS = $(cockpit_ws_CFLAGS)
test_channelresponse_SOURCES = src/ws/test-channelresponse.c \
	src/ws/mock-auth.c src/ws/mock-auth.h \
	src/common/mock-io-stream.c src/common/mock-io-stream.h \
	$(NULL)
test_channelresponse_LDADD = \
	libwebsocket.a \
	libcockpit-ws.a \
	$(cockpit_ws_LDADD) \
	$(NULL)

test_compat_CFLAGS = $(cockpit_ws_CFLAGS)
test_compat_SOURCES = src/ws/test-compat.c
test_compat_LDADD = libcockpit-ws.a $(cockpit_ws_LDADD)

test_creds_CFLAGS = $(cockpit_ws_CFLAGS)
test_creds_SOURCES = src/ws/test-creds.c
test_creds_LDADD = \
	libcockpit-ws.a \
	$(cockpit_ws_LDADD)

test_handlers_CFLAGS = $(cockpit_ws_CFLAGS)

test_handlers_SOURCES = \
	src/ws/test-handlers.c \
	src/ws/mock-auth.c src/ws/mock-auth.h \
	src/common/mock-io-stream.c src/common/mock-io-stream.h \
	$(NULL)

test_handlers_LDADD = \
	libcockpit-ws.a \
	$(cockpit_ws_LDADD) \
	$(NULL)

test_remotectlcertificate_CFLAGS = \
	$(COCKPIT_CFLAGS) \
	$(NULL)
test_remotectlcertificate_SOURCES = \
	src/ws/test-remotectlcertificate.c \
	src/ws/cockpitcertificate.c \
	src/ws/cockpitcertificate.h \
	src/ws/remotectl-certificate.c \
	src/ws/remotectl.h \
	$(NULL)
test_remotectlcertificate_LDADD = \
	libcockpit-common.a \
	$(COCKPIT_LIBS) \
	$(NULL)

test_webservice_SOURCES = \
	src/ws/test-webservice.c \
	src/ws/mock-auth.c src/ws/mock-auth.h \
	src/common/mock-io-stream.c src/common/mock-io-stream.h \
	$(NULL)

test_webservice_CFLAGS = $(cockpit_ws_CFLAGS)

test_webservice_LDADD = \
	libwebsocket.a \
	libcockpit-ws.a \
	$(cockpit_ws_LDADD) \
	$(NULL)

test_kerberos_SOURCES = src/ws/test-kerberos.c
test_kerberos_LDADD =  $(cockpit_ws_LDADD) $(cockpit_session_LDADD)
test_kerberos_CFLAGS = $(cockpit_ws_CFLAGS) $(cockpit_session_CFLAGS)

mock_echo_SOURCES = src/ws/mock-echo.c
mock_echo_CFLAGS = $(COCKPIT_WS_CFLAGS)
mock_echo_LDADD = $(COCKPIT_WS_LIBS)

mock_auth_command_SOURCES = \
	src/common/cockpitauthorize.c \
	src/common/cockpitauthorize.h \
	src/common/cockpitbase64.c \
	src/common/cockpitbase64.h \
	src/common/cockpitframe.c \
	src/common/cockpitframe.h \
	src/common/cockpitmemory.c \
	src/common/cockpitmemory.h \
	src/ws/mock-auth-command.c \
	$(NULL)

noinst_PROGRAMS += \
	$(WS_CHECKS) \
	mock-echo \
	mock-auth-command \
	$(NULL)

noinst_SCRIPTS += \
	src/ws/mock-cat-with-init \
	$(NULL)

if WITH_COCKPIT_SSH

# HACK: https://bugs.libssh.org/T117: libssh 0.8.4 breaks interactive keyboard auth on server side, i. e. our mock-ssh for unit tests
if !HAVE_BROKEN_SSH_KBDINT
WS_CHECKS += \
	test-authssh \
	$(NULL)
endif

test_authssh_CFLAGS = $(cockpit_ws_CFLAGS)
test_authssh_SOURCES = \
	src/ws/test-authssh.c \
	src/ws/mock-auth.c \
	src/ws/mock-auth.h \
	$(NULL)
test_authssh_LDADD = \
	libcockpit-ws.a \
	$(cockpit_ws_LDADD)

endif

TESTS += $(WS_CHECKS)

mock_pam_conv_mod_so_SOURCES = src/ws/mock-pam-conv-mod.c
mock-pam-conv-mod.so$(EXEEXT): $(mock_pam_conv_mod_so_SOURCES)
	$(AM_V_CCLD) $(CC) -fPIC -shared $(CFLAGS) -I$(builddir) \
		-o $@ $^ $(PAM_LIBS) $(LDFLAGS)

noinst_PROGRAMS += mock-pam-conv-mod.so
CLEANFILES += mock-pam-conv-mod.so
EXTRA_DIST += src/ws/fatal.conf

testassetsdir = $(prefix)/lib/cockpit-test-assets
testserviceddir = $(systemdunitdir)/cockpit.service.d

install-tests::
	$(MKDIR_P) $(DESTDIR)$(testassetsdir) $(DESTDIR)$(testserviceddir) $(DESTDIR)/etc/cockpit
	$(INSTALL_DATA) mock-pam-conv-mod.so $(DESTDIR)$(testassetsdir)
	$(INSTALL_DATA) $(srcdir)/src/ws/fatal.conf $(DESTDIR)/etc/cockpit/cockpit.conf

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

prepare-po-appdata: $(appdata_in)
	cp $< .
	$(INTLTOOL_EXTRACT) -l --type=gettext/xml $(notdir $(appdata_in))

prepare-po:: prepare-po-appdata
