#
# This Makefile should run fine with both pmake and gmake
#

CC = @CC@
CFLAGS = @CFLAGS@ @DEFS@ @NO_GETOPT@ @NO_LIBPAPER@ @NO_STRERROR@
RANLIB = @RANLIB@

INSTALL = @INSTALL@

LDFLAGS = -L. -l$(TARGET) @LIBPAPER@

prefix = @prefix@
BINDIR = $(prefix)/bin
LIBDIR = $(prefix)/lib
INCDIR = $(prefix)/include
MAN1DIR = $(prefix)/man/man1
MAN3DIR = $(prefix)/man/man3
INFODIR = $(prefix)/info

# getopt may be installed or not, if not take our copy
GETOPT_O = @GETOPT_O@

TARGET = barcode
LIBRARY = lib$(TARGET).a
MAN1 = $(TARGET).1
MAN3 = $(TARGET).3
INFO = doc/$(TARGET).info
HEADER = $(TARGET).h

LIBOBJECTS = library.o ean.o code128.o code39.o code93.o i25.o \
		msi.o plessey.o codabar.o \
		ps.o pcl.o
EXEOBJECTS = main.o cmdline.o $(GETOPT_O)
ALLSOURCES = $(LIBOBJECTS:.o=.c) $(EXEOBJECT:.o=.c)


#Hmm... "RM" is undefined in pmake
RM = rm

all: $(TARGET) $(LIBRARY) $(MAN1) $(MAN3) $(INFO) sample

$(TARGET): $(LIBRARY) $(EXEOBJECTS)
	$(CC) $(CFLAGS) $(EXEOBJECTS) $(LDFLAGS) -o $(TARGET)

sample: sample.o $(LIBRARY)
	$(CC) $(CFLAGS) sample.o $(LDFLAGS) -o $@ 

# Avoid the standard CFLAGS, to avoid -Wall and -DNO_GETOPT
compat/getopt.o: compat/getopt.c
	$(CC) -O -c compat/getopt.c -o $@

$(LIBRARY): $(LIBOBJECTS)
	$(AR) r $(LIBRARY) $(LIBOBJECTS)
	$(RANLIB) $(LIBRARY)

$(MAN1) $(MAN3): doc/doc.$(TARGET)
	awk -f doc/manpager doc/doc.$(TARGET)


# Unfortunately, pmake has no "-C". Also, "pmake -n" doesn't follow the cd.
$(INFO): doc/doc.$(TARGET)
	cd doc && $(MAKE) 

install:
	$(INSTALL) -d $(BINDIR) $(INCDIR) $(LIBDIR) $(MAN1DIR) \
		$(MAN3DIR) $(INFODIR)
	$(INSTALL) -c $(TARGET) $(BINDIR)
	$(INSTALL) -c -m 0644 $(HEADER) $(INCDIR)
	$(INSTALL) -c -m 0644  $(LIBRARY) $(LIBDIR)
	$(INSTALL) -c -m 0644  $(MAN1) $(MAN1DIR)
	$(INSTALL) -c -m 0644  $(MAN3) $(MAN3DIR)
	$(INSTALL) -c -m 0644  $(INFO) $(INFODIR)

uninstall:
	$(RM) -f $(BINDIR)/$(TARGET)
	$(RM) -f $(INCDIR)/$(HEADER)
	$(RM) -f $(LIBDIR)/$(LIBRARY)
	$(RM) -f $(MAN1DIR)/$(MAN1)
	$(RM) -f $(MAN3DIR)/$(MAN3)
	$(RM) -f $(INDODIR)/$(INFO)

#Make clean keeps the compiled documents
clean:
	$(RM) -f *.o */*.o *~ */*~ $(TARGET) $(LIBRARY)
	$(RM) -f  $(MAN1) $(MAN3) core sample
	cd doc && $(MAKE) terse
	$(RM) -f .depend

distclean: clean
	if [ -f build ]; then debian/rules clean; fi
	# remove the configure stuff as well
	$(RM) -f Makefile config.h config.log config.status config.cache \
		doc/Makefile

cleanest: clean
	cd doc && $(MAKE) clean

Makefile: Makefile.in configure
	./configure

configure: configure.in
	autoconf

.depend: $(ALLSOURCES)
	$(CC) $(CFLAGS) -MM $(ALLSOURCES) > $@

depend: .depend

tar:
	@if [ "x" = "x$(RELEASE)" ]; then \
	    n=`basename \`pwd\``; cd ..; tar cvf - $$n | gzip > $$n.tar.gz; \
	    echo 'you can set a numeric $$(RELEASE) to make a named tar'; \
	else \
	    if [ -d ../$(TARGET)-$(RELEASE) ]; then \
		rm -rf ../$(TARGET)-$(RELEASE); \
	    fi; \
	    mkdir ../$(TARGET)-$(RELEASE) || exit 1; \
	    cp -a . ../$(TARGET)-$(RELEASE) && cd .. && \
	        tar --exclude '*/CVS*' \
		    -cvzf $(TARGET)-$(RELEASE).tar.gz $(TARGET)-$(RELEASE); \
	fi

# print the version, as I usually forget to update it when distributing
printv:
	@grep -n VERSION $(HEADER) /dev/null
	@grep -n set.version doc/doc.$(TARGET) /dev/null
	@grep -n dpkg.-i INSTALL /dev/null

# and this is how I make the distribution
distrib: $(INFO) distclean tar printv


.PHONY: all install uninstall mostlyclean clean disclean depend \
	tar printv distrib

# Only if make is gmake, have a rule to conditionally include .depend
@GMAKEDEPEND0@
@GMAKEDEPEND1@
@GMAKEDEPEND2@

