# tthsum makefile
# 
# Makefile for GNU Make.

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

#ifeq ($(ARCH),)
#  ARCH = $(shell uname -m)
#endif
ifeq ($(BIN),)
  BIN = $(DESTDIR)/usr/local/bin
endif
ifeq ($(MAN),)
  MAN = $(DESTDIR)/usr/local/man
endif

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

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

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

BINS = obj-unix
#obj-$(ARCH)
SHARES = share
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


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: clean
	sh -c 'X=`basename \`pwd\`` ; \
		tar zvcf ../$$X.tar.gz -C.. $$X'
	sh -c 'X=`basename \`pwd\`` ; \
		tar jvcf ../$$X.tar.bz2 -C.. $$X'
	#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)

$(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)
