# Makefile - Don Yang (uguu.org)
#
# 12/11/11

char = chinatsu
source = $(char).c
ifeq ($(OSTYPE),cygwin)
eext = .exe
target = $(char)$(eext)
cc = i686-w64-mingw32-gcc
cflags = -O3 -Wall -ansi -pedantic
else
eext =
target = $(char)
cc = gcc
cflags = -O3 -Wall -ansi -pedantic
endif

all: $(target)

$(target): $(source)
	$(cc) $(cflags) $< -o $@

test: test_output_odd.ppm test_output_even.ppm test_output_identical_odd.ppm test_output_identical_even.ppm

test_output_odd.ppm: $(target) test_input1.ppm test_input2.ppm test_input_expected_odd.ppm
	./$(target) test_input1.ppm test_input2.ppm $@ odd
	perl cmp_ppm.pl $@ test_input_expected_odd.ppm || (rm $@ && false)

test_output_even.ppm: $(target) test_input1.ppm test_input2.ppm test_input_expected_even.ppm
	./$(target) test_input1.ppm test_input2.ppm $@
	perl cmp_ppm.pl $@ test_input_expected_even.ppm || (rm $@ && false)

test_output_identical_odd.ppm: $(target) test_input1.ppm test_input_expected_identical.ppm
	./$(target) test_input1.ppm test_input1.ppm $@ odd
	perl cmp_ppm.pl $@ test_input_expected_identical.ppm || (rm $@ && false)

test_output_identical_even.ppm: $(target) test_input1.ppm test_input_expected_identical.ppm
	./$(target) test_input1.ppm test_input1.ppm $@
	perl cmp_ppm.pl $@ test_input_expected_identical.ppm || (rm $@ && false)

test_input1.ppm: primary.png
	pngtopnm $< > $@

test_input2.ppm: secondary.png
	pngtopnm $< > $@

test_input_expected_odd.ppm: test_expected_output_odd.png
	pngtopnm $< > $@

test_input_expected_even.ppm: test_expected_output_even.png
	pngtopnm $< > $@

test_input_expected_identical.ppm: test_expected_output_identical.png
	pngtopnm $< > $@

clean:
	-rm -f $(target)
	-rm -f test_input* test_output*

backup:
	-rm -f backup.tar.xz
	tar cf backup.tar *.c *.pl *.log *.png *.txt Makefile
	xz -9 backup.tar
