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

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

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

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

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

TIFFDIR = $(BUILDDIR)/libtiff
LIBTIFF = libtiff.$(NETPBMLIBSUFFIX)

URTDIR = $(BUILDDIR)/urt
LIBURT = librle.a

INCLUDE = -I$(SRCDIR)/shhopt
ifneq ($(TIFFHDR_DIR),NONE)
  INCLUDE += -I$(TIFFHDR_DIR)
endif
ifneq ($(PNGHDR_DIR),NONE)
  INCLUDE += -I$(PNGHDR_DIR)
  INCLUDE += -I$(ZHDR_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
  ifeq ($(NEED_RUNTIME_PATH),Y)
    JPEGLD += -R$(JPEGLIB_DIR)
  endif
endif

ifeq ($(PNGLIB_DIR),NONE)
  PNGLD =
else
  PNGLD = -L$(PNGLIB_DIR) -lpng
  ifeq ($(NEED_RUNTIME_PATH),Y)
    PNGLD += -R$(PNGLIB_DIR)
  endif
endif

ifeq ($(URTLIB_DIR),NONE)
  URTLD =
else
  URTLD = -L$(URTLIB_DIR) -lrle
  ifeq ($(NEED_RUNTIME_PATH),Y)
    URTLD += -R$(URTLIB_DIR)
  endif
endif

ifeq ($(TIFFLIB_DIR),NONE)
  TIFFLD =
else
  TIFFLD = -L$(TIFFLIB_DIR) -ltiff
  ifeq ($(NEED_RUNTIME_PATH),Y)
    TIFFLD += -R$(TIFFLIB_DIR)
  endif
endif

ifeq ($(ZLIB_DIR),NONE)
  ZLD =
else
  ZLD = -L$(ZLIB_DIR) -lz
  ifeq ($(NEED_RUNTIME_PATH),Y)
    ZLD += -R$(ZLIB_DIR)
  endif
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 pamchannel pamcut \
		pamfile pamoil pamtopnm \
		pnmalias pnmarith pnmcat pnmcomp pnmconvol pnmcrop \
		pnmcut pnmdepth pnmenlarge pnmfile pnmflip pnmhisteq \
		pnmhistmap pnminterp pnminvert pnmmontage pnmpad pnmpaste \
		pnmsmooth pnmsplit pnmtile pnmtoddif pnmtofits \
		pnmtoplainpnm pnmtops pnmtorast \
		pnmtosgi pnmtosir pnmtoxwd pstopnm \
		rasttopnm sgitopnm sirtopnm xwdtopnm zeisstopnm
MATHBINARIES =	pnmgamma pnmnlfilt pnmpsnr pnmrotate \
	pnmscale pnmscalefixed 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
# 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 libpam.o

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

MERGENAME = pnmmerge

INTERFACE_HEADERS = pnm.h

.PHONY: all
all:      $(BINARIES)
ifneq ($(BUILD_FIASCO), N) 
	$(MAKE) -C fiasco all
endif
	$(MAKE) -C jbig all
	$(MAKE) -C pnmtopalm all
PHONY: merge
merge:    $(MERGENAME) $(NOMERGEBINARIES)
ifneq ($(BUILD_FIASCO), N) 
	$(MAKE) -C fiasco all
endif
	$(MAKE) -C jbig all
	$(MAKE) -C pnmtopalm all

# The Tiff library references math functions.
tifftopnm pnmtotiff pnmtotiffcmyk: %: %.o $(NETPBMLIBS) $(LIBOPT)
	$(LD) $(LDFLAGS) -o $@ $@.o `$(LIBOPT) $(NETPBMLIBS)` \
	  $(TIFFLD) $(JPEGLD) -lm $(TIFFLIB_LDFLAGS) $(CDEBUG)

pngtopnm pnmtopng: %: %.o $(NETPBMLIBS) $(LIBOPT)
	$(LD) $(LDFLAGS) -o $@ $@.o `$(LIBOPT) $(NETPBMLIBS)` \
	  $(PNGLD) $(ZLD) -lm $(CDEBUG)

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

ifeq ($(URTLIB_DIR),$(URTDIR))
# We're using the internal URT library, so make sure it's built
URTLIB_DEP = $(URTDIR)/$(LIBURT)
else
# It's user's own URT library, so if it doesn't exist now -- too bad.
URTLIB_DEP = 
endif

rletopnm pnmtorle: %: %.o $(NETPBMLIBS) $(LIBOPT) $(URTLIB_DEP)
	$(LD) $(LDFLAGS) -o $@ $@.o `$(LIBOPT) $(NETPBMLIBS)` \
	  $(URTLD) $(CDEBUG)

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

HEADERLINKS = pm_config.h pm.h pbm.h libpbm.h pgm.h libpgm.h ppm.h libppm.h ppmcmap.h

include $(SRCDIR)/Makefile.common

.PHONY: install.bin
install.bin: install.bin.common
# Remember that $(SYMLINK) might just be a copy command.
# backward compatibility: program used to be named pnmnoraw
	cd $(INSTALLBINARIES) ; \
	rm -f pnmnoraw ; \
	$(SYMLINK) pnmtoplainpnm$(EXE) pnmnoraw
# backward compatibility: program used to be gemtopbm
	cd $(INSTALLBINARIES) ; \
	rm -f gemtopbm ; \
	$(SYMLINK) gemtopnm$(EXE) gemtopbm
ifneq ($(BUILD_FIASCO), N) 
	$(MAKE) -C fiasco install.bin
endif
	$(MAKE) -C jbig install.bin
	$(MAKE) -C pnmtopalm install.bin

.PHONY: install.merge
install.merge: install.merge.common
ifneq ($(BUILD_FIASCO), N) 
	$(MAKE) -C fiasco install.bin
endif
	$(MAKE) -C jbig install.bin
	$(MAKE) -C pnmtopalm install.bin

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

.PHONY: install.man
install.man: install.man.common
ifneq ($(BUILD_FIASCO), N) 
	$(MAKE) -C fiasco install.man
endif
	$(MAKE) -C jbig install.man
	$(MAKE) -C pnmtopalm install.man

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

FORCE:
