# Makefile for ppm 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
#LDFLAGS =	-s
LDFLAGS =	
INSTALLBINARIES =	/usr/new/pbm
INSTALLSCRIPTS =	$(INSTALLBINARIES)
INSTALLMANUALS1 =	/usr/man/mann
SUFFIXMANUALS1 =	1
INSTALLMANUALS3 =	/usr/man/mann
SUFFIXMANUALS3 =	3
INSTALLMANUALS5 =	/usr/man/mann
SUFFIXMANUALS5 =	5
MANCP =			cp

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.. $(INCLUDEPGM) $(INCLUDEPBM)
ALLCFLAGS =	$(CFLAGS) $(INCLUDE)
LIBPPM =	libppm.a

PORTBINARIES =	bmptoppm gouldtoppm hpcdtoppm ilbmtoppm imgtoppm \
		mtvtoppm pcxtoppm pgmtoppm pi1toppm picttoppm \
		pjtoppm \
		ppm3d ppmbrighten ppmchange ppmdim ppmdist ppmdither \
		ppmflash ppmhist ppmmake ppmmix ppmnorm \
		ppmntsc ppmquant ppmrelief ppmshift ppmspread \
		ppmtoacad \
		ppmtobmp ppmtogif ppmtoicr ppmtoilbm ppmtomitsu \
		ppmtopcx ppmtopgm ppmtopi1 ppmtopict ppmtopj \
		ppmtopjxl ppmtopuzz ppmtorgb3 ppmtosixel \
		ppmtotga ppmtouil ppmtoxpm ppmtoyuv \
		ppmtoyuvsplit \
		qrttoppm rawtoppm rgb3toppm sldtoppm spctoppm \
		sputoppm tgatoppm ximtoppm xpmtoppm xvminitoppm \
		yuvtoppm yuvsplittoppm
MATHBINARIES =	ppmforge ppmpat ppmqvga ppmtomap
BINARIES =	$(PORTBINARIES) $(MATHBINARIES)
SCRIPTS =	ppmquantall

OBJECTS =	bmptoppm.o gouldtoppm.o hpcdtoppm.o ilbmtoppm.o imgtoppm.o \
		mtvtoppm.o pcxtoppm.o pgmtoppm.o pi1toppm.o picttoppm.o \
		pjtoppm.o \
		ppm3d.o ppmbrighten.o ppmchange.o ppmdim.o ppmdist.o ppmdither.o \
		ppmflash.o ppmhist.o ppmmake.o ppmmix.o ppmnorm.o \
		ppmntsc.o ppmquant.o ppmrelief.o ppmshift.o ppmspread.o \
		ppmtoacad.o \
		ppmtobmp.o ppmtogif.o ppmtoicr.o ppmtoilbm.o ppmtomitsu.o \
		ppmtopcx.o ppmtopgm.o ppmtopi1.o ppmtopict.o ppmtopj.o \
		ppmtopjxl.o ppmtopuzz.o ppmtorgb3.o ppmtosixel.o \
		ppmtotga.o ppmtouil.o ppmtoxpm.o ppmtoyuv.o \
		ppmtoyuvsplit.o \
		qrttoppm.o rawtoppm.o rgb3toppm.o sldtoppm.o spctoppm.o \
		sputoppm.o tgatoppm.o ximtoppm.o xpmtoppm.o xvminitoppm.o \
		yuvtoppm.o yuvsplittoppm.o \
		ppmforge.o ppmpat.o ppmqvga.o ppmtomap.o

MANUALS1 =	$(BINARIES) $(SCRIPTS)
MANUALS3 =	libppm
MANUALS5 =	ppm


#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:		ppmmerge
ppmmerge:	ppmmerge.c $(OBJECTS) $(LIBPPM) $(LIBPGM) $(LIBPBM)
	$(CC) $(ALLCFLAGS) $(LDFLAGS) -o $@ $@.c $(OBJECTS) -lm $(LIBPPM) $(LIBPGM) $(LIBPBM)

install.merge:	install.ppmmerge $(SCRIPTS)
install.ppmmerge:	ppmmerge
	cd $(INSTALLBINARIES) ; rm -f $(BINARIES)
	cp ppmmerge $(INSTALLBINARIES)
	cd $(INSTALLBINARIES) ; for i in $(BINARIES) ; do ln ppmmerge $$i ; done
	rm -f $(INSTALLBINARIES)/ppmmerge
	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


# Rule for plain programs.
$(PORTBINARIES):	ppm.h $(DEFPGM) $(DEFPBM) $(LIBPPM) $(LIBPGM) $(LIBPBM)
	$(CC) $(ALLCFLAGS) $(LDFLAGS) -o $@ $@.c $(LIBPPM) $(LIBPGM) $(LIBPBM)

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

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

# And libraries.
$(LIBPBM):
	cd $(PBMDIR) ; make lib
$(LIBPGM):
	cd $(PGMDIR) ; make lib
lib:		$(LIBPPM)
$(LIBPPM):	libppm1.o libppm2.o libppm3.o libppm4.o libppm5.o bitio.o
	-rm -f $(LIBPPM)
	ar rc $(LIBPPM) libppm1.o libppm2.o libppm3.o libppm4.o \
	libppm5.o bitio.o
	-ranlib $(LIBPPM)

libppm1.o:	ppm.h $(DEFPGM) $(DEFPBM) libppm.h libppm1.c
	$(CC) $(ALLCFLAGS) -c libppm1.c
libppm2.o:	ppm.h $(DEFPGM) $(DEFPBM) libppm.h libppm2.c $(DEFLIBPGM) \
		$(DEFLIBPBM)
	$(CC) $(ALLCFLAGS) -c libppm2.c
libppm3.o:	ppm.h $(DEFPGM) $(DEFPBM) ppmcmap.h libppm.h libppm3.c
	$(CC) $(ALLCFLAGS) -c libppm3.c
libppm4.o:	ppm.h $(DEFPGM) $(DEFPBM) libppm4.c
	$(CC) $(ALLCFLAGS) -c libppm4.c
libppm5.o:	ppm.h $(DEFPGM) $(DEFPBM) ppmdraw.h libppm5.c
	$(CC) $(ALLCFLAGS) -c libppm5.c
bitio.o:	../pbmplus.h bitio.c
	$(CC) $(ALLCFLAGS) -c bitio.c

# Other dependencies.
bmptoppm bmptoppm.o:		bmptoppm.c bitio.o bitio.h bmp.h
gouldtoppm gouldtoppm.o:	gouldtoppm.c
hpcdtoppm hpcdtoppm.o:		hpcdtoppm.c
ilbmtoppm ilbmtoppm.o:		ilbmtoppm.c ilbm.h
imgtoppm imgtoppm.o:		imgtoppm.c
mtvtoppm mtvtoppm.o:		mtvtoppm.c
pcxtoppm pcxtoppm.o:		pcxtoppm.c
pgmtoppm pgmtoppm.o:		pgmtoppm.c
pi1toppm pi1toppm.o:		pi1toppm.c
picttoppm picttoppm.o:		picttoppm.c
pjtoppm pjtoppm.o:		pjtoppm.c
ppm3d ppm3d.o:			ppm3d.c
ppmbrighten ppmbrighten.o:	ppmbrighten.c
ppmchange ppmchange.o:		ppmchange.c
ppmdim ppmdim.o:		ppmdim.c
ppmdist ppmdist.o:		ppmdist.c ppmcmap.h
ppmdither ppmdither.o:		ppmdither.c
ppmflash ppmflash.o:		ppmflash.c
ppmforge ppmforge.o:		ppmforge.c
ppmhist ppmhist.o:		ppmhist.c ppmcmap.h
ppmmake ppmmake.o:		ppmmake.c
ppmmix ppmmix.o:		ppmmix.c
ppmnorm ppmnorm.o:		ppmnorm.c lum.h
ppmntsc ppmntsc.o:		ppmntsc.c
ppmpat ppmpat.o:		ppmpat.c ppmdraw.h
ppmquant ppmquant.o:		ppmquant.c $(PGMDIR)/dithers.h ppmcmap.h
ppmqvga ppmqvga.o:		ppmqvga.c
ppmrelief ppmrelief.o:		ppmrelief.c
ppmshift ppmshift.o:		ppmshift.c
ppmspread ppmspread.o:		ppmspread.c
ppmtoacad ppmtoacad.o:		ppmtoacad.c ppmcmap.h autocad.h
ppmtobmp ppmtobmp.o:		ppmtobmp.c bitio.h bmp.h
ppmtogif ppmtogif.o:		ppmtogif.c ppmcmap.h
ppmtoicr ppmtoicr.o:		ppmtoicr.c ppmcmap.h
ppmtoilbm ppmtoilbm.o:		ppmtoilbm.c ilbm.h ppmcmap.h
ppmtomitsu ppmtomitsu.o:	ppmtomitsu.c ppmcmap.h mitsu.h
ppmtomap ppmtomap.o:		ppmtomap.c ppmcmap.h
ppmtopcx ppmtopcx.o:		ppmtopcx.c ppmcmap.h
ppmtopgm ppmtopgm.o:		ppmtopgm.c
ppmtopi1 ppmtopi1.o:		ppmtopi1.c ppmcmap.h
ppmtopict ppmtopict.o:		ppmtopict.c ppmcmap.h
ppmtopj ppmtopj.o:		ppmtopj.c
ppmtopjxl ppmtopjxl.o:		ppmtopjxl.c ppmcmap.h
ppmtopuzz ppmtopuzz.o:		ppmtopuzz.c ppmcmap.h
ppmtorgb3 ppmtorgb3.o:		ppmtorgb3.c
ppmtosixel ppmtosixel.o:	ppmtosixel.c ppmcmap.h
ppmtotga ppmtotga.o:		ppmtotga.c ppmcmap.h
ppmtouil ppmtouil.o:		ppmtouil.c ppmcmap.h
ppmtoxpm ppmtoxpm.o:		ppmtoxpm.c ppmcmap.h
ppmtoyuv ppmtoyuv.o:		ppmtoyuv.c
ppmtoyuvsplit ppmtoyuvsplit.o:	ppmtoyuvsplit.c
qrttoppm qrttoppm.o:		qrttoppm.c
rawtoppm rawtoppm.o:		rawtoppm.c
rgb3toppm rgb3toppm.o:		rgb3toppm.c
sldtoppm sldtoppm.o:		sldtoppm.c ppmdraw.h autocad.h
spctoppm spctoppm.o:		spctoppm.c
sputoppm sputoppm.o:		sputoppm.c
tgatoppm tgatoppm.o:		tgatoppm.c tga.h
ximtoppm ximtoppm.o:		ximtoppm.c xim.h
xpmtoppm xpmtoppm.o:		xpmtoppm.c
xvminitoppm xvminitoppm.o:	xvminitoppm.c
yuvtoppm yuvtoppm.o:		yuvtoppm.c
yuvsplittoppm yuvsplittoppm.o:	yuvsplittoppm.c

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