CFLAGS=-g -DPROTOTYPES=1 -Wall
CPPFLAGS=-I.
ifeq (cygwin,$(patsubst CYGWIN%,cygwin,$(shell uname -s)))
  EXEEXT=.exe
else
  EXEEXT=
endif
OBJS1 = ar.o io.o encode.o decode.o maketree.o maketbl.o huf.o \
	strlib.o pathlib.o filelib.o header.o add.o extract.o list.o \
	error.o
OBJS2 = getopt_long.o

OBJS = $(OBJS1) $(OBJS2)

DEPS = $(OBJS:.o=.d)
TARGET = olha$(EXEEXT)

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) $^ -o $@

proto:
	sh ./cproto.sh $(OBJS1:.o=.c)

%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<

clean:
	$(RM) $(OBJS) $(TARGET) $(TARGET).stackdump

distclean: clean
	$(RM) $(DEPS) tests/test.log

install: $(TARGET)
	install -m 755 olha$(EXEEXT) /usr/local/bin

check: $(TARGET) randtest
	sh ./tests/lha-test.sh 2 10 3 4 5 7 8 16 11
	# 12: testing the end of line conversion.
	# 13: testing the kanji code conversion.
	# 14: testing to handle symbolic links
	# 15: testing to self extracting archive
	# 17: testing to remove relative path
	# 18: restore directory permission
	sh ./test.sh

t: $(TARGET)
	cd t && python lha-test.py

randtest: $(TARGET)
	cd ./randtest && PATH=..:$$PATH ruby randtest.rb

# include $(wildcard *.d)
include $(DEPS)

%.d: %.c
	@echo creating $@
	@set -e; $(CC) -M $(CPPFLAGS) $< \
		| sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \
		[ -s $@ ] || rm -f $@

.PHONY: clean distclean install check t randtest proto
