axiom_c_runtime_extra = @axiom_c_runtime_extra@

bin_PROGRAMS = htadd hypertex spadbuf hthits ex2ht

# this is where to put the various commands
OUTLIB=	$(axiom_target_libdir)

# this is where the include files live
INC=    $(axiom_src_srcdir)/include

# this is where the hypertex documentation files are
HYPER=$(axiom_target_datadir)/hypertex

HTADD=$(axiom_target_bindir)/htadd

BITMAPS = mouse11.bitmap mouse11.mask sdown3d.bitmap sdown3dpr.bitmap \
	sdown.bitmap sup3d.bitmap sup3dpr.bitmap sup.bitmap ht_icon

SCRIPTS=${OUTLIB}/htsearch ${OUTLIB}/presea

BINFILES= $(axiom_target_bindir)/hypertex$(EXEEXT) \
		$(axiom_target_bindir)/htadd$(EXEEXT) \
		${OUTLIB}/spadbuf$(EXEEXT) \
		${OUTLIB}/hthits$(EXEEXT) ${OUTLIB}/ex2ht$(EXEEXT)


HEADERS_src = addfile.h cond.h dialog.h display.h event.h extent.h \
	group.h hterror.h hyper.h initx.h keyin.h lex.h mem.h \
	parse.h parse-aux.h parse-paste.h parse-types.h scrollbar.h \
	show-types.h titlebar.h token.h

HEADERS = $(addprefix $(srcdir)/, $(HEADERS_src) $(BITMAPS)) $(axiom_c_macros_h)


build_libdir = $(abs_top_builddir)/src/lib

bin_PROGRAMS = hypertex$(EXEEXT) \
		htadd$(EXEEXT) \
		spadbuf$(EXEEXT) \
		ex2ht$(EXEEXT) \
		hthits$(EXEEXT)

hypertex_sources = addfile.c cond.c dialog.c display.c event.c extent1.c \
		extent2.c form-ext.c group.c halloc.c hash.c hterror.c \
		htinp.c hyper.c initx.c input.c item.c keyin.c lex.c \
		macro.c mem.c parse.c parse-aux.c parse-input.c \
		parse-paste.c parse-types.c ReadBitmap.c scrollbar.c \
		show-types.c spadint.c titlebar.c

hypertex_objects = $(hypertex_sources:.c=.$(OBJEXT))
hypertex_LDADD = -L$(build_libdir) -lspad $(axiom_c_runtime_extra)
hypertex_DEPENDENCIES = $(build_libdir)/libspad.a

htadd_sources = addfile.c halloc.c hash.c htadd.c hterror.c lex.c
htadd_SOURCES = $(htadd_sources:.c=.pamphlet)
htadd_objects = $(htadd_sources:.c=.$(OBJEXT))
# FIXME: remove extra dependency
htadd_LDADD = -L$(build_libdir) -lspad $(axiom_c_runtime_extra)
htadd_DEPENDENCIES = $(build_libdir)/libspad.a


spadbuf_sources = spadbuf.c
spadbuf_objects = $(spadbuf_sources:.c=.$(OBJEXT))
spadbuf_LDADD = -L$(build_libdir) -lspad $(axiom_c_runtime_extra)
spadbuf_DEPENDENCIES = $(build_libdir)/libspad.a

hthits_sources = hthits.c
hthits_objects = $(hthits_sources:.c=.$(OBJEXT))
hthits_LDADD = -L$(build_libdir) -lspad
hthits_DEPENDENCIES = $(build_libdir)/libspad.a

ex2ht_sources = ex2ht.c
ex2ht_objects = $(ex2ht_sources:.c=.$(OBJEXT))
ex2ht_LDADD = -L$(build_libdir) -lspad
ex2ht_DEPENDENCIES = $(build_libdir)/libspad.a


subdir = src/hyper/

.PHONY: all all-hyper util-ht
.SUFFIXES:
.SUFFIXES: .c .o .h

all: all-ax

all-ax all-hyper: stamp
	@ echo finished $(builddir)

stamp: ${SCRIPTS} ${BINFILES} ${HYPER}/pages/ht.db  
	-rm -f stamp
	$(STAMP) stamp

util-ht: stamp-util-ht

stamp-util-ht: $(srcdir)/pages/util.ht
	mkdir -p ${HYPER}/pages
	cp $(srcdir)/pages/util.ht ${HYPER}/pages/util.ht
	$(STAMP) stamp-util-ht

mostlyclean-local:
	-rm -f $(hypertex_objects)
	-rm -f $(htadd_objects)
	-rm -f $(htsearch_objects)
	-rm -f $(spadbuf_objects)
	-rm -f $(hthits_objects)
	-rm -f $(ex2ht_objects)
	-rm -f stamp stamp-util-ht


clean-local: mostlyclean-local
	-rm -f $(BINFILES)

distclean-local: clean-local

.PRECIOUS: %.$(OBJEXT)

%.$(OBJEXT): $(srcdir)/%.c $(HEADERS)
	$(CC) $(CFLAGS) ${CCF} $(axiom_includes) $(AXIOM_X11_CFLAGS) \
	    -I$(srcdir) -c -o $@ $<


# We need to make sure that 'ht.db', the hypertex database file
# is up to date. The file contains absolute offsets into the various
# '.ht' and '.pht' files so it must reflect the current pages. In
# order to do this we run 'htadd' in the '${HYPER}/pages' directory.

# '.pht' pages are generated later from 'paste/Makefiel.in'

${HYPER}/pages/ht.db: $(HTADD) $(srcdir)/pages/*.ht
	@echo making ${HYPER}/pages from $(axiom_src_srcdir)/pages directory
	mkdir -p ${HYPER}/pages
	mkdir -p $(axiom_target_datadir)/viewports
	cp $(srcdir)/pages/*.ht ${HYPER}/pages
	(cd ${HYPER}/pages ; \
           rm -f ht.db ; \
           rm -f *~ ; \
	   ${HTADD} *.ht )
	rm -rf ${HYPER}/bitmaps
	mkdir -p ${HYPER}/bitmaps
	cp $(srcdir)/bitmaps/*.bitmap $(srcdir)/bitmaps/*.xbm \
	        ${HYPER}/bitmaps
	for A in anna.xbm.tiny door drown.bm help2.bakmap \
	      ht_icon integral.bm sdown.bm sum.bm sup.bm ; do \
	   cp $(srcdir)/bitmaps/$$A ${HYPER}/bitmaps ; \
	done


${OUTLIB}/htsearch: $(srcdir)/htsearch
	cp $< $@
	chmod a+x $@

${OUTLIB}/presea: $(srcdir)/presea.in
	sed 's,@TARGET_AWK@,${TARGET_AWK},' $< > $@
	chmod a+x $@

${OUTLIB}/ex2ht$(EXEEXT): $(ex2ht_objects) $(ex2ht_DEPENDENCIES)
	${CC} $(ex2ht_objects) -o $@ $(ex2ht_LDADD) $(AXIOM_X11_LDFLAGS) -lm

$(axiom_target_bindir)/htadd$(EXEEXT): $(htadd_objects) $(htadd_DEPENDENCIES)
	${CC} $(htadd_objects) -o $@ $(htadd_LDADD) $(AXIOM_X11_LDFLAGS) -lm

${OUTLIB}/hthits$(EXEEXT): $(hthits_objects) $(hthits_DEPENDENCIES)
	${CC} $(hthits_objects) -o $@ $(hthits_LDADD) $(AXIOM_X11_LDFLAGS) -lm


$(axiom_target_bindir)/hypertex$(EXEEXT): $(hypertex_objects) $(hypertex_DEPENDENCIES)
	 ${CC} -g $(hypertex_objects) -o $@ $(hypertex_LDADD) \
		$(AXIOM_X11_LDFLAGS) -lm

${OUTLIB}/spadbuf$(EXEEXT): $(spadbuf_objects) $(spadbuf_DEPENDENCIES)
	${CC} $(spadbuf_objects) -o $@ $(spadbuf_LDADD) \
		$(AXIOM_X11_LDFLAGS) -lm
