# Makefile - Don Yang (uguu.org)
#
# 2013-04-20

ifeq ($(OSTYPE),cygwin)
eext = .exe
cc = i686-w64-mingw32-gcc
cflags = -O3 -Wall -ansi -pedantic -include binmode_main.h -Werror
else
eext =
cc =gcc
cflags = -O3 -Wall -ansi -pedantic -Werror
endif

target = kanna$(eext)
utils = gifsize$(eext) jpgsize$(eext) pngsize$(eext) pnmsize$(eext)

all: $(target)

$(target): kanna.c
	$(cc) $(cflags) $< -o $@

gifsize$(eext): gifsize.c
	$(cc) $(cflags) $< -o $@

jpgsize$(eext): jpgsize.c
	$(cc) $(cflags) $< -o $@

pngsize$(eext): pngsize.c
	$(cc) $(cflags) $< -o $@

pnmsize$(eext): pnmsize.c
	$(cc) $(cflags) $< -o $@

bf.output.txt: bf.prog.txt
	perl bfi.pl $< > $@

bf.prog.txt: bf.c
	sed -ne 's/.*BF:\(.*\) \*\//\1/;T;p' $< > $@

bf$(eext): bf.c
	$(cc) $(cflags) $< -o $@

test: \
	test_passed_gif.txt \
	test_passed_jpg0.txt \
	test_passed_jpg1.txt \
	test_passed_jpg2.txt \
	test_passed_png.txt \
	test_passed_pnm0.txt \
	test_passed_pnm1.txt \
	test_passed_pnm2.txt \
	test_passed_pnm3.txt \
	test_passed_pam0.txt \
	test_passed_pam1.txt \
	test_passed_valid_header_jpg.txt \
	test_passed_valid_header_gif.txt \
	test_passed_valid_header_png.txt \
	test_passed_invalid_header_jpg.txt \
	test_passed_invalid_header_gif.txt \
	test_passed_invalid_header_png.txt \
	test_passed_argv.txt

test_passed_gif.txt: test.pl $(target)
	perl test.pl ppmtogif $(target) && touch $@

test_passed_jpg0.txt: test.pl $(target)
	perl test.pl ppmtojpeg $(target) && touch $@

test_passed_jpg1.txt: test.pl $(target)
	perl test.pl 'ppmtojpeg | jpegtran -optimize' $(target) && touch $@

test_passed_jpg2.txt: test.pl $(target)
	perl test.pl 'ppmtojpeg | jpegtran -optimize -progressive' $(target) && touch $@

test_passed_png.txt: test.pl $(target)
	perl test.pl pnmtopng $(target) && touch $@

test_passed_pnm0.txt: test.pl $(target)
	perl test.pl cat $(target) && touch $@

test_passed_pnm1.txt: test.pl $(target)
	perl test.pl 'sed -e "1a#comment"' $(target) && touch $@

test_passed_pnm2.txt: test.pl $(target)
	perl test.pl pgmnorm $(target) && touch $@

test_passed_pnm3.txt: test.pl $(target)
	perl test.pl 'pgmnorm | sed -e "1a#comment"' $(target) && touch $@

test_passed_pam0.txt: test.pl $(target)
	perl test.pl 'perl pbmtopam.pl' $(target) && touch $@

test_passed_pam1.txt: test.pl $(target)
	perl test.pl 'perl pbmtopam.pl | sed -e "1a#comment"' $(target) && touch $@

test_passed_valid_header_jpg.txt: $(target) test_input.jpg_header.bin
	./$(target) test_input.jpg_header.bin | grep -q "1024 768"
	touch $@

test_passed_valid_header_gif.txt: $(target) test_input.gif_header.bin
	./$(target) test_input.jpg_header.bin | grep -q "1024 768"
	touch $@

test_passed_valid_header_png.txt: $(target) test_input.png_header.bin
	./$(target) test_input.png_header.bin | grep -q "1024 768"
	touch $@

test_passed_invalid_header_jpg.txt: $(target) test_input.jpg_header.bin
	./truncated_input_test.sh test_input.jpg_header.bin $(target)
	touch $@

test_passed_invalid_header_gif.txt: $(target) test_input.gif_header.bin
	./truncated_input_test.sh test_input.gif_header.bin $(target)
	touch $@

test_passed_invalid_header_png.txt: $(target) test_input.png_header.bin
	./truncated_input_test.sh test_input.png_header.bin $(target)
	touch $@

test_passed_argv.txt: $(target) test_input.png test_input.ppm test_input.pbm
	./$(target) test_input.png | grep -q "16 8 test_input.png"
	./$(target) test_input.ppm test_input.pbm | grep -q "24 32 test_input.ppm"
	./$(target) test_input.ppm test_input.pbm | grep -q "32 24 test_input.pbm"
	./$(target) /dev/null | grep -q "0 0 /dev/null"
	touch $@

test_input.jpg_header.bin: jpg_header.xxd
	perl reverse_xxd.pl $< > $@

test_input.gif_header.bin: gif_header.xxd
	perl reverse_xxd.pl $< > $@

test_input.png_header.bin: png_header.xxd
	perl reverse_xxd.pl $< > $@

test_input.png:
	ppmmake rgb:f/f/f 16 8 | pnmtopng > $@
	
test_input.ppm:
	ppmmake rgb:f/f/f 24 32 > $@

test_input.pbm:
	pbmmake 32 24 > $@

clean:
	-rm -f $(target) $(utils)
	-rm -f bf$(eext) bf.prog.txt bf.output.txt
	-rm -f test_passed_*.txt test_input.*

backup:
	-rm -f backup.tar.gz
	tar cf backup.tar *.c *.pl *.sh *.png *.log *.txt *.xxd Makefile
	gzip -9 backup.tar
