SRCDIR=../..
BUILDDIR = $(SRCDIR)

include $(BUILDDIR)/Makefile.config

PNMDIR = $(BUILDDIR)/pnm
LIBPNM = $(NETPBMSHLIBPREFIX)pnm.$(NETPBMLIBSUFFIX)

PPMDIR = $(BUILDDIR)/ppm
LIBPPM = $(NETPBMSHLIBPREFIX)ppm.$(NETPBMLIBSUFFIX)

PGMDIR = $(BUILDDIR)/pgm
LIBPGM = $(NETPBMSHLIBPREFIX)pgm.$(NETPBMLIBSUFFIX)

PBMDIR = $(BUILDDIR)/pbm
LIBPBM = $(NETPBMSHLIBPREFIX)pbm.$(NETPBMLIBSUFFIX)

JBIGDIR = $(BUILDDIR)/pnm/jbig
LIBJBIG = jbig.o jbig_tab.o

INCLUDE = -I$(SRCDIR)/shhopt
ifneq ($(JBIGHDR_DIR),NONE)
  INCLUDE += -I$(JBIGHDR_DIR)
endif

NETPBMLIBS = $(PNMDIR)/$(LIBPNM) \
           $(PPMDIR)/$(LIBPPM) $(PGMDIR)/$(LIBPGM) $(PBMDIR)/$(LIBPBM) 

ifeq ($(JBIGLIB_DIR),NONE)
  JBIGLD =
else
  ifeq ($(JBIGLIB_DIR),$(JBIGDIR))
    JBIGLD = $(JBIGDIR)/$(LIBJBIG)
  else
    JBIGLD = -L$(JBIGLIB_DIR) -ljbig
    ifeq ($(NEED_RUNTIME_PATH),Y)
      JBIGLD += -R$(JBIGLIB_DIR)
    endif
  endif
endif

ifneq ($(JBIGHDR_DIR),NONE)
  ifneq ($(JBIGLIB_DIR),NONE)
    BINARIES = jbigtopnm pnmtojbig
  endif
endif

OBJECTS = $(patsubst %, %.o, $(BINARIES)) $(LIBJBIG)

MANUALS1 = $(BINARIES)

all: $(BINARIES)

ifeq ($(JBIGLIB_DIR),$(JBIGDIR))
# We're using the internal JBIG library, so make sure it's built
JBIGLIB_DEP = $(LIBJBIG)
else
# It's user's own JBIG library, so if it doesn't exist now -- too bad.
JBIGLIB_DEP =
endif

$(BINARIES): %: %.o $(JBIGLIB_DEP) $(NETPBMLIBS) $(LIBOPT)
	$(LD) $(LDFLAGS) -o $@ $< $(JBIGLD) `$(LIBOPT) $(NETPBMLIBS)` \
	  $(CDEBUG)


.PHONY: install.bin
install.bin: install.bin.common

.PHONY: install.man
install.man: install.man.common

# And libraries.
$(PBMDIR)/$(LIBPBM): FORCE
	$(MAKE) -C $(PBMDIR) $(LIBPBM)
$(PGMDIR)/$(LIBPGM): FORCE
	$(MAKE) -C $(PGMDIR) $(LIBPGM)
$(PPMDIR)/$(LIBPPM): FORCE
	$(MAKE) -C $(PPMDIR) $(LIBPPM)
$(PNMDIR)/$(LIBPNM): FORCE
	$(MAKE) -C $(PNMDIR) $(LIBPNM)

HEADERLINKS = pm_config.h pm.h pbm.h pgm.h ppm.h pnm.h

include $(SRCDIR)/Makefile.common

.PHONY: clean
clean: clean.common

FORCE:

