all:	libcommon.a
ROOT=../..
include $(ROOT)/Config.mk
include $(ROOT)/makearch/$(ARCH)

INCS =  -I. \
        -I$(ROOT) \
        -I../cdt \
	-I../pack \
	-I../xdot \
	-I../fdpgen \
    -I../pathplan \
	-I../gd \
	-I../graph \
	-I../gvc \
	$(EXTLIB_INC)

DEFINES = -DHAVE_CONFIG_H

AWKDIR = $(ROOT)/awk

INST_HDRS = geom.h color.h types.h textpara.h usershape.h
NOINST_HDRS = render.h utils.h memory.h \
	geomprocs.h colorprocs.h const.h macros.h pointset.h
NOINST_GENERATED_HDRS = colortbl.h htmltable.h  htmlparse.h htmllex.h 

OBJS = arrows.o colxlate.o fontmetrics.o \
	args.o memory.o globals.o htmllex.o htmlparse.o htmltable.o input.o \
	pointset.o postproc.o routespl.o splines.o psusershape.o \
	timing.o labels.o ns.o shapes.o utils.o geom.o \
	output.o emit.o

libcommon.a : $(OBJS)
	$(RM) libcommon.a
	$(AR) cr libcommon.a $(OBJS)
	$(RANLIB) libcommon.a

htmllex.o : htmlparse.c

htmlparse.h htmlparse.c : htmlparse.y
	$(YACC) -dv htmlparse.y
	if test $(ARCH) = "netbsd.i386" -o $(ARCH) = "osx.i386"; then $(SED) "s/yy/html/g" < y.tab.c > htmlparse.c; else $(SED) 1s/#include.*// < y.tab.c | $(SED) "s/yy/html/g" > htmlparse.c; fi
	$(SED) "s/yy/html/g" < y.tab.h > htmlparse.h
	$(RM) y.tab.c y.tab.h

htmllex.o : htmllex.c
	$(CC) -c $(CCFLAGS) $(DEFINES) $(INCS) $(EXPAT_INC) htmllex.c

install: libcommon.a 
	$(MKPATH) $(INCDIR)
	$(INSTALL) $(INST_HDRS) $(INCDIR)
	$(MKPATH) $(LIBDIR)
	$(INSTALL) libcommon.a $(LIBDIR)

$(OBJS) : $(INST_HDRS) $(NOINST_HDRS)
colxlate.o : colortbl.h
emit.o mapgen.o utils.o htmllex.o htmltable.o htmlparse.o : htmltable.h
htmllex.o : htmlparse.h
htmlparse.o htmllex.o : htmllex.h
globals.o : globals.h

fontmetrics.o: ps_font_equiv.h

# ensure font names are properly sorted for bsearch operation 
ps_font_equiv.h: ps_font_equiv.txt ps_fontmap.txt ps_fontmap.txt
	cp ps_fontmap.txt ps_font_equiv.h
	./mksvgfonts.pl fontmap.cfg ps_font_equiv.txt | LC_ALL=C $(SORT) >>ps_font_equiv.h

colortbl.h : color_names brewer_colors
	$(AWK) -f $(AWKDIR)/brewer.awk brewer_colors > brewer_lib
	cat brewer_lib color_names | LC_COLLATE=C $(SORT) > color_lib
	$(AWK) -f $(AWKDIR)/colortbl.awk color_lib > colortbl.h
	$(RM) color_lib brewer_lib

clean:
	$(RM) core *.o htmlparse.c htmlparse.h colortbl.h y.output

distclean: clean
	$(RM) *.a