source = nuko.c

ifeq ($(OSTYPE),cygwin)
eext = .exe
cc = x86_64-w64-mingw32-gcc
copts = -O3 -Wall -Werror
strip = x86_64-w64-mingw32-strip
else
eext =
cc = gcc
copts = -O3 -Wall -Werror
strip = strip
endif

test_outputs = \
	test_output_shift1.txt \
	test_output_shift2.txt \
	test_output_shift3.txt \
	test_output_msg1.txt \
	test_output_msg2.txt \
	test_output_msg3.txt \
	test_output_msg4.txt \
	test_output_msg5.txt \
	test_output_msg6.txt \
	test_output_msg7.txt

test: $(test_outputs)

test_output_shift1.txt: shift1$(eext)
	./$< > $@

test_output_shift2.txt: shift2$(eext)
	./$< > $@

test_output_shift3.txt: shift3$(eext)
	./$< > $@

test_output_msg1.txt: msg1$(eext)
	./$< > $@

test_output_msg2.txt: msg2$(eext)
	./$< > $@

test_output_msg3.txt: msg3$(eext)
	./$< > $@

test_output_msg4.txt: msg4$(eext)
	./$< > $@

test_output_msg5.txt: msg5$(eext)
	./$< > $@

test_output_msg6.txt: msg6$(eext)
	./$< > $@

test_output_msg7.txt: decode_msg7$(eext) $(source)
	./$< < $(source) > $@

rotate_ccw$(eext): $(source)
	$(cc) $(copts) $< -o $@

rotate_cw$(eext): generated_rotate_cw.c
	$(cc) $(copts) $< -o $@

ltrim$(eext): generated_ltrim.c
	$(cc) $(copts) $< -o $@

shift1$(eext): generated_shift_original.c
	$(cc) $(copts) $< -o $@

shift2$(eext): generated_shift_rotate_ccw.c
	$(cc) $(copts) $< -o $@

shift3$(eext): generated_shift_rotate_cw.c
	$(cc) $(copts) $< -o $@

msg1$(eext): generated_msg1.c
	$(cc) $(copts) $< -o $@

msg2$(eext): generated_msg2.c
	$(cc) $(copts) $< -o $@

msg3$(eext): generated_msg3.c
	$(cc) $(copts) $< -o $@

msg4$(eext): generated_msg4.c
	$(cc) $(copts) $< -o $@

msg5$(eext): generated_msg5.c
	$(cc) $(copts) $< -o $@

msg6$(eext): generated_msg6.c
	$(cc) $(copts) $< -o $@

decode_msg7$(eext): generated_final_decode.c
	$(cc) $(copts) $< -o $@

generated_rotate_cw.c: $(source) rotate_ccw$(eext)
	./rotate_ccw$(eext) < $< > $@

generated_ltrim.c: $(source) rotate_cw$(eext)
	./rotate_cw$(eext) < $< > $@

generated_shift_original.c: $(source) rotate_cw$(eext) rotate_ccw$(eext) ltrim$(eext)
	./rotate_cw$(eext) < $< | ./ltrim$(eext) | ./rotate_ccw$(eext) > $@

generated_shift_rotate_ccw.c: $(source) ltrim$(eext) rotate_ccw$(eext)
	./ltrim$(eext) < $< | ./rotate_ccw$(eext) > $@

generated_shift_rotate_cw.c: $(source) ltrim$(eext) rotate_cw$(eext)
	./ltrim$(eext) < $< | ./rotate_cw$(eext) > $@

generated_msg1.c: $(source) rotate_cw$(eext)
	./rotate_cw$(eext) < $< | ./rotate_cw$(eext) > $@

generated_msg2.c: generated_msg1.c ltrim$(eext) rotate_cw$(eext) rotate_ccw$(eext)
	./rotate_ccw$(eext) < $< | ./ltrim$(eext) | ./rotate_cw$(eext) > $@

generated_msg3.c: generated_msg2.c ltrim$(eext) rotate_ccw$(eext)
	./rotate_ccw$(eext) < $< | ./rotate_ccw$(eext) | ./ltrim$(eext) | ./rotate_ccw$(eext) | ./ltrim$(eext) | ./rotate_ccw$(eext) > $@

generated_msg4.c: generated_msg3.c ltrim$(eext) rotate_cw$(eext) rotate_ccw$(eext)
	./ltrim$(eext) < $< | ./rotate_ccw$(eext) | ./ltrim$(eext) | ./rotate_cw$(eext) > $@

generated_msg5.c: generated_msg4.c ltrim$(eext) rotate_ccw$(eext)
	./rotate_ccw$(eext) < $< | ./rotate_ccw$(eext) | ./ltrim$(eext) | ./rotate_ccw$(eext) | ./ltrim$(eext) | ./rotate_ccw$(eext) > $@

generated_msg6.c: generated_msg5.c ltrim$(eext) rotate_cw$(eext) rotate_ccw$(eext)
	./ltrim$(eext) < $< | ./rotate_ccw$(eext) | ./ltrim$(eext) | ./rotate_cw$(eext) > $@

generated_final_decode.c: msg3$(eext)
	./$< > $@

clean:
	rm -f generated_*.c $(test_outputs)
	rm -f rotate_cw$(eext) rotate_ccw$(eext) ltrim$(eext) shift*$(eext) msg*$(eext) decode_msg7$(eext)

backup:
	-rm -f backup.tar.gz
	tar cf backup.tar *.c *.pl *.sh *.txt *.log Makefile ioccc_entry/* gif/*
	gzip -9 backup.tar
