prog = daruma3.rb
layout = $(prog)
output = output_cipher.rb output_msg.rb
bf_interpreter = bf_debug.rb

# Check output scripts.
all: $(output)

output_cipher.rb: $(prog)
	ruby $< < $< > $@

output_msg.rb: $(prog)
	ruby $< $< > $@

# Generate editor.
edit: edit.html

edit.html: $(layout) generate_editor.rb
	ruby generate_editor.rb $< > $@

# Miscellaneous tests.
test: \
	test_passed.reversible_rotate \
	test_passed.stable_rotation_cw \
	test_passed.stable_rotation_ccw \
	test_passed.expand_tabs_cw \
	test_passed.expand_tabs_ccw \
	test_passed.empty_cw \
	test_passed.empty_ccw \
	test_passed.space_cw \
	test_passed.space_ccw \
	test_passed.crlf_cw \
	test_passed.crlf_ccw \
	test_passed.msg \
	test_passed.cipher \
	test_passed.bf

test_passed.reversible_rotate:
	ruby $(prog) $(layout) | ruby $(prog) | diff $(layout) -
	ruby $(prog) < $(layout) | ruby $(prog) - | diff $(layout) -
	touch $@

test_passed.stable_rotation_cw: $(prog) expected_output.stable_rotation
	ruby $< < expected_output.stable_rotation | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | ruby $< | diff expected_output.stable_rotation - && touch $@

test_passed.stable_rotation_ccw: $(prog) expected_output.stable_rotation
	ruby $< expected_output.stable_rotation | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | ruby $< - | diff expected_output.stable_rotation - && touch $@

expected_output.stable_rotation: generate_sample_input.rb
	ruby $< > $@

test_passed.empty_cw: $(prog)
	ruby $< < /dev/null | diff /dev/null - && touch $@

test_passed.empty_ccw: $(prog)
	ruby $< /dev/null | diff /dev/null - && touch $@

test_passed.space_cw: $(prog)
	/bin/echo -e "   \t \n \r   " | ruby $< | diff /dev/null - && touch $@

test_passed.space_ccw: $(prog)
	/bin/echo -e "   \t \n \r   " | ruby $< - | diff /dev/null - && touch $@

test_passed.expand_tabs_cw: $(prog) generate_tab_test.rb expected_output.expand_tabs_cw
	ruby generate_tab_test.rb | ruby $< | diff expected_output.expand_tabs_cw - && touch $@

expected_output.expand_tabs_cw: $(prog) generate_tab_test.rb expand_tabs.rb
	ruby generate_tab_test.rb | ruby expand_tabs.rb | ruby $< > $@

test_passed.expand_tabs_ccw: $(prog) generate_tab_test.rb expected_output.expand_tabs_ccw
	ruby generate_tab_test.rb | ruby $< - | diff expected_output.expand_tabs_ccw - && touch $@

expected_output.expand_tabs_ccw: $(prog) generate_tab_test.rb expand_tabs.rb
	ruby generate_tab_test.rb | ruby expand_tabs.rb | ruby $< - > $@

test_passed.crlf_cw: $(prog) generate_tab_test.rb lf_to_crlf.rb expected_output.expand_tabs_cw
	ruby generate_tab_test.rb | ruby lf_to_crlf.rb | ruby $< | diff expected_output.expand_tabs_cw - && touch $@

test_passed.crlf_ccw: $(prog) generate_tab_test.rb lf_to_crlf.rb expected_output.expand_tabs_cw
	ruby generate_tab_test.rb | ruby lf_to_crlf.rb | ruby $< - | diff expected_output.expand_tabs_ccw - && touch $@

test_passed.msg: output_msg.rb output_cipher.rb expected_output.msg
	ruby $< | ruby output_cipher.rb | diff expected_output.msg - && touch $@

expected_output.msg:
	echo "transcendental imbroglio ruby" > $@

test_passed.cipher: output_cipher.rb expected_output.cipher
	echo "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | ruby $< | diff expected_output.cipher - && touch $@

expected_output.cipher:
	echo "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" > $@

test_passed.bf: $(prog) $(bf_interpreter) expected_output.bf
	ruby $(bf_interpreter) $< | diff expected_output.bf - && touch $@

expected_output.bf:
	echo TRICK > $@

clean:
	-rm -f $(output) edit.html test_passed.* expected_output.*
