SRCDIR = ..
include $(SRCDIR)/Makefile.config

LIBROOT = pnm
LIBPNM = lib$(LIBROOT).$(NETPBMLIBSUFFIX)
MAJ = 9
MIN = 0

PPMDIR = $(SRCDIR)/ppm
LIBPPM = libppm.$(NETPBMLIBSUFFIX)

PGMDIR = $(SRCDIR)/pgm
LIBPGM = libpgm.$(NETPBMLIBSUFFIX)

PBMDIR = $(SRCDIR)/pbm
LIBPBM = libpbm.$(NETPBMLIBSUFFIX)

TIFFDIR = ../libtiff
LIBTIFF = libtiff.$(LIBTIFFSUFFIX)

INCLUDE = -I$(SRCDIR) -I$(PPMDIR) -I$(PGMDIR) -I$(PBMDIR) \
          -I$(SRCDIR)/shhopt
ifneq ($(PNGHDR_DIR),NONE)
  INCLUDE += -I$(PNGHDR_DIR)
endif
ifneq ($(TIFFHDR_DIR),NONE)
  INCLUDE += -I$(TIFFHDR_DIR)
endif
ifneq ($(JPEGHDR_DIR),NONE)
  INCLUDE += -I$(JPEGHDR_DIR)
endif
ifneq ($(URTHDR_DIR),NONE)
  INCLUDE += -I$(URTHDR_DIR)
endif

# The order of these libraries is important for static library build
NETPBMLIBS = $(LIBPNM) \
           $(PPMDIR)/$(LIBPPM) $(PGMDIR)/$(LIBPGM) $(PBMDIR)/$(LIBPBM) 
LIBLIBS =  $(PPMDIR)/$(LIBPPM) $(PGMDIR)/$(LIBPGM) $(PBMDIR)/$(LIBPBM) 

ifeq ($(JPEGLIB_DIR),NONE)
  JPEGLD =
else
  JPEGLD = -L$(JPEGLIB_DIR) -ljpeg
endif

# We tend to separate out the build targets so that we don't have
# any more dependencies for a given target than it really needs.
# That way, if there is a problem with a dependency, we can still
# successfully build all the stuff that doesn't depend upon it.
# This package is so big, it's useful even when some parts won't 
# build.

PORTBINARIES = fitstopnm gemtopnm giftopnm \
		pnmalias pnmarith pnmcat pnmcomp pnmconvol pnmcrop \
		pnmcut pnmdepth pnmenlarge pnmfile pnmflip pnmhisteq \
		pnmhistmap pnminterp pnminvert pnmpad pnmpaste \
		pnmsmooth pnmtile pnmtoddif pnmtofits \
		pnmtoplainpnm pnmtops pnmtorast \
		pnmtosgi pnmtosir pnmtoxwd \
		rasttopnm sgitopnm sirtopnm xwdtopnm zeisstopnm
MATHBINARIES =	pnmgamma pnmnlfilt pnmrotate pnmscale pnmshear

# We don't include programs that have special library dependencies in the
# merge scheme, because we don't want those dependencies to prevent us
# from building all the other programs.

NOMERGEBINARIES = 
ifneq ($(PNGHDR_DIR),NONE)
  ifneq ($(PNGLIB_DIR),NONE)
    NOMERGEBINARIES += pnmtopng pngtopnm
  endif
endif
ifneq ($(JPEGHDR_DIR),NONE)
  ifneq ($(JPEGLIB_DIR),NONE)
    NOMERGEBINARIES += jpegtopnm
  endif
endif
ifneq ($(TIFFHDR_DIR),NONE)
  ifneq ($(TIFFLIB_DIR),NONE)
    NOMERGEBINARIES +=  tifftopnm pnmtotiff pnmtotiffcmyk
  endif
endif
ifneq ($(URTHDR_DIR),NONE)
  ifneq ($(URTLIB_DIR),NONE)
    NOMERGEBINARIES +=  rletopnm pnmtorle
  endif
endif

MERGEBINARIES = $(PORTBINARIES) $(MATHBINARIES)


BINARIES = $(MERGEBINARIES) $(NOMERGEBINARIES)
SCRIPTS = anytopnm pnmindex pnminterp-gen pnmmargin pstopnm
# MERGE_ALIASES is additional names to be linked to the merge binary which are
# alternate names for a program whose source is called something else.
# Every name here must be referenced in pnmmerge.c.
MERGE_ALIASES = pnmnoraw gemtopbm

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

MERGE_OBJECTS = $(patsubst %,%.o2, $(MERGEBINARIES))

LIBOBJECTS = libpnm1.o libpnm2.o libpnm3.o libpnm4.o

MANUALS1 = $(BINARIES) $(SCRIPTS)
MANUALS3 = libpnm
MANUALS5 = pnm

MERGENAME = pnmmerge

INTERFACE_HEADERS = pnm.h

.PHONY: all
all:      $(BINARIES)
	$(MAKE) -C jbig all
PHONY: merge
merge:    $(MERGENAME) $(NOMERGEBINARIES)
	$(MAKE) -C jbig all

ifeq ($(TIFFLIB_DIR),$(TIFFDIR))
# We're using the internal Tiff library, so make sure it's built
TIFFLIB_DEP = $(TIFFDIR)/$(LIBTIFF)
else
# It's user's own Tiff library, so if it doesn't exist now -- too bad.
TIFFLIB_DEP = 
endif

# The Tiff library references math functions.
tifftopnm pnmtotiff pnmtotiffcmyk: %: %.o \
  $(NETPBMLIBS) $(TIFFLIB_DIR)/libtiff.$(LIBTIFFSUFFIX) $(LIBOPT)
	$(LD) $(LDFLAGS) -o $@ $@.o `$(LIBOPT) $(NETPBMLIBS)` \
	  -L$(TIFFLIB_DIR) -ltiff $(JPEGLD) -lm $(CDEBUG)

pngtopnm pnmtopng: %: %.o $(NETPBMLIBS) $(LIBOPT)
	$(LD) $(LDFLAGS) -o $@ $@.o `$(LIBOPT) $(NETPBMLIBS)` \
	  -L$(PNGLIB_DIR) -lpng -lz -lm $(CDEBUG)

jpegtopnm: %: %.o $(NETPBMLIBS) $(LIBOPT)
	$(LD) $(LDFLAGS) -o $@ $@.o `$(LIBOPT) $(NETPBMLIBS)` $(JPEGLD) \
	   $(CDEBUG)

rletopnm pnmtorle: %: %.o $(NETPBMLIBS) $(LIBOPT)
	$(LD) $(LDFLAGS) -o $@ $@.o `$(LIBOPT) $(NETPBMLIBS)` \
	  -L$(URTLIB_DIR) -lrle $(CDEBUG)

# And libraries.
$(PBMDIR)/$(LIBPBM): FORCE
	cd $(PBMDIR) ; $(MAKE) $(LIBPBM)
$(PGMDIR)/$(LIBPGM): FORCE
	cd $(PGMDIR) ; $(MAKE) $(LIBPGM)
$(PPMDIR)/$(LIBPPM): FORCE
	cd $(PPMDIR) ; $(MAKE) $(LIBPPM)
$(TIFFDIR)/$(LIBTIFF): FORCE
	cd $(TIFFDIR) ; $(MAKE) $(LIBTIFF)

include $(SRCDIR)/Makefile.common

.PHONY: install.bin
install.bin: install.bin.common
# backward compatibility: program used to be named pnmnoraw
	rm -f $(INSTALLBINARIES)/pnmnoraw
	ln -s $(INSTALLBINARIES)/pnmtoplainpnm$(EXE) \
	  $(INSTALLBINARIES)/pnmnoraw
# backward compatibility: program used to be gemtopbm
	rm -f $(INSTALLBINARIES)/gemtopbm
	ln -s $(INSTALLBINARIES)/gemtopnm$(EXE) $(INSTALLBINARIES)/gemtopbm
	$(MAKE) -C jbig install.bin

.PHONY: install.merge
install.merge: install.merge.common
	$(MAKE) -C jbig install.bin

.PHONY: install.lib
install.lib: install.lib.common

.PHONY: install.man
install.man: install.man.common
	$(MAKE) -C jbig install.man


.PHONY: clean
clean: clean.common
	$(MAKE) -C jbig clean

FORCE:
