# -*- Mode: sh

LIB=libmpsl.a

all: $(TARGET) docs

PROJ=mpsl

ADD_DOCS=doc/mpsl_quickref.ps
GRUTATXT_DOCS=doc/mpsl_index.html doc/mpsl_overview.html doc/mpsl_internals.html
MP_DOCCER_DOCS=doc/mpsl_api.html

OBJS=mpsl_c.o mpsl_l.o mpsl_m.o mpsl_y.o mpsl_f.o

DIST_TARGET=/tmp/$(PROJ)-$(VERSION)

##################################################################

version:
	@echo $(VERSION)

.c.o:
	$(CC) $(CFLAGS) `cat config.cflags` -c $<

y.tab.h: mpsl.y
	$(YACC) -d mpsl.y

y.tab.c: mpsl.y
	$(YACC) -d mpsl.y

lex.yy.c: mpsl.l
	flex mpsl.l

mpsl_l.o: lex.yy.c y.tab.h
	$(CC) $(CFLAGS) `cat config.cflags` -c lex.yy.c -o mpsl_l.o

mpsl_y.o: y.tab.c
	$(CC) $(CFLAGS) `cat config.cflags` -c y.tab.c -o mpsl_y.o

$(MPDM)/libmpdm.a:
	( cd $(MPDM); $(MAKE) )

dep:
	gcc `cat config.cflags` -MM *.c | \
		sed -e 's;$(MPDM)/;$$(MPDM)/;g' > makefile.depend

$(LIB): $(OBJS)
	$(AR) rv $(LIB) $(OBJS)

$(TARGET): $(LIB) $(MPDM)/libmpdm.a
	$(CC) $(CFLAGS) -L. -lmpsl `cat config.ldflags` -o $@

stress: stress.c $(LIB) $(MPDM)/libmpdm.a
	$(CC) $(CFLAGS) `cat config.cflags` stress.c \
		-L. -lmpsl `cat config.ldflags` -o $@

clean:
	rm -f $(TARGET) $(LIB) $(OBJS) *.o tags *.tar.gz stress

realclean: clean
	rm -f y.tab.c y.tab.h lex.yy.c

distclean: clean y.tab.c y.tab.h lex.yy.c
	rm -f config.h config.cflags config.ldflags makefile.opts .config.log Makefile

realclean: distclean docsclean

realdistclean: realclean

.SUFFIXES: .txt .html

.txt.html:
	grutatxt < $< > $@

doc/mpsl_quickref.ps: doc/mpsl_quickref.txt
	-./mpsl scripts/mkquickref.mpsl < $< > $@

doc/mpsl_api.html: mpsl_*.c mpsl.y
	mkdir -p doc
	mp_doccer mpsl_*.c mpsl.y -o doc/mpsl_api -f html1 \
	-t "MPSL C API ($(VERSION))" \
	-a 'Angel Ortega - angel@triptico.com'

docs: $(DOCS)

docsclean:
	rm -f doc/*.html

distcopy: distclean
	mkdir -p $(DIST_TARGET) ; \
	tar cf - * | (cd $(DIST_TARGET) ; tar xf -)

dist: distcopy
	(cd /tmp ; tar czf - $(PROJ)-$(VERSION)/* ) > $(PROJ)-$(VERSION).tar.gz ; \
	rm -rf $(DIST_TARGET)

install: installdoc
	install $(TARGET) $(PREFIX)/bin

installdoc:
	install -m 644 doc/* $(DOCDIR)
	for f in README AUTHORS COPYING TODO RELEASE_NOTES ; do \
		[ -f $$f ] && install -m 644 $$f $(DOCDIR)/$$f.$(PROJ) ; \
	done
