# -*- mode: Makefile; tab-width: 8; coding: utf-8-unix -*-

PENV=env http_proxy=http://127.0.0.1:3128 \
	    https_proxy=http://127.0.0.1:3128 \
	    no_proxy="localhost,127.0.0.1" 
NENV=env no_proxy=localhost,127.0.0.1 \
		https_proxy= socks_proxy= http_proxy= \

default:: test

.SUFFIXES:      .yml .json

.yml.json:
	yaml2json.bash < $< > $@

test:: test_test test_net test_local
test_net::
	$(MAKE) $(MFLAGS) TARGET=ipleak.net PDB="" ENV="$(PENV)" targets cleanup
	$(MAKE) $(MFLAGS) TARGET=python.org PDB="" ENV="$(PENV)" targets cleanup
test_local::
	$(MAKE) $(MFLAGS) TARGET=polipo     PDB="" ENV="" targets cleanup
test_test::
	which firefox || exit 0
	$(NENV) /var/local/bin/python2.bash selenium_test.py test
	$(MAKE) $(MFLAGS) cleanup
test_keepassxc::
	[ ! -f /home/devuan/Passwords.kdbx ] || \
	( cd .. ; \
		ANSIBLE_KEEPASSXC_PASSWORD=foobar ansible -i hosts.yml \
		-c local -m ansible-keepassxc \
		-a 'database=/home/devuan/Passwords.kdbx entry=test_h@creep.im group=/Root/Xmpp/Chat' \
		localhost )

debug:: tests/selenium_test-python.org-firefox.json
	$(PENV) /var/local/bin/python2.bash -m pdb selenium_test.py test
	$(PENV) /var/local/bin/python2.bash -m pdb selenium_test.py $<

cleanup::
	@ps ax | grep -v grep | grep geckodriver && killall geckodriver /usr/bin/geckodriver 2>/dev/null || true
	@ps ax | grep -v grep | grep /firefox    && killall /usr/lib64/firefox/firefox 2>/dev/null || true
	@ps ax | grep -v grep | grep phantomjs   && killall phantomjs 2>/dev/null || true

targets::
#	[ -z "${DISPLAY}" ] && xvfb-run $(MAKE) $(MFLAGS) TARGET=$(TARGET) ENV="" /tmp/firefox-$(TARGET).log \
#		|| $(MAKE) $(MFLAGS) TARGET=$(TARGET) ENV="$(ENV)" /tmp/firefox-$(TARGET).log
	$(MAKE) $(MFLAGS) TARGET=$(TARGET) ENV="$(ENV) MOZ_HEADLESS=1" /tmp/firefox-$(TARGET).log
	$(MAKE) $(MFLAGS) TARGET=$(TARGET) ENV="$(ENV)" /tmp/phantomjs-$(TARGET).log

/tmp/chromium-$(TARGET).log::

/tmp/firefox-$(TARGET).log:: tests/selenium_test-$(TARGET)-firefox.json clean
	rm -f /tmp/test_ipleak.net_firefox_*
	$(ENV) /var/local/bin/python2.bash $(PDB) selenium_test.py tests/selenium_test-$(TARGET)-firefox.json > \
		/tmp/firefox-$(TARGET).out \
		&& jq .results < /tmp/firefox-$(TARGET).out > /tmp/firefox-$(TARGET).json \
		|| echo ERROR: $@
	[ ! -f /tmp/firefox-$(TARGET).log ] || cat /tmp/firefox-$(TARGET).log
	[ ! -f /tmp/geckodriver-$(TARGET).log ] || cat /tmp/geckodriver-$(TARGET).log
	[ ! -f /tmp/test_$(TARGET)_failed.png ] || fbi /tmp/test_$(TARGET)_failed.png

/tmp/waterfox-$(TARGET).log:: tests/selenium_test-$(TARGET)-waterfox.json clean
	rm -f /tmp/test_ipleak.net_waterfox_*
	$(ENV) /var/local/bin/python2.bash $(PDB) selenium_test.py tests/selenium_test-$(TARGET)-waterfox.json > \
		/tmp/waterfox-$(TARGET).out \
		&& jq .results < /tmp/waterfox-$(TARGET).out > /tmp/waterfox-$(TARGET).out \
		|| echo ERROR: $@
	[ ! -f /tmp/waterfox-$(TARGET).log ] || cat /tmp/waterfox-$(TARGET).log
	[ ! -f /tmp/geckodriver-$(TARGET).log ] || cat /tmp/geckodriver-$(TARGET).log
	[ ! -f /tmp/test_$(TARGET)_failed.png ] || fbi /tmp/test_$(TARGET)_failed.png

/tmp/phantomjs-$(TARGET).log:: tests/selenium_test-$(TARGET)-phantomjs.json clean
	rm -f /tmp/test_ipleak.net_phantomjs_*
	$(ENV) /var/local/bin/python2.bash $(PDB) selenium_test.py tests/selenium_test-$(TARGET)-phantomjs.json > \
		/tmp/phantomjs-$(TARGET).out \
		&& jq .results < /tmp/phantomjs-$(TARGET).out > /tmp/phantomjs-$(TARGET).json \
		|| echo ERROR: $@
	[ ! -f /tmp/phantomjs-$(TARGET).log ] || cat /tmp/phantomjs-$(TARGET).log
	[ ! -f /tmp/ghostdriver-$(TARGET).log ] || cat /tmp/ghostdriver-$(TARGET).log
	[ ! -f /tmp/test_$(TARGET)_failed.png ] || fbi /tmp/test_$(TARGET)_failed.png
	@ps ax | grep -v grep | grep phantomjs && killall phantomjs 2>/dev/null || true

tests/selenium_test-$(TARGET)-phantomjs.json:: tests/selenium_test-$(TARGET)-firefox.json
	sed -e 's/geckodriver/ghostdriver/' -e 's/firefox/phantomjs/' < $< > $@


clean::
	@rm -f /tmp/firefox-$(TARGET).log /tmp/geckodriver-$(TARGET).log
	@rm -f /tmp/phantomjs-$(TARGET).log /tmp/ghostdriver-$(TARGET).log
	@rm -f /tmp/test_$(TARGET)_failed.png
	@rm -f geckodriver.log ghostdriver.log