# makefile for magicfilter-2

OBJS=getline.o xyzzy.o actions.o magic.o trace.o reject.o args.o getaline.o
LIB     = libmf.a
CC      = @CC@
LDFLAGS = @MAGIC_LIB@ -L. @LDFLAGS@
CFLAGS  = -I. -I@srcdir@ @MAGIC_HEADER@ @CFLAGS@
LIBES   = -lmf -lmagic @LIBS@
RANLIB  = @RANLIB@
PROGS=traditional magicfilter mpp textonly
VPATH   = @srcdir@
MAGIC	= @LIBMAGIC@

all: $(PROGS)

libmagic @LIBMAGIC@:
	@MAKE_MAGIC@ all

textonly: contrib/textonly.c
	$(CC) $(CFLAGS) -o textonly $<

mpp: magic.m4 mkmpp
	@srcdir@/mkmpp magic.m4

traditional: $(LIB) fe.c @LIBMAGIC@
	$(CC) $(CFLAGS) $(LDFLAGS) -DNOFE -o traditional @srcdir@/fe.c $(LIBES)

magicfilter: $(LIB) fe.c @LIBMAGIC@
	$(CC) $(CFLAGS) $(LDFLAGS) -o magicfilter @srcdir@/fe.c $(LIBES)

$(OBJS): rule.h magicfilter.h

$(LIB): $(OBJS) @XGETOPT@
	$(AR) cr $(LIB) $(OBJS) @XGETOPT@
	$(RANLIB) $(LIB)

ignore @XGETOPT@: basis/options.h basis/options.c
	$(CC) $(CFLAGS) -c -o @XGETOPT@ @srcdir@/basis/options.c

magic.c: magic.m4 mkmagic config.h
	@srcdir@/mkmagic magic.m4

cfmagic: file/magic
	@MAKE_MAGIC@ magic

commoninstall: textonly @CFMAGIC@
	@INSTALL_DIR@ $(prefix)/@exedir@
	@INSTALL_DIR@ $(prefix)/@mandir@/man8
	@INSTALL_DIR@ $(prefix)/@mandir@/man5
	@INSTALL_DIR@ $(prefix)/@filterdir@
	@INSTALL_MAGIC@ -m 444 file/magic $(prefix)/@MAGIC@
	@INSTALL_PROGRAM@ -m 511 textonly $(prefix)/@filterdir@

install:  @DO_WHAT@

install-bin-t: traditional commoninstall
	@INSTALL_PROGRAM@ -m 511 traditional $(prefix)/@exedir@/magicfilter-t

install-bin: magicfilter commoninstall
	@INSTALL_PROGRAM@ -m 511 magicfilter $(prefix)/@exedir@

install-data-t: traditional commoninstall
	@srcdir@/mkmpp
	for x in @srcdir@/filters/*.def;do \
	    ./mpp @exedir@/magicfilter-t $$x > $(prefix)/@filterdir@/`basename $$x .def`-filter; \
	    chmod +x $(prefix)/@filterdir@/`basename $$x .def`-filter; \
	done
	@rm -f mpp

install-data: magicfilter commoninstall
	for x in @srcdir@/filters/*.def;do \
	    sed -e 's,@MAGIC''FILTER@,@exedir@/magicfilter,' < $$x > $(prefix)/@filterdir@/`basename $$x .def`; \
	    chmod +x $(prefix)/@filterdir@/`basename $$x .def`; \
	done

install-man: VERSION commoninstall
	@SED@ -n -e 's/&PROGRAM&/magicfilter/g' -e 's/^MAN //p' -e @LPD_OPTS@ < magicfilter.8templ > $$$$; \
	@INSTALL_DATA@ -m 444 $$$$ $(prefix)/@mandir@/man8/magicfilter.8; \
	rm -f $$$$
	@INSTALL_DATA@ -m 444 magicfilter.5 $(prefix)/@mandir@/man5

install-man-t: VERSION commoninstall
	@SED@ -n -e 's/&PROGRAM&/magicfilter-t/g' -e 's/^MAN //p' -e 's/^TRAD//p' -e @LPD_OPTS@ < magicfilter.8templ > $$$$; \
	@INSTALL_DATA@ -m 444 $$$$ $(prefix)/@mandir@/man8/magicfilter-t.8; \
	rm -f $$$$
	@INSTALL_DATA@ -m 444 magicfilter-t.5 $(prefix)/@mandir@/man5

install-traditional: install-man-t install-bin-t install-data-t

install-magicfilter: install-man install-bin install-data 

export: VERSION
	V=`cat VERSION`; \
	if bk export -tplain -r+ /tmp/magicfilter-$$V; then \
	    cd /tmp; \
	    (cd magicfilter-$$V;./joincfg.sh); \
	    tar czf magicfilter-$$V.tar.gz magicfilter-$$V; \
	    rm -rf magicfilter-$$V; \
	fi

spotless distclean: clean
	rm -f @GENERATED_FILES@ @CONFIGURE_FILES@

clean:
	rm -f $(OBJS) magic.c $(PROGS) $(LIB) @XGETOPT@
	@MAKE_MAGIC@ clean

# redefine .c.o to get around gmake
.c.o:
	$(CC) $(CFLAGS) -c -o $*.o $<
