ANALFLAGS= -pg -W -Wall -pedantic -Wcast-align \
           -Wcast-qual -Wchar-subscripts -Winline \
           -Wmissing-prototypes -Wpointer-arith \
           -Wredundant-decls -Wshadow -Wstrict-prototypes -Wwrite-strings
CFLAGS = -O -Iinclude
LDFLAGS = -L. -lcanlock
LIBOBJS = src/sha1.o src/hmac_sha1.o src/base64.o src/canlock.o
LOCKLIB = libcanlock.a

.c.o:
	$(CC) $(CFLAGS) -c $< -o $@

all: sha1test hmactest canlocktest

anal:
	$(MAKE) CFLAGS="$(CFLAGS) $(ANALFLAGS)"

sha1test: t/sha1test.c $(LOCKLIB)
	$(CC) $(CFLAGS) t/$@.c -o $@ $(LDFLAGS)

hmactest: t/hmactest.c $(LOCKLIB)
	$(CC) $(CFLAGS) t/$@.c -o $@ $(LDFLAGS)

canlocktest: t/canlocktest.c $(LOCKLIB)
	$(CC) $(CFLAGS) t/$@.c -o $@ $(LDFLAGS)

src/base64.o: src/base64.c include/base64.h
src/canlock.o: src/canlock.c include/canlock.h
src/hmac_sha1.o: src/hmac_sha1.c include/hmac_sha1.h
src/sha1.o: src/sha1.c include/sha1.h

libs: $(LOCKLIB)
$(LOCKLIB): $(LIBOBJS)
	@echo "Arr, ye be makin' the library matey!"
	ar r $@ $(LIBOBJS)
	ranlib $@
	ln -s include/canlock.h .

clean:
	rm -f src/*.o t/*.o t/*.out *.gmon gmon.*

nuke: clean
	rm -f *.a canlocktest hmactest sha1test *.exe *.h
	
test: all
	@echo "sha1test: "
	@./sha1test > t/sha1test.out
	@diff t/sha1test.shouldbe t/sha1test.out && echo " Pass." || echo " **FAIL**"
	@echo "=-=-=-="
	@echo "hmactest: "
	@./hmactest > t/hmactest.out || echo hmm
	@diff t/hmactest.shouldbe t/hmactest.out && echo " Pass." || echo " **FAIL**"
	@echo "=-=-=-="
	@echo "canlocktest: "
	@./canlocktest > t/canlocktest.out
	@diff t/canlocktest.shouldbe t/canlocktest.out && echo " Pass." || echo " **FAIL**"
	@echo "=-=-=-="

help:
	@echo make options:
	@echo '   anal   compile with lots of warnings enabled.'
	@echo '   libs   build only the library.'
	@echo '   clean  delete stray object files.'
	@echo '   nuke   clean up everything.'

