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

LIBROOT = pnm
LIBPNM = lib$(LIBROOT).so
MAJ = 1
MIN = 2

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

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

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

TIFFDIR = ../libtiff
LIBTIFF = libtiff.so

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

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

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 giftopnm \
		pnmalias pnmarith pnmcat pnmcomp pnmconvol pnmcrop \
		pnmcut pnmdepth pnmenlarge pnmfile pnmflip pnmhisteq \
		pnmhistmap 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
BINARIES = $(PORTBINARIES) $(MATHBINARIES) $(NOMERGEBINARIES)
SCRIPTS = anytopnm pnmindex 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

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

MERGE_OBJECTS = $(patsubst %,%.o2, $(PORTBINARIES) $(MATHBINARIES))

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)
PHONY: merge
merge:    $(MERGENAME) $(NOMERGEBINARIES)

tifftopnm pnmtotiff:  %: %.o $(NETPBMLIBS) $(TIFFLIB_DIR)/libtiff.so 
	$(LD) $(LDFLAGS) -o $@ $@.o $(NETPBMLIBS) -L$(TIFFLIB_DIR) -ltiff \
	  $(JPEGLD) $(CDEBUG)

pnmtotiffcmyk: %: %.o $(NETPBMLIBS) $(TIFFLIB_DIR)/libtiff.so
	$(LD) $(LDFLAGS) -o $@ $@.o $(NETPBMLIBS) -lm -L$(TIFFLIB_DIR) -ltiff \
	  $(JPEGLD) $(CDEBUG)

# Rules for plain programs.
$(PORTBINARIES): %: %.o $(NETPBMLIBS) 
	$(LD) $(LDFLAGS) -o $@ $@.o $(NETPBMLIBS) $(CDEBUG)

# Rule for math-dependent programs.
$(MATHBINARIES): %: %.o $(NETPBMLIBS)
	$(LD) $(LDFLAGS) -o $@ $@.o -lm $(NETPBMLIBS) $(CDEBUG)

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

jpegtopnm: %: %.o $(NETPBMLIBS)
	$(LD) $(LDFLAGS) -o $@ $@.o $(NETPBMLIBS) $(JPEGLD) -lm $(CDEBUG)

# Rule for objects.
$(OBJECTS) $(LIBOBJECTS): %.o: %.c
	$(CC) -c $(CFLAGS) $(INCLUDE) -o $@ $<

$(MERGE_OBJECTS): %.o2: %.c
	$(CC) -c $(CFLAGS) $(INCLUDE) "-Dmain=$*_main" -o $@ $<

# 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
	ln -sf $(INSTALLBINARIES)/pnmtoplainpnm $(INSTALLBINARIES)/pnmnoraw

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

.PHONY: clean
clean:
	-rm -f *.o *.o2 *.a *.so* *.cat *~ core $(BINARIES) $(MERGENAME)

FORCE: