TWISTED_TESTS = \
	addressing.py \
	avatar-requirements.py \
	caps/advertise-contact-caps.py \
	caps/advertise-legacy.py \
	caps/broken-reply.py \
	caps/caps-cache.py \
	caps/caps-persistent-cache.py \
	caps/compat-bundles.py \
	caps/disco-without-node.py \
	caps/double-disco.py \
	caps/from-bare-jid.py \
	caps/hashed-caps.py \
	caps_helper.py \
	caps/initial-caps.py \
	caps/jingle-caps.py \
	caps/offline.py \
	caps/receive-jingle.py \
	caps/trust-thyself.py \
	caps/tube-caps.py \
	client-types.py \
	cm/protocol.py \
	connect/disco-error-from-bare-jid.py \
	connect/disco-facebook.py \
	connect/disconnect-timeout.py \
	connect/disco-no-reply.py \
	connect/network-error.py \
	connect/stream-closed.py \
	connect/test-connection-params.py \
	connect/test-fail.py \
	connect/test-nonblocking-tls.py \
	connect/test-success.py \
	connect/test-twice.py \
	console.py \
	dataforms.py \
	gateways.py \
	last-activity.py \
	mail-notification.py \
	muc/avatars.py \
	muc/banned.py \
	muc/chat-states.py \
	muc/conference.py \
	muc/kicked.py \
	muc/name-conflict.py \
	muc/password.py \
	muc/presence-before-closing.py \
	muc/renamed.py \
	muc/room-config.py \
	muc/roomlist.py \
	muc/room.py \
	muc/scrollback.py \
	muc/send-error.py \
	muc/subject.py \
	muc/test-ensure.py \
	muc/test-muc-alias.py \
	muc/test-muc-invitation.py \
	muc/test-muc-ownership.py \
	muc/test-muc.py \
	olpc/change-notifications.py \
	olpc/current-activity.py \
	olpc/olpc-muc-invitation.py \
	olpc/olpc-muc-prop-change.py \
	olpc/test-olpc-bundle.py \
	olpc/test-olpc-set-props-preload.py \
	pep-support.py \
	plugin-channel-managers.py \
	power-save.py \
	presence/decloak.py \
	presence/error.py \
	presence/initial-contact-presence.py \
	presence/initial-presence.py \
	presence/invisible_xep_0126.py \
	presence/invisible_xep_0186.py \
	presence/plugins.py \
	presence/presence.py \
	presence/set-idempotence.py \
	presence/shared-status.py \
	pubsub.py \
	roster/authorize.py \
	roster/edit-before-roster.py \
	roster/ensure.py \
	roster/groups-12791.py \
	roster/groups.py \
	roster/initial-aliases.py \
	roster/push-from-contact.py \
	roster/push-without-id.py \
	roster/removed-from-rp-subscribe.py \
	roster/request-group-after-roster.py \
	roster/request-group-before-roster.py \
	roster/test-google-roster.py \
	roster/test-roster-item-deletion.py \
	roster/test-roster.py \
	roster/test-roster-subscribe.py \
	roster/test-save-alias-to-roster.py \
	sasl/abort.py \
	sasl/close.py \
	sasl/complex.py \
	sasl/jabber_auth.py \
	sasl/plain.py \
	sasl/telepathy-password.py \
	search/ceci-nest-pas-un-serveur.py \
	search/extended.py \
	search/no-server-property.py \
	search/unextended.py \
	servicetest.py \
	sidecar-own-caps.py \
	sidecars.py \
	test-debug.py \
	test-fallback-socks5-proxy.py \
	test-location.py \
	test-register.py \
	text/destroy.py \
	text/ensure.py \
	text/facebook-own-message.py \
	text/initiate.py \
	text/initiate-requestotron.py \
	text/receipts.py \
	text/respawn.py \
	text/send-error.py \
	text/send-to-correct-resource.py \
	text/test-chat-state.py \
	text/test-text-delayed.py \
	text/test-text-no-body.py \
	text/test-text.py \
	tls/legacy-jabber.py \
	tls/server-tls-channel.py \
	version.py \
	$(NULL)

