# Makefile - Don Yang (uguu.org)
#
# 2013-09-08

ifeq ($(OSTYPE),cygwin)
eext = .exe
cc = x86_64-w64-mingw32-gcc
cflags = -Wall -Werror -Wno-comment -O3 -pedantic -std=c99 -include binmode_main.h -static
else
eext =
cc = gcc
cflags = -Wall -Werror -Wno-comment -O3 -pedantic -std=c99
endif

source = misaka.c
bf_source = misaka_bf2.c
vertical_source = vertical_cat.c
cat1_source = long_cat.c
cat2_source = looong_cat.c
cat3_source = fat_cat.c
cat4_source = big_cat.c

target1 = horizontal_cat$(eext)
target2 = vertical_cat$(eext)
target3 = long_cat$(eext)
target4 = looong_cat$(eext)
target5 = fat_cat$(eext)
target6 = big_cat$(eext)

bin_targets = $(target1) $(target2) $(target3) $(target4) $(target5) $(target6)
test_targets = test_passed_horizontal.txt test_passed_vertical.txt test_passed_cat.txt test_passed_multiply.txt test_passed_size.txt test_passed_error_horizontal.txt test_passed_error_vertical.txt test_passed_bf.txt test_passed_bf_base.txt

all: $(bin_targets) $(test_targets)

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

$(target2): $(vertical_source)
	$(cc) $(cflags) $< -o $@

$(target3): $(cat1_source)
	$(cc) $(cflags) $< -o $@

$(target4): $(cat2_source)
	$(cc) $(cflags) $< -o $@

$(target5): $(cat3_source)
	$(cc) $(cflags) $< -o $@

$(target6): $(cat4_source)
	$(cc) $(cflags) $< -o $@

$(vertical_source): $(target1) $(source)
	./$(target1) $(source) $(source) > $@

$(cat1_source): $(target2) $(source)
	./$(target2) $(source) $(source) > $@

$(cat2_source): $(target2) $(source)
	./$(target2) $(source) $(source) $(source) > $@

$(cat3_source): $(target1) $(target2) $(source)
	./$(target1) $(source) $(source) | ./$(target2) - - > $@

$(cat4_source): $(target1) $(target2) $(source)
	./$(target2) $(source) $(source) $(source) | ./$(target1) - - - > $@


test: $(test_targets)

test_passed_horizontal.txt: $(target1) test_horizontal.pl
	perl test_horizontal.pl ./$(target1) && touch $@

test_passed_vertical.txt: $(target2) test_vertical.pl
	perl test_vertical.pl ./$(target2) && touch $@

test_passed_cat.txt: $(target3) $(target4) test_cat.pl
	perl test_cat.pl ./$(target3) ./$(target4) && touch $@

test_passed_multiply.txt: $(target3) $(target5) $(target6) test_multiply.pl
	perl test_multiply.pl ./$(target3) ./$(target5) ./$(target6) && touch $@

test_passed_size.txt: iocccsize$(eext) $(source) test_size.pl
	perl test_size.pl ./iocccsize$(eext) $(source) && touch $@

test_passed_error_horizontal.txt: $(target1) test_errors.sh
	./test_errors.sh ./$(target1) && touch $@

test_passed_error_vertical.txt: $(target2) test_errors.sh
	./test_errors.sh ./$(target2) && touch $@

test_passed_bf.txt: $(target1) $(target2) $(source) test_bf.sh
	./test_bf.sh ./$(target1) ./$(target2) $(source) && touch $@

test_passed_bf_base.txt: misaka_bf$(eext)
	./misaka_bf$(eext) > /dev/null && touch $@


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

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

misaka_bf$(eext): $(bf_source)
	$(cc) $(cflags) $< -o $@

misaka_bf.txt: $(bf_source)
	grep -n BF: $< | sed -ne 's/\(\S\+:\).*BF:\(\S\+\).*/\1\2/;T;p' > $@


clean:
	-rm -f $(bin_targets) encode$(eext) iocccsize$(eext)
	-rm -f misaka_bf$(eext) misaka_bf.txt
	-rm -f $(vertical_source) $(cat1_source) $(cat2_source) $(cat3_source) $(cat4_source)
	-rm -f test_passed*.txt test_input*.txt

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