# Makefile for pgm tools.
#
# Copyright (C) 1989, 1991 by Jef Poskanzer.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation.  This software is provided "as is" without express or
# implied warranty.

# Default values, usually overridden by top-level Makefile.
#CC =		cc
# When we put -ansi in the following, we get complaints about our use of
# bzero() in asciitopgm, for one thing.
CC =		gcc -pedantic -fpcc-struct-return
CFLAGS =	-O2 -Wall -Wno-uninitialized $(CDEBUG)
INSTALLBINARIES = /usr/local/netpbm
INSTALLIBS = $(INSTALLBINARIES)
INSTALLMANUALS1 = /usr/man/man1
SUFFIXMANUALS1 =1
INSTALLMANUALS3 = /usr/man/man3
SUFFIXMANUALS3 = 3
INSTALLMANUALS5 = /usr/man/man5
SUFFIXMANUALS5 = 5
MANCP =	cp

#If you change the LIBx variables from .so to .a, you should get an
#old fashioned static link of all the netpbm and tiff libraries.

LIBPGM = libpgm.so
# The minor number in the library filename corresponds to the point release of
# Netpbm at which the library last changed.  Do not change the major 
# number (and soname) unless the old library is no longer forward compatible.
MAJ = 1
MIN = 0

PBMDIR =	../pbm
INCLUDEPBM =	-I$(PBMDIR)
LIBPBM = libpbm.so

SHELL =		/bin/sh
INCLUDE =	-I.. $(INCLUDEPBM)

NETPBMLIBS = $(LIBPGM) $(PBMDIR)/$(LIBPBM)

PORTBINARIES =	asciitopgm bioradtopgm fstopgm hipstopgm \
		lispmtopgm pbmtopgm pgmbentley pgmenhance pgmhist \
		pgmnoise pgmnorm pgmoil pgmramp pgmtofs pgmtolispm \
		pgmtopbm psidtopgm spottopgm
MATHBINARIES =	pgmcrater pgmedge pgmtexture rawtopgm pgmkernel
BINARIES =	$(PORTBINARIES) $(MATHBINARIES)

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

LIBOBJECTS = libpgm1.o libpgm2.o

MANUALS1 =	$(BINARIES)
MANUALS3 =	libpgm
MANUALS5 =	pgm


.PHONY: all
all:		$(BINARIES)
.PHONY: merge
merge:      pgmmerge

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

# Rule for math-dependent programs.
$(MATHBINARIES): %: %.o $(NETPBMLIBS)
	$(CC) $(LDFLAGS) -o $@ $@.o -lm $(NETPBMLIBS) $(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):
	cd $(PBMDIR) ; make $(LIBPBM)

libpgm.so: libpgm.so.$(MAJ).$(MIN)
	ln -sf libpgm.so.$(MAJ).$(MIN) libpgm.so
libpgm.so.$(MAJ).$(MIN): $(LIBOBJECTS)
	gcc -shared -Wl,-soname,libpgm.so.$(MAJ) -o libpgm.so.$(MAJ).$(MIN) \
	   $(LIBOBJECTS)

libpgm.a: $(LIBOBJECTS)
	-rm -f $@
	ar rc $@ $(LIBOBJECTS)
	-ranlib $@

pgmmerge:	pgmmerge.c $(MERGE_OBJECTS) $(NETPBMLIBS) 
	$(CC) $(CFLAGS) $(INCLUDE) $(LDFLAGS) -o $@ $@.c \
        $(MERGE_OBJECTS) -lm $(NETPBMLIBS)


.PHONY: install.bin
install.bin: $(BINARIES)
	cd $(INSTALLBINARIES) ; rm -f $(BINARIES)
	cp $(BINARIES) $(INSTALLBINARIES)
	cd $(INSTALLBINARIES) ; strip $(BINARIES)

.PHONY: install.merge
install.merge: pgmmerge
	cd $(INSTALLBINARIES) ; rm -f $(BINARIES)
	cp pgmmerge $(INSTALLBINARIES)
	cd $(INSTALLBINARIES) ; for i in $(BINARIES) ; do ln pgmmerge $$i ; done
	rm -f $(INSTALLBINARIES)/pgmmerge

.PHONY: install.lib
install.lib: libpgm.$(MAJ).$(MIN)
	cd $(INSTALLLIBS) ; rm -f libpgm.$(MAJ).*
	cp libpgm.$(MAJ).$(MIN) $(INSTALLLIBS)
	cd $(INSTALLBINARIES) ; ln -sf libpgm.$(MAJ).$(MIN) libpgm.so

.PHONY: install.man
install.man:
	for i in $(MANUALS1) ; do \
	    rm -f $(INSTALLMANUALS1)/$$i.$(SUFFIXMANUALS1) ; \
	    $(MANCP) $$i.1 $(INSTALLMANUALS1)/$$i.$(SUFFIXMANUALS1) ; \
	done
	for i in $(MANUALS3) ; do \
	    rm -f $(INSTALLMANUALS3)/$$i.$(SUFFIXMANUALS3) ; \
	    $(MANCP) $$i.3 $(INSTALLMANUALS3)/$$i.$(SUFFIXMANUALS3) ; \
	done
	for i in $(MANUALS5) ; do \
	    rm -f $(INSTALLMANUALS5)/$$i.$(SUFFIXMANUALS5) ; \
	    $(MANCP) $$i.5 $(INSTALLMANUALS5)/$$i.$(SUFFIXMANUALS5) ; \
	done

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

.PHONY: dep
dep Makefile.depend:
	$(CC) -MM $(INCLUDE) *.c >Makefile.depend

include Makefile.depend
