# tthsum makefile
# 
# Makefile for GNU Make.

.PHONY: all clean dists install manual tthsum uninstall test runtest

ifeq ($(BIN),)
  BIN = $(DESTDIR)/usr/local/bin
endif
ifeq ($(MAN),)
  MAN = $(DESTDIR)/usr/local/man
endif

ifeq ($(DEBUG),)
  override CPPFLAGS += -DNDEBUG
  CFLAGS += -O3 #-fno-stack-protector #-march=native
  LDFLAGS += -O3 #-march=native
else
  override CPPFLAGS += -DDEBUG
  CFLAGS += -g #-pg
  LDFLAGS += -g #-pg
endif

CC ?= gcc
GZIP = gzip -9 -c
RM = rm -rf

override CPPFLAGS += -D_FILE_OFFSET_BITS=64 -DUSE_TEXTS #-DUSE_MY_GETOPT
CFLAGS += -Wall #-pedantic #-ansi
LDFLAGS += -Wall

BINS = obj-unix
SHARES = share
HEADERS = base32.h endian.h escape.h getopt.h read.h test.h texts.h \
	thex.h tiger.h tthsum.h types.h utf8.h
APPSRC = base32.c escape.c getopt.c read.c texts.c thex.c tiger.c \
	tthsum.c utf8.c
APPOBJ = $(patsubst %.c,$(BINS)/%.o, $(APPSRC))
APPENTRY = $(BINS)/main.o
TSTSRC = $(APPSRC) base32_test.c endian_test.c escape_test.c getopt_test.c \
	read_test.c texts_test.c thex_test.c tiger_test.c types_test.c \
	utf8_test.c
TSTOBJ = $(patsubst %.c,$(BINS)/%.o, $(TSTSRC))
TSTENTRY = $(BINS)/test.o

# "autoconf"
override CPPFLAGS += $(shell cat $(BINS)/autoconf.cppflags)


all: tthsum manual test runtest

install: tthsum manual test runtest
	install -d $(BIN) $(MAN)/man1
	install $(BINS)/tthsum $(BIN)
	install -m644 $(SHARES)/tthsum.1.gz $(MAN)/man1

uninstall:
	$(RM) $(BIN)/tthsum
	$(RM) $(MAN)/man1/tthsum.1.gz

tthsum: $(BINS)/tthsum

manual: $(SHARES)/tthsum.1.gz

test: $(BINS)/test

runtest: test tthsum
	$(BINS)/test
	$(BINS)/tthsum $(BINS)/tthsum $(BINS)/test | $(BINS)/tthsum -cv

dists:
	sh -c 'X=`basename \`pwd\`` ; for opts in "zcf ../$$X.tar.gz" \
	  "jcf ../$$X.tar.bz2" ; do tar $$opts --no-recursion -C.. \
	  `svn ls ../$$X | sed -e "s#^#$$X/#"` ; done'
	#sh -c 'X=`basename \`pwd\`` ; cp ../$$X.tar.gz /usr/src/rpm/SOURCES/'
	#rpm -bs rpm/tthsum.spec
	#rpm -bb rpm/tthsum.spec

clean:
	$(RM) $(BINS) $(SHARES)

# TSTSRC includes APPSRC
$(TSTSRC) autoconf.c: $(HEADERS)
	touch $(TSTSRC) autoconf.c

$(BINS)/%.o: %.c
	@mkdir -p $(BINS)
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

$(BINS)/tthsum: $(APPOBJ) $(APPENTRY)

$(SHARES)/tthsum.1.gz: tthsum.1
	@mkdir -p $(SHARES)
	$(GZIP) $< > $@

$(BINS)/test: $(TSTOBJ) $(TSTENTRY)

# "autoconf"
$(BINS)/autoconf.cppflags: autoconf.c
	@mkdir -p $(BINS)
	$(CC) $(CPPFLAGS) $(CFLAGS) -c autoconf.c -o $(BINS)/autoconf.o
	$(CC) $(LDFLAGS) $(BINS)/autoconf.o -o $(BINS)/autoconf
	$(BINS)/autoconf >$(BINS)/autoconf.cppflags
$(APPOBJ): $(BINS)/autoconf.cppflags
$(TSTOBJ): $(BINS)/autoconf.cppflags