TWISTED_TUBE_TESTS = \
	tubes/accept-muc-dbus-tube.py \
	tubes/accept-muc-stream-tube.py \
	tubes/accept-private-dbus-tube.py \
	tubes/accept-private-stream-tube.py \
	tubes/check-create-tube-return.py \
	tubes/close-muc-with-closed-tube.py \
	tubes/create-invalid-tube-channels.py \
	tubes/ensure-si-tube.py \
	tubes/offer-muc-dbus-tube.py \
	tubes/offer-muc-stream-tube.py \
	tubes/offer-no-caps.py \
	tubes/offer-private-dbus-tube.py \
	tubes/offer-private-stream-tube.py \
	tubes/request-invalid-dbus-tube.py \
	tubes/test-get-available-tubes.py \
	tubes/test-socks5-muc.py \
	$(NULL)

TWISTED_VCARD_TESTS = \
	vcard/clear-avatar.py \
	vcard/disconnect-during-pep.py \
	vcard/get-contact-info.py \
	vcard/item-not-found.py \
	vcard/overlapping-sets.py \
	vcard/redundant-set.py \
	vcard/refresh-contact-info.py \
	vcard/set-avatar.py \
	vcard/set-contact-info.py \
	vcard/set-set-disconnect.py \
	vcard/supported-fields.py \
	vcard/test-alias-empty-vcard.py \
	vcard/test-alias-message.py \
	vcard/test-alias-pep.py \
	vcard/test-alias.py \
	vcard/test-avatar-async.py \
	vcard/test-avatar-multiple-resources.py \
	vcard/test-avatar.py \
	vcard/test-avatar-retrieved.py \
	vcard/test-avatar-tokens.py \
	vcard/test-save-alias-to-vcard.py \
	vcard/test-set-alias.py \
	vcard/test-vcard-cache.py \
	vcard/test-vcard-race.py \
	vcard/update-get-failed.py \
	vcard/update-rejected.py \
	$(NULL)

TWISTED_JINGLE_TESTS = \
	jingle/accept-extra-stream.py \
	jingle/call-basics.py \
	jingle/call-codecoffer.py \
	jingle/call-content-adding-removal.py \
	jingle/call-dtmf.py \
	jingle/call-google-relay.py \
	jingle/call-hold-audio.py \
	jingle/call-hold-av.py \
	jingle/call-muc-cancel.py \
	jingle/call-muc.py \
	jingle/call-muc-re-re-request.py \
	jingle/call-state.py \
	jingle/decloak-peer.py \
	jingle/dtmf-no-audio.py \
	jingle/dtmf.py \
	jingle/google-relay.py \
	jingle/hold-audio.py \
	jingle/hold-av.py \
	jingle/incoming-basics.py \
	jingle/incoming-call-stream-error.py \
	jingle/incoming-gmail-modern-jingle.py \
	jingle/initial-audio-video.py \
	jingle/misuse.py \
	jingle/outgoing-basics.py \
	jingle/outgoing-ensure.py \
	jingle/outgoing-many-streams.py \
	jingle/payload-types.py \
	jingle/preload-caps-crash.py \
	jingle/session-id-collision.py \
	jingle/stream-errors-on-content-reject.py \
	jingle/stream-errors-on-terminate.py \
	jingle/stream-handler-error.py \
	jingle/stun-server.py \
	jingle/test-content-adding-removal.py \
	jingle/test-content-complex.py \
	jingle/test-description-info.py \
	jingle/test-incoming-call-reject.py \
	jingle/test-incoming-iceudp.py \
	jingle/test-outgoing-call-rejected.py \
	jingle/test-outgoing-iceudp.py \
	jingle/test-wait-for-caps-incomplete.py \
	jingle/test-wait-for-caps.py \
	jingle/transport-info-parsing.py \
	jingle/unknown-session.py \
	$(NULL)

