# vim: filetype=automake

REMOTE_DRIVER_GENERATED = \
	remote/remote_protocol.c \
	remote/remote_protocol.h \
	remote/remote_client_bodies.h \
	remote/lxc_protocol.c \
	remote/lxc_protocol.h \
	remote/lxc_client_bodies.h \
	remote/qemu_protocol.c \
	remote/qemu_protocol.h \
	remote/qemu_client_bodies.h \
	$(NULL)

REMOTE_DRIVER_SOURCES = \
	remote/remote_driver.c \
	remote/remote_driver.h \
	$(REMOTE_DRIVER_GENERATED) \
	$(NULL)

REMOTE_DAEMON_GENERATED = \
	remote/remote_daemon_dispatch_stubs.h \
	remote/remote_daemon_dispatch_lxc_stubs.h \
	remote/remote_daemon_dispatch_qemu_stubs.h \
	$(NULL)

REMOTE_DAEMON_SOURCES = \
	remote/remote_daemon.c \
	remote/remote_daemon.h \
	remote/remote_daemon_config.c \
	remote/remote_daemon_config.h \
	remote/remote_daemon_dispatch.c \
	remote/remote_daemon_dispatch.h \
	remote/remote_daemon_stream.c \
	remote/remote_daemon_stream.h \
	$(REMOTE_DAEMON_GENERATED) \
	$(NULL)

REMOTE_DAEMON_CFLAGS = \
	$(LIBXML_CFLAGS) \
	$(GLIB_CFLAGS) \
	$(GNUTLS_CFLAGS) \
	$(SASL_CFLAGS) \
	$(XDR_CFLAGS) \
	$(DBUS_CFLAGS) \
	$(LIBNL_CFLAGS) \
	$(WARN_CFLAGS) \
	$(PIE_CFLAGS) \
	$(COVERAGE_CFLAGS) \
	-I$(srcdir)/access \
	-I$(srcdir)/conf \
	-I$(srcdir)/rpc \
	$(NULL)

REMOTE_DAEMON_LD_FLAGS = \
	$(RELRO_LDFLAGS) \
	$(PIE_LDFLAGS) \
	$(NO_INDIRECT_LDFLAGS) \
	$(NO_UNDEFINED_LDFLAGS) \
	$(COVERAGE_LDFLAGS) \
	$(NULL)

REMOTE_DAEMON_LD_ADD = \
	libvirt_driver_admin.la \
	libvirt-lxc.la \
	libvirt-qemu.la \
	libvirt.la \
	$(LIBXML_LIBS) \
	$(GNUTLS_LIBS) \
	$(SASL_LIBS) \
	$(DBUS_LIBS) \
	$(LIBNL_LIBS) \
	$(NULL)

if WITH_DTRACE_PROBES
REMOTE_DAEMON_LD_ADD += ../src/libvirt_probes.lo
endif WITH_DTRACE_PROBES

REMOTE_DAEMON_LD_ADD += \
	$(GLIB_LIBS) \
	../gnulib/lib/libgnu.la \
	$(NULL)

LOGROTATE_FILES_IN += \
	remote/libvirtd.qemu.logrotate.in \
	remote/libvirtd.lxc.logrotate.in \
	remote/libvirtd.libxl.logrotate.in \
	remote/libvirtd.logrotate.in \
	$(NULL)

SYSCONF_FILES += remote/libvirtd.sysconf

PODFILES += remote/libvirtd.pod
MANINFILES += libvirtd.8.in

LIBVIRTD_SOCKET_UNIT_FILES_IN = \
	remote/libvirtd.socket.in \
	remote/libvirtd-ro.socket.in \
	remote/libvirtd-admin.socket.in \
	remote/libvirtd-tcp.socket.in \
	remote/libvirtd-tls.socket.in \
	$(NULL)

LIBVIRTD_SOCKET_UNIT_FILES = $(notdir $(LIBVIRTD_SOCKET_UNIT_FILES_IN:%.in=%))

LIBVIRTD_UNIT_FILES_IN = \
	remote/libvirtd.service.in \
	$(LIBVIRTD_SOCKET_UNIT_FILES_IN) \
	$(NULL)

VIRTPROXYD_UNIT_FILES_IN = \
	remote/virtproxyd.service.in \
	$(NULL)

GUEST_UNIT_FILES_IN = \
	remote/virt-guest-shutdown.target.in \
	$(NULL)


SYSTEMD_UNIT_FILES += \
	$(notdir $(LIBVIRTD_UNIT_FILES_IN:%.in=%)) \
	$(notdir $(LIBVIRTD_UNIT_FILES_IN:remote/libvirtd%.in=remote/virtproxyd%)) \
	$(notdir $(GUEST_UNIT_FILES_IN:%.in=%)) \
	$(NULL)
SYSTEMD_UNIT_FILES_IN += \
	$(LIBVIRTD_UNIT_FILES_IN) \
	$(VIRTPROXYD_UNIT_FILES_IN) \
	$(GUEST_UNIT_FILES_IN) \
	$(NULL)

REMOTE_PROTOCOL = $(srcdir)/remote/remote_protocol.x
LXC_PROTOCOL = $(srcdir)/remote/lxc_protocol.x
QEMU_PROTOCOL = $(srcdir)/remote/qemu_protocol.x
REMOTE_DRIVER_PROTOCOL = \
	$(REMOTE_PROTOCOL) \
	$(QEMU_PROTOCOL) \
	$(LXC_PROTOCOL) \
	$(NULL)

DRIVER_SOURCE_FILES += $(REMOTE_DRIVER_SOURCES)

EXTRA_DIST += \
	$(REMOTE_DRIVER_PROTOCOL) \
	$(REMOTE_DRIVER_SOURCES) \
	$(REMOTE_DAEMON_SOURCES) \
	remote/test_libvirtd.aug.in \
	remote/libvirtd.aug.in \
	remote/libvirtd.conf.in \
	remote/libvirtd.policy \
	remote/libvirtd.rules \
	remote/libvirtd.sasl \
	remote/libvirtd.sysctl \
	$(NULL)

# Needed to build libvirt.pot, so must be listed outside
# the WITH_REMOTE/WITH_LIBVIRTD conditionals
BUILT_SOURCES += \
	$(REMOTE_DRIVER_GENERATED) \
	$(REMOTE_DAEMON_GENERATED) \
	$(NULL)
MAINTAINERCLEANFILES += \
	$(REMOTE_DRIVER_GENERATED) \
	$(REMOTE_DAEMON_GENERATED) \
	$(NULL)
CLEANFILES += \
	remote/libvirtd.conf \
	remote/virtproxyd.conf \
	$(NULL)

if WITH_REMOTE
noinst_LTLIBRARIES += libvirt_driver_remote.la
libvirt_la_BUILT_LIBADD += libvirt_driver_remote.la
libvirt_driver_remote_la_CFLAGS = \
	$(XDR_CFLAGS) \
	-I$(srcdir)/conf \
	-I$(srcdir)/rpc \
	$(AM_CFLAGS) \
	$(NULL)
libvirt_driver_remote_la_LDFLAGS = $(AM_LDFLAGS)
libvirt_driver_remote_la_SOURCES = $(REMOTE_DRIVER_SOURCES)

if WITH_DTRACE_PROBES
nodist_libvirt_driver_remote_la_SOURCES = libvirt_probes.h
endif WITH_DTRACE_PROBES

if WITH_SASL
libvirt_driver_remote_la_CFLAGS += \
	$(SASL_CFLAGS) \
	$(NULL)
endif WITH_SASL

endif WITH_REMOTE

if WITH_REMOTE
USED_SYM_FILES += $(srcdir)/libvirt_remote.syms
else ! WITH_REMOTE
SYM_FILES += $(srcdir)/libvirt_remote.syms
endif ! WITH_REMOTE

if WITH_LIBVIRTD

sbin_PROGRAMS += libvirtd virtproxyd

augeas_DATA += \
	remote/libvirtd.aug \
	remote/virtproxyd.aug \
	$(NULL)

augeastest_DATA += \
	remote/test_libvirtd.aug \
	remote/test_virtproxyd.aug \
	$(NULL)

nodist_conf_DATA += \
	remote/libvirtd.conf \
	remote/virtproxyd.conf \
	$(NULL)

CLEANFILES += \
	remote/libvirtd.aug \
	remote/virtproxyd.aug \
	$(NULL)

man8_MANS += libvirtd.8

libvirtd_SOURCES = $(REMOTE_DAEMON_SOURCES)

libvirtd_CFLAGS = \
	$(REMOTE_DAEMON_CFLAGS) \
	-DSOCK_PREFIX="\"libvirt\"" \
	-DDAEMON_NAME="\"libvirtd\"" \
	-DWITH_IP \
	-DLIBVIRTD \
	$(NULL)

libvirtd_LDFLAGS = $(REMOTE_DAEMON_LD_FLAGS)

libvirtd_LDADD = $(REMOTE_DAEMON_LD_ADD)

virtproxyd_SOURCES = $(REMOTE_DAEMON_SOURCES)
virtproxyd_CFLAGS = \
	$(REMOTE_DAEMON_CFLAGS) \
	-DSOCK_PREFIX="\"libvirt\"" \
	-DDAEMON_NAME="\"virtproxyd\"" \
	-DWITH_IP \
	-DVIRTPROXYD \
	$(NULL)
virtproxyd_LDFLAGS = $(REMOTE_DAEMON_LD_FLAGS)
virtproxyd_LDADD = $(REMOTE_DAEMON_LD_ADD)

remote/libvirtd.conf: remote/libvirtd.conf.in
	$(AM_V_GEN)$(SED) \
		-e '/[@]CUT_ENABLE_IP[@]/d' \
		-e '/[@]END[@]/d' \
		-e 's|[@]sysconfdir[@]|@sysconfdir@|' \
		-e 's|[@]runstatedir[@]|@runstatedir@|' \
		-e 's|[@]DAEMON_NAME[@]|libvirtd|' \
		$< > $@

remote/virtproxyd.conf: remote/libvirtd.conf.in
	$(AM_V_GEN)sed \
		-e '/[@]CUT_ENABLE_IP[@]/d' \
		-e '/[@]END[@]/d' \
		-e 's|[@]runstatedir[@]|@runstatedir@|' \
		-e 's/[@]DAEMON_NAME[@]/virtproxyd/' \
		$< > $@

INSTALL_DATA_DIRS += remote

install-data-remote:
	$(MKDIR_P) "$(DESTDIR)$(localstatedir)/log/libvirt"

uninstall-data-remote:
	rmdir "$(DESTDIR)$(localstatedir)/log/libvirt" ||:

remote/libvirtd.aug: remote/libvirtd.aug.in
	$(AM_V_GEN)$(SED) \
		-e '/[@]CUT_ENABLE_IP[@]/d' \
		-e '/[@]END[@]/d' \
		-e 's|[@]sysconfdir[@]|@sysconfdir@|' \
		-e 's|[@]DAEMON_NAME[@]|libvirtd|' \
		-e 's|[@]DAEMON_NAME_UC[@]|Libvirtd|' \
		$< > $@

remote/virtproxyd.aug: remote/libvirtd.aug.in
	$(AM_V_GEN)$(SED) \
		-e '/[@]CUT_ENABLE_IP[@]/d' \
		-e '/[@]END[@]/d' \
		-e 's/[@]DAEMON_NAME[@]/virtproxyd/' \
		-e 's/[@]DAEMON_NAME_UC[@]/Virtproxyd/' \
		$< > $@

remote/test_libvirtd.aug: remote/test_libvirtd.aug.in \
		remote/libvirtd.conf $(AUG_GENTEST_SCRIPT)
	$(AM_V_GEN)$(AUG_GENTEST) remote/libvirtd.conf \
		$(srcdir)/remote/test_libvirtd.aug.in | \
		$(SED) \
		-e '/[@]CUT_ENABLE_IP[@]/d' \
		-e '/[@]END[@]/d' \
		-e 's|[@]sysconfdir[@]|@sysconfdir@|' \
		-e 's|[@]runstatedir[@]|@runstatedir@|' \
		-e 's|[@]DAEMON_NAME[@]|libvirtd|' \
		-e 's|[@]DAEMON_NAME_UC[@]|Libvirtd|' \
		> $@ || rm -f $@

remote/test_virtproxyd.aug: remote/test_libvirtd.aug.in \
		remote/virtproxyd.conf $(AUG_GENTEST_SCRIPT)
	$(AM_V_GEN)$(AUG_GENTEST) remote/virtproxyd.conf \
		$(srcdir)/remote/test_libvirtd.aug.in | \
		$(SED) \
		-e '/[@]CUT_ENABLE_IP[@]/d' \
		-e '/[@]END[@]/d' \
		-e 's|[@]runstatedir[@]|@runstatedir@|' \
		-e 's/[@]DAEMON_NAME[@]/virtproxyd/' \
		-e 's/[@]DAEMON_NAME_UC[@]/Virtproxyd/' \
		> $@ || rm -f $@

if WITH_SYSCTL
# Use $(prefix)/lib rather than $(libdir), since man sysctl.d insists on
# /usr/lib/sysctl.d/ even when libdir is /usr/lib64
sysctldir = $(prefix)/lib/sysctl.d

install-sysctl:
	$(MKDIR_P) $(DESTDIR)$(sysctldir)
	$(INSTALL_DATA) $(srcdir)/remote/libvirtd.sysctl \
	  $(DESTDIR)$(sysctldir)/60-libvirtd.conf

uninstall-sysctl:
	rm -f $(DESTDIR)$(sysctldir)/60-libvirtd.conf
	rmdir $(DESTDIR)$(sysctldir) || :

INSTALL_DATA_LOCAL += install-sysctl
UNINSTALL_LOCAL += uninstall-sysctl
endif WITH_SYSCTL

if WITH_POLKIT
polkitdir = $(datadir)/polkit-1
polkitactionsdir = $(polkitdir)/actions
polkitrulesdir = $(polkitdir)/rules.d

install-polkit:
	$(MKDIR_P) $(DESTDIR)$(polkitactionsdir)
	$(INSTALL_DATA) $(srcdir)/remote/libvirtd.policy \
		$(DESTDIR)$(polkitactionsdir)/org.libvirt.unix.policy
	$(MKDIR_P) $(DESTDIR)$(polkitrulesdir)
	$(INSTALL_DATA) $(srcdir)/remote/libvirtd.rules \
		$(DESTDIR)$(polkitrulesdir)/50-libvirt.rules

uninstall-polkit:
	rm -f $(DESTDIR)$(polkitactionsdir)/org.libvirt.unix.policy
	rmdir $(DESTDIR)$(polkitactionsdir) || :
	rm -f $(DESTDIR)$(polkitrulesdir)/50-libvirt.rules
	rmdir $(DESTDIR)$(polkitrulesdir) || :

INSTALL_DATA_LOCAL += install-polkit
UNINSTALL_LOCAL += uninstall-polkit
endif WITH_POLKIT

endif WITH_LIBVIRTD

.PHONY: \
	install-data-remote \
	uninstall-data-remote \
	$(NULL)

# This is needed for clients too, so can't wrap in
# the WITH_LIBVIRTD conditional
if WITH_SASL
sasldir = $(sysconfdir)/sasl2

install-sasl:
	$(MKDIR_P) $(DESTDIR)$(sasldir)
	$(INSTALL_DATA) $(srcdir)/remote/libvirtd.sasl \
		$(DESTDIR)$(sasldir)/libvirt.conf

uninstall-sasl:
	rm -f $(DESTDIR)$(sasldir)/libvirt.conf
	rmdir $(DESTDIR)$(sasldir) || :

INSTALL_DATA_LOCAL += install-sasl
UNINSTALL_LOCAL += uninstall-sasl
endif WITH_SASL

LIBVIRTD_UNIT_VARS = \
	$(COMMON_UNIT_VARS) \
	-e 's|[@]name[@]|Libvirt|g' \
	-e 's|[@]service[@]|libvirtd|g' \
	-e 's|[@]sockprefix[@]|libvirt|g' \
	-e 's|[@]deps[@]||g' \
	$(NULL)

VIRTD_UNIT_VARS = \
	$(COMMON_UNIT_VARS) \
	-e 's|[@]deps[@]|Conflicts=$(LIBVIRTD_SOCKET_UNIT_FILES)|g' \
	$(NULL)

VIRTPROXYD_UNIT_VARS = \
	$(VIRTD_UNIT_VARS) \
	-e 's|[@]name[@]|Libvirt proxy|g' \
	-e 's|[@]service[@]|virtproxyd|g' \
	-e 's|[@]sockprefix[@]|libvirt|g' \
	$(NULL)

