#!/usr/bin/make -f


#
# You can configure this at heart
#

# Where the executable goes
SBINDIR = /usr/local/sbin
# Where the manual goes
MANDIR = /usr/local/man

# Your favourite C compiler flags
CFLAGS = -D_GNU_SOURCE -O3 -Wall -Wshadow -Wlarger-than-4096 -Wcast-align -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Wnested-externs -Winline
# Parser generator, plain yacc probably won't do
BISON = bison
# Scanner generator, plain lex may do
LEX = flex

# How to install a binary
$(SBINDIR)/%: %
	test -d $(SBINDIR)
	install -m 0775 -s $^ $@
# How to install a manual
$(MANDIR)/man8/%.8: %.man
	test -d $(MANDIR)/man8
	install -m 0664 $^ $@
# How to generate a parser
%.tab.c %.tab.h: %.y
	$(BISON) $(BFLAGS) -d $^

#
# You ought not to configure below here
#

# These are the available programs and manuals

SBINS    = pringd
SBINMANS = pringd
BOTH     = decode log config rs rsm semantics signals syntax.tab timeval
HEADERS  = $(patsubst %,%.h,$(BOTH)) types.h
SOURCES  = $(patsubst %,%.c,$(BOTH)) pringd.c lexical.c
OBJECTS  = $(patsubst %.c,%.o,$(SOURCES))

# These are some useful targets

all: $(SBINS) pringd.man

install: install.bin install.man

install.bin: $(patsubst %,$(SBINDIR)/%,$(SBINS))
install.man: $(patsubst %,$(MANDIR)/man8/%.8,$(SBINMANS))

clean:
	rm -f pringd $(OBJECTS) lexical.c syntax.tab.[ch] config.c pringd.man depend

# And here the real target at last
pringd: $(OBJECTS)

# Generating files from templates
config.c: subst config.c.cfg
	sed -f subst config.c.cfg >$@
pringd.man: subst pringd.man.cfg
	sed -f subst pringd.man.cfg >$@

# How to make the dependencies below
depend: $(SOURCES) $(HEADERS)
	$(CC) -MM -MG $(SOURCES) >$@

#
# These are taken from depend
#

decode.o: decode.c types.h log.h syntax.tab.h decode.h
log.o: log.c types.h config.h log.h
config.o: config.c types.h timeval.h decode.h log.h semantics.h rs.h config.h
rs.o: rs.c types.h log.h config.h rs.h
rsm.o: rsm.c rs.h types.h log.h timeval.h config.h signals.h rsm.h
semantics.o: semantics.c rs.h types.h timeval.h log.h semantics.h
signals.o: signals.c log.h types.h signals.h
syntax.tab.o: syntax.tab.c timeval.h types.h log.h semantics.h
timeval.o: timeval.c timeval.h types.h log.h
pringd.o: pringd.c config.h types.h log.h rsm.h rs.h signals.h
lexical.o: lexical.c timeval.h types.h decode.h log.h semantics.h syntax.tab.h