TWISTED_FT_TESTS = \
	file-transfer/test-caps-file-transfer.py \
	file-transfer/test-ibb-too-early.py \
	file-transfer/test-receive-file-and-close-socket-while-receiving.py \
	file-transfer/test-receive-file-and-disconnect.py \
	file-transfer/test-receive-file-and-sender-disconnect-while-pending.py \
	file-transfer/test-receive-file-and-sender-disconnect-while-transfering.py \
	file-transfer/test-receive-file-decline.py \
	file-transfer/test-receive-file.py \
	file-transfer/test-send-file-and-cancel-immediately.py \
	file-transfer/test-send-file-declined.py \
	file-transfer/test-send-file-provide-immediately.py \
	file-transfer/test-send-file-send-before-accept.py \
	file-transfer/test-send-file-to-unknown-contact.py \
	file-transfer/test-send-file-wait-to-provide.py \
	file-transfer/test-uri.py \
	file-transfer/metadata.py \
	file-transfer/ft-client-caps.py \
	jingle-share/test-caps-file-transfer.py \
	jingle-share/test-multift.py \
	jingle-share/test-receive-file-and-close-socket-while-receiving.py \
	jingle-share/test-receive-file-and-disconnect.py \
	jingle-share/test-receive-file-and-sender-disconnect-while-pending.py \
	jingle-share/test-receive-file-and-sender-disconnect-while-transfering.py \
	jingle-share/test-receive-file-decline.py \
	jingle-share/test-send-file-and-cancel-immediately.py \
	jingle-share/test-send-file.py \
	jingle-share/test-send-file-send-before-accept.py \
	jingle-share/test-send-file-wait-to-provide.py \
	$(NULL)

# other files used by the twisted tests, but are not tests and are not built
# source
TWISTED_OTHER_FILES = \
	bytestream.py \
	connect/torture.py \
	constants.py \
	file-transfer/file_transfer_helper.py \
	gabbletest.py \
	httptest.py \
	jingle/call_helper.py \
	jingle/callutils.py \
	jingle/__init__.py \
	jingle/jingletest2.py \
	jingle-share/file_transfer_helper.py \
	jingle-share/jingleshareutils.py \
	mucutil.py \
	ns.py \
	olpc/util.py \
	presence/__init__.py \
	presence/invisible_helper.py \
	rostertest.py \
	sasl/saslutil.py \
	search/search_helper.py \
	test-helper.py \
	tls-cert.pem \
	tls-key.pem \
	tubes/tubetestutil.py \
	$(NULL)

if ENABLE_INSTALLED_TESTS
# Install files in each directory. They could be tests, pristine data files,
# scripts or built source
twistedtestsdir = @gabbletestsdir@/twisted
nobase_nodist_twistedtests_SCRIPTS = \
	run-test.sh \
	tools/exec-with-log.sh \
	tools/run-gabble.sh \
	$(NULL)
nobase_dist_twistedtests_SCRIPTS = \
	tools/with-session-bus.sh \
	$(NULL)
nobase_dist_twistedtests_DATA = \
	$(TWISTED_TESTS) \
	$(TWISTED_OTHER_FILES) \
	$(NULL)
nobase_nodist_twistedtests_DATA = \
	config.py \
	gabble-twisted-tests.list \
	$(installed_conf_files) \
	$(service_files)
	$(NULL)
endif

check-local: check-coding-style check-twisted

# set to 6 when using refdbg, to give Gabble time to exit
CHECK_TWISTED_SLEEP=0

check-twisted: $(BUILT_SOURCES)
if WANT_TWISTED_TESTS
	rm -f tools/core
	rm -f tools/vgcore.*
	rm -f tools/gabble-testing.log
	rm -f tools/strace.log
	if test -n "$$GABBLE_TEST_REFDBG"; then \
	  sleep=6; \
        else \
	  sleep=$(CHECK_TWISTED_SLEEP); \
	fi; \
	failed=0; \
	GABBLE_TEST_UNINSTALLED=1 \
	  GABBLE_TEST_SLEEP="--sleep=$$sleep" \
	  GABBLE_ABS_TOP_SRCDIR=@abs_top_srcdir@ \
	  GABBLE_ABS_TOP_BUILDDIR=@abs_top_builddir@ \
	  sh run-test.sh "$(TWISTED_TESTS)" || failed=1; \
	if test -e tools/core; then\
		echo -e "\033[0;31;1mCore dump exists: tools/core\033[0m";\
		exit 1;\
	fi; \
	if test $$failed = 1; then\
		exit 1;\
	fi;
else
	@echo "Configured without Twisted test support. To enable them,"
	@echo "ensure that these Python modules are available:"
	@echo " • twisted.words.xish.domish"
	@echo " • twisted.words.protocols.jabber"
	@echo " • twisted.internet.reactor"
	@echo "and then re-run configure."
endif

if ENABLE_DEBUG
DEBUGGING_PYBOOL = True
else
DEBUGGING_PYBOOL = False
endif

if ENABLE_PLUGINS
PLUGINS_ENABLED_PYBOOL = True
else
PLUGINS_ENABLED_PYBOOL = False
endif

if ENABLE_CHANNEL_TYPE_CALL
CHANNEL_TYPE_CALL_ENABLED_PYBOOL = True
else
CHANNEL_TYPE_CALL_ENABLED_PYBOOL = False
endif

if ENABLE_GOOGLE_RELAY
GOOGLE_RELAY_ENABLED_PYBOOL = True
else
GOOGLE_RELAY_ENABLED_PYBOOL = False
endif

if ENABLE_FILE_TRANSFER
FILE_TRANSFER_ENABLED_PYBOOL = True
else
FILE_TRANSFER_ENABLED_PYBOOL = False
endif

if ENABLE_VOIP
VOIP_ENABLED_PYBOOL = True
else
VOIP_ENABLED_PYBOOL = False
endif

if ENABLE_JINGLE_FILE_TRANSFER
JINGLE_FILE_TRANSFER_ENABLED_PYBOOL = True
else
JINGLE_FILE_TRANSFER_ENABLED_PYBOOL = False
endif

config.py: Makefile
	$(AM_V_GEN) { \
		echo "PACKAGE_STRING = \"$(PACKAGE_STRING)\""; \
		echo "CLIENT_TYPE = '$(CLIENT_TYPE)'"; \
		echo "DEBUGGING = $(DEBUGGING_PYBOOL)"; \
		echo "PLUGINS_ENABLED = $(PLUGINS_ENABLED_PYBOOL)"; \
		echo "CHANNEL_TYPE_CALL_ENABLED = $(CHANNEL_TYPE_CALL_ENABLED_PYBOOL)"; \
		echo "GOOGLE_RELAY_ENABLED = $(GOOGLE_RELAY_ENABLED_PYBOOL)"; \
		echo "FILE_TRANSFER_ENABLED = $(FILE_TRANSFER_ENABLED_PYBOOL)"; \
		echo "VOIP_ENABLED = $(VOIP_ENABLED_PYBOOL)"; \
		echo "JINGLE_FILE_TRANSFER_ENABLED = $(JINGLE_FILE_TRANSFER_ENABLED_PYBOOL)"; \
	} > $@

BUILT_SOURCES = config.py

TWISTED_TESTS += $(TWISTED_FT_TESTS) $(TWISTED_TUBE_TESTS)
TWISTED_TESTS += $(TWISTED_JINGLE_TESTS) $(TWISTED_VCARD_TESTS)

if ENABLE_INSTALLED_TESTS
gabbledebugdir = @gabbletestsdir@/twisted
gabbledebug_PROGRAMS = \
	telepathy-gabble-debug
else
noinst_PROGRAMS = \
	telepathy-gabble-debug
endif

telepathy_gabble_debug_SOURCES = \
    main-debug.c \
    test-resolver.c \
    test-resolver.h

telepathy_gabble_debug_LDADD = \
    $(top_builddir)/src/libgabble-convenience.la \
    $(ALL_LIBS)

telepathy_gabble_debug_LDFLAGS = -export-dynamic

AM_CFLAGS = $(ERROR_CFLAGS) @DBUS_CFLAGS@ @GLIB_CFLAGS@ @WOCKY_CFLAGS@ \
    @TP_GLIB_CFLAGS@ \
    -I $(top_srcdir)/src -I $(top_builddir)/src \
     -I $(top_srcdir)/lib -I $(top_builddir)/lib \
    -I $(top_srcdir) -I $(top_builddir)
ALL_LIBS = @DBUS_LIBS@ @GLIB_LIBS@ @WOCKY_LIBS@ @TP_GLIB_LIBS@

CLEANFILES = gabble-[1-9]*.log *.pyc */*.pyc config.py

check_misc_sources = $(TESTS)

# the following used to be in tools/
include $(top_srcdir)/tools/check-coding-style.mk

gabble-twisted-tests.list: Makefile
	$(AM_V_GEN)echo $(TWISTED_TESTS) > $@

run-test.sh: run-test.sh.in Makefile
	$(AM_V_GEN)sed -e "s|[@]gabbletestsdir[@]|@gabbletestsdir@|g" \
		-e "s|[@]PYTHON[@]|$(PYTHON)|g" \
		-e "s|[@]TEST_PYTHON[@]|$(TEST_PYTHON)|g" \
		 $< > $@
	@chmod +x $@

tools/run-gabble.sh: tools/run-gabble.sh.in Makefile
	@mkdir -p tools
	$(AM_V_GEN)sed \
		-e "s|[@]gabbletestsdir[@]|@gabbletestsdir@|g" \
		-e "s|[@]pluginexecdir[@]|@pluginexecdir@|g" \
		-e "s|[@]libdir[@]|$(libdir)|g" \
		$< > $@
	@chmod +x $@

tools/exec-with-log.sh: tools/exec-with-log.sh.in
	$(MKDIR_P) tools
	$(AM_V_GEN)sed -e "s|[@]abs_top_builddir[@]|@abs_top_builddir@|g" \
		-e "s|[@]abs_top_srcdir[@]|@abs_top_srcdir@|g" $< > $@
	@chmod +x $@

# The wildcard % matches both config files!
tools/%.conf: tools/%.conf.in Makefile
	$(MKDIR_P) tools/servicedir
	$(MKDIR_P) tools/servicedir-uninstalled
	$(AM_V_GEN)sed -e "s|[@]gabbletestsdir[@]|@gabbletestsdir@|g" \
		-e "s|[@]abs_top_builddir[@]|@abs_top_builddir@|g" $< > $@

# We don't use the full filename for the .in because > 99 character filenames
# in tarballs are non-portable (and automake 1.8 doesn't let us build
# non-archaic tarballs)
tools/servicedir/org.freedesktop.Telepathy.ConnectionManager.%.service: tools/servicedir/%.service.in  Makefile
	$(MKDIR_P) tools/servicedir
	$(AM_V_GEN)sed -e "s|[@]gabbletestsdir[@]|@gabbletestsdir@|g" $< > $@

tools/servicedir-uninstalled/org.freedesktop.Telepathy.ConnectionManager.%.service: tools/servicedir-uninstalled/%.service.in
	$(MKDIR_P) tools/servicedir-uninstalled
	$(AM_V_GEN)sed -e "s|[@]abs_top_builddir[@]|@abs_top_builddir@|g" $< > $@

# D-Bus service file for testing
installed_service_in_files = tools/servicedir/gabble.service.in
service_files = tools/servicedir/org.freedesktop.Telepathy.ConnectionManager.gabble.service
uninstalled_service_in_files = tools/servicedir-uninstalled/gabble.service.in
uninstalled_service_files = tools/servicedir-uninstalled/org.freedesktop.Telepathy.ConnectionManager.gabble.service

# D-Bus config file for testing
installed_conf_in_files = tools/servicedir/tmp-session-bus.conf.in
installed_conf_files = $(installed_conf_in_files:.conf.in=.conf)
uninstalled_conf_in_files = tools/servicedir-uninstalled/tmp-session-bus.conf.in
uninstalled_conf_files = $(uninstalled_conf_in_files:.conf.in=.conf)

BUILT_SOURCES += \
	$(service_files) \
	$(installed_conf_files) \
	$(uninstalled_service_files) \
	$(uninstalled_conf_files) \
	gabble-twisted-tests.list \
	run-test.sh \
	tools/exec-with-log.sh \
	tools/run-gabble.sh \
	$(NULL)

EXTRA_DIST = \
	$(installed_service_in_files) \
	$(uninstalled_service_in_files) \
	$(installed_conf_in_files) \
	$(uninstalled_conf_in_files) \
	tools/exec-with-log.sh.in \
	tools/run-gabble.sh.in \
	run-test.sh.in \
	$(NULL)

CLEANFILES += \
    $(BUILT_SOURCES) \
    tools/gabble-testing.log