libvirtd.service: remote/libvirtd.service.in $(top_builddir)/config.status
	$(AM_V_GEN)$(SED) $(LIBVIRTD_UNIT_VARS) $< > $@-t && mv $@-t $@

libvirt%.socket: remote/libvirt%.socket.in $(top_builddir)/config.status
	$(AM_V_GEN)$(SED) $(LIBVIRTD_UNIT_VARS) $< > $@-t && mv $@-t $@

virtproxyd.service: remote/virtproxyd.service.in $(top_builddir)/config.status
	$(AM_V_GEN)$(SED) $(VIRTPROXYD_UNIT_VARS) $< > $@-t && mv $@-t $@

virtproxy%.socket: remote/libvirt%.socket.in $(top_builddir)/config.status
	$(AM_V_GEN)$(SED) $(VIRTPROXYD_UNIT_VARS) $< > $@-t && mv $@-t $@

virt-guest-shutdown.target: remote/virt-guest-shutdown.target.in \
			$(top_builddir)/config.status
	$(AM_V_GEN)cp $< $@


remote/remote_client_bodies.h: $(srcdir)/rpc/gendispatch.pl \
		$(REMOTE_PROTOCOL) Makefile.am
	$(AM_V_GEN)$(PERL) -w $(srcdir)/rpc/gendispatch.pl --mode=client \
	  remote REMOTE $(REMOTE_PROTOCOL) \
	  > $(srcdir)/remote/remote_client_bodies.h

remote/lxc_client_bodies.h: $(srcdir)/rpc/gendispatch.pl \
		$(LXC_PROTOCOL) Makefile.am
	$(AM_V_GEN)$(PERL) -w $(srcdir)/rpc/gendispatch.pl --mode=client \
	  lxc LXC $(LXC_PROTOCOL) \
	  > $(srcdir)/remote/lxc_client_bodies.h

remote/qemu_client_bodies.h: $(srcdir)/rpc/gendispatch.pl \
		$(QEMU_PROTOCOL) Makefile.am
	$(AM_V_GEN)$(PERL) -w $(srcdir)/rpc/gendispatch.pl --mode=client \
	  qemu QEMU $(QEMU_PROTOCOL) \
	  > $(srcdir)/remote/qemu_client_bodies.h

remote/remote_daemon_dispatch_stubs.h: $(srcdir)/rpc/gendispatch.pl \
		$(REMOTE_PROTOCOL) Makefile.am
	$(AM_V_GEN)$(PERL) -w $(top_srcdir)/src/rpc/gendispatch.pl \
	  --mode=server remote REMOTE $(REMOTE_PROTOCOL) \
	  > $(srcdir)/remote/remote_daemon_dispatch_stubs.h

remote/remote_daemon_dispatch_lxc_stubs.h: $(srcdir)/rpc/gendispatch.pl \
		$(LXC_PROTOCOL) Makefile.am
	$(AM_V_GEN)$(PERL) -w $(top_srcdir)/src/rpc/gendispatch.pl \
	  --mode=server lxc LXC $(LXC_PROTOCOL) \
	  > $(srcdir)/remote/remote_daemon_dispatch_lxc_stubs.h

remote/remote_daemon_dispatch_qemu_stubs.h: $(srcdir)/rpc/gendispatch.pl \
		$(QEMU_PROTOCOL) Makefile.am
	$(AM_V_GEN)$(PERL) -w $(top_srcdir)/src/rpc/gendispatch.pl \
	  --mode=server qemu QEMU $(QEMU_PROTOCOL) \
	  > $(srcdir)/remote/remote_daemon_dispatch_qemu_stubs.h

libvirtd.8.in: remote/libvirtd.pod
	$(AM_V_GEN)$(POD2MAN) --section=8 $< $@-t1 && \
	if grep 'POD ERROR' $@-t1; then rm $@-t1; exit 1; fi && \
	sed \
		-e 's|SYSCONFDIR|\@sysconfdir\@|g' \
		-e 's|LOCALSTATEDIR|\@localstatedir\@|g' \
		< $@-t1 > $@-t2 && \
	rm -f $@-t1 && \
	mv $@-t2 $@
