# Makefile for pnm 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
CC =		gcc -ansi -pedantic -fpcc-struct-return
CFLAGS =	-O
#CFLAGS =	-g
#CFLAGS =	-g -O
TIFFDEF =	-DLIBTIFF
TIFFINC =       -I../libtiff
TIFFLIB =       ../libtiff/libtiff.a
TIFFBINARIES =  tifftopnm pnmtotiff
TIFFOBJECTS =   tifftopnm.o pnmtotiff.o
#LDFLAGS =	-s
LDFLAGS =	
INSTALLBINARIES =	/usr/new/pbm
INSTALLSCRIPTS =	$(INSTALLBINARIES)
INSTALLMANUALS1 =	/usr/man/man1
SUFFIXMANUALS1 =	1
INSTALLMANUALS3 =	/usr/man/man3
SUFFIXMANUALS3 =	3
INSTALLMANUALS5 =	/usr/man/man5
SUFFIXMANUALS5 =	5
MANCP =			cp

PPMDIR =	../ppm
INCLUDEPPM =	-I$(PPMDIR)
LIBPPM =	$(PPMDIR)/libppm.a
DEFPPM =	$(PPMDIR)/ppm.h
DEFLIBPPM =	$(PPMDIR)/libppm.h

PGMDIR =	../pgm
INCLUDEPGM =	-I$(PGMDIR)
LIBPGM =	$(PGMDIR)/libpgm.a
DEFPGM =	$(PGMDIR)/pgm.h
DEFLIBPGM =	$(PGMDIR)/libpgm.h

PBMDIR =	../pbm
INCLUDEPBM =	-I$(PBMDIR)
LIBPBM =	$(PBMDIR)/libpbm.a
DEFPBM =	$(PBMDIR)/pbm.h ../pbmplus.h
DEFLIBPBM =	$(PBMDIR)/libpbm.h

SHELL =		/bin/sh
INCLUDE =	-I.. $(INCLUDEPPM) $(INCLUDEPGM) $(INCLUDEPBM)
ALLCFLAGS =	$(CFLAGS) $(INCLUDE) $(TIFFDEF) $(TIFFINC)
LIBPNM =	libpnm.a

PORTBINARIES =	fitstopnm giftopnm \
		pnmalias pnmarith pnmcat pnmcomp pnmconvol pnmcrop \
		pnmcut pnmdepth pnmenlarge pnmfile pnmflip pnmhisteq \
		pnmhistmap pnminvert pnmnoraw pnmpad pnmpaste \
		pnmtile pnmtoddif pnmtofits pnmtops pnmtorast \
		pnmtosgi pnmtosir pnmtoxwd \
		rasttopnm sgitopnm sirtopnm xwdtopnm zeisstopnm
MATHBINARIES =	pnmgamma pnmnlfilt pnmrotate pnmscale pnmshear
BINARIES =      $(PORTBINARIES) $(MATHBINARIES) $(TIFFBINARIES)
SCRIPTS =	anytopnm pnmindex pnmmargin pnmsmooth pstopnm

PORTOBJECTS =	fitstopnm.o giftopnm.o \
		pnmalias.o pnmarith.o pnmcat.o pnmcomp.o pnmconvol.o \
		pnmcrop.o pnmcut.o pnmdepth.o pnmenlarge.o pnmfile.o \
		pnmflip.o pnmhisteq.o pnmhistmap.o pnminvert.o \
		pnmnoraw.o pnmpad.o pnmpaste.o pnmtile.o pnmtoddif.o \
		pnmtofits.o pnmtops.o pnmtorast.o pnmtosgi.o pnmtosir.o \
		pnmtoxwd.o rasttopnm.o sgitopnm.o sirtopnm.o xwdtopnm.o \
		zeisstopnm.o pnmgamma.o pnmnlfilt.o pnmrotate.o \
		pnmscale.o pnmshear.o
OBJECTS =	$(PORTOBJECTS) $(TIFFOBJECTS)

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


#all:		binaries
all:		merge
#install:	install.bin
install:	install.merge


binaries:	$(BINARIES)

install.bin:	binaries $(SCRIPTS)
	cd $(INSTALLBINARIES) ; rm -f $(BINARIES)
	cp $(BINARIES) $(INSTALLBINARIES)
	cd $(INSTALLSCRIPTS) ; rm -f $(SCRIPTS)
	cp $(SCRIPTS) $(INSTALLSCRIPTS)
	cd $(INSTALLSCRIPTS) ; chmod +x $(SCRIPTS)


merge:		pnmmerge
pnmmerge:	pnmmerge.c $(OBJECTS) $(LIBPNM) $(LIBPPM) $(LIBPGM) $(LIBPBM)
	$(CC) $(ALLCFLAGS) $(LDFLAGS) -o $@ $@.c $(OBJECTS) -lm $(LIBPNM) $(LIBPPM) $(LIBPGM) $(LIBPBM) $(TIFFLIB)

install.merge:	install.pnmmerge $(SCRIPTS)
install.pnmmerge:	pnmmerge
	cd $(INSTALLBINARIES) ; rm -f $(BINARIES)
	cp pnmmerge $(INSTALLBINARIES)
	cd $(INSTALLBINARIES) ; for i in $(BINARIES) ; do ln pnmmerge $$i ; done
	rm -f $(INSTALLBINARIES)/pnmmerge
	cd $(INSTALLSCRIPTS) ; rm -f $(SCRIPTS)
	cp $(SCRIPTS) $(INSTALLSCRIPTS)
	cd $(INSTALLSCRIPTS) ; chmod +x $(SCRIPTS)


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


# Rules for plain programs.
$(PORTBINARIES) $(TIFFBINARIES):	pnm.h $(DEFPPM) $(DEFPGM) $(DEFPBM) $(LIBPNM) $(LIBPPM) $(LIBPGM) $(LIBPBM) $(TIFFLIB)
	$(CC) $(ALLCFLAGS) $(LDFLAGS) -o $@ $@.c $(LIBPNM) $(LIBPPM) $(LIBPGM) $(LIBPBM) $(TIFFLIB)

# Rule for math-dependent programs.
$(MATHBINARIES):        pnm.h $(DEFPPM) $(DEFPGM) $(DEFPBM) $(LIBPNM) \
			$(LIBPPM) $(LIBPGM) $(LIBPBM)
	$(CC) $(ALLCFLAGS) $(LDFLAGS) -o $@ $@.c -lm $(LIBPNM) $(LIBPPM) $(LIBPGM) $(LIBPBM)

# Rule for objects.
$(OBJECTS):	pnm.h $(DEFPPM) $(DEFPGM) $(DEFPBM)
	$(CC) $(ALLCFLAGS) "-Dmain=$*_main" -c $*.c

# And libraries.
$(LIBPBM):
	cd $(PBMDIR) ; make lib
$(LIBPGM) FOO:
	cd $(PGMDIR) ; make lib
$(LIBPPM) BAR:
	cd $(PPMDIR) ; make lib
lib:		$(LIBPNM)
$(LIBPNM):	libpnm1.o libpnm2.o libpnm3.o libpnm4.o
	-rm -f $(LIBPNM)
	ar rc $(LIBPNM) libpnm1.o libpnm2.o libpnm3.o libpnm4.o
	-ranlib $(LIBPNM)

libpnm1.o:	pnm.h $(DEFPPM) $(DEFPGM) $(DEFPBM) libpnm1.c
	$(CC) $(ALLCFLAGS) -c libpnm1.c
libpnm2.o:	pnm.h $(DEFPPM) $(DEFPGM) $(DEFPBM) libpnm2.c $(DEFLIBPPM) \
		$(DEFLIBPGM) $(DEFLIBPBM)
	$(CC) $(ALLCFLAGS) -c libpnm2.c
libpnm3.o:	pnm.h $(DEFPPM) $(DEFPGM) $(DEFPBM) libpnm3.c $(DEFLIBPPM) \
		$(DEFLIBPGM) $(DEFLIBPBM)
	$(CC) $(ALLCFLAGS) -c libpnm3.c
libpnm4.o:	pnm.h $(DEFPPM) $(DEFPGM) $(DEFPBM) rast.h libpnm4.c
	$(CC) $(ALLCFLAGS) -c libpnm4.c

# Other dependencies.
fitstopnm fitstopnm.o:		fitstopnm.c
giftopnm giftopnm.o:		giftopnm.c
pnmalias pnmalias.o:		pnmalias.c
pnmarith pnmarith.o:		pnmarith.c
pnmcat pnmcat.o:		pnmcat.c
pnmcomp pnmcomp.o:		pnmcomp.c
pnmconvol pnmconvol.o:		pnmconvol.c
pnmcrop pnmcrop.o:		pnmcrop.c
pnmcut pnmcut.o:		pnmcut.c
pnmdepth pnmdepth.o:		pnmdepth.c
pnmenlarge pnmenlarge.o:	pnmenlarge.c
pnmfile pnmfile.o:		pnmfile.c
pnmflip pnmflip.o:		pnmflip.c
pnmgamma pnmgamma.o:		pnmgamma.c
pnmhistmap pnmhistmap.o:	pnmhistmap.c
pnminvert pnminvert.o:		pnminvert.c
pnmnoraw pnmnoraw.o:		pnmnoraw.c
pnmpad pnmpad.o:		pnmpad.c
pnmpaste pnmpaste.o:		pnmpaste.c
pnmrotate pnmrotate.o:		pnmrotate.c
pnmscale pnmscale.o:		pnmscale.c
pnmshear pnmshear.o:		pnmshear.c
pnmtile pnmtile.o:		pnmtile.c
pnmtoddif pnmtoddif.o:		pnmtoddif.c
pnmtofits pnmtofits.o:		pnmtofits.c
pnmtops pnmtops.o:		pnmtops.c
pnmtosgi pnmtosgi.o:		pnmtosgi.c sgi.h
pnmtosir pnmtosir.o:		pnmtosir.c
pnmtorast pnmtorast.o:		pnmtorast.c rast.h
pnmtotiff pnmtotiff.o:		pnmtotiff.c
pnmtoxwd pnmtoxwd.o:		pnmtoxwd.c x11wd.h
rasttopnm rasttopnm.o:		rasttopnm.c rast.h
sgitopnm sgitopnm.o:		sgitopnm.c sgi.h
sirtopnm sirtopnm.o:		sirtopnm.c
tifftopnm tifftopnm.o:		tifftopnm.c
xwdtopnm xwdtopnm.o:		xwdtopnm.c x10wd.h x11wd.h
zeisstopnm zeisstopnm.o:	zeisstopnm.c

clean:
	-rm -f *.o *.a *.cat *~ core $(BINARIES) pnmmerge
