# 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
#CC =		gcc
CC =		gcc -ansi -pedantic -fpcc-struct-return
CFLAGS =	-O
#CFLAGS =	-g
#CFLAGS =	-g -O
LDFLAGS =	-s
#LDFLAGS =	
INSTALLBINARIES =	/usr/new/pbm
INSTALLMANUALS1 =	/usr/man/man1
SUFFIXMANUALS1 =	1
INSTALLMANUALS3 =	/usr/man/man3
SUFFIXMANUALS3 =	3
INSTALLMANUALS5 =	/usr/man/man5
SUFFIXMANUALS5 =	5
MANCP =			cp

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

SHELL =		/bin/sh
INCLUDE =	-I.. $(INCLUDEPBM)
ALLCFLAGS =	$(CFLAGS) $(INCLUDE)
LIBPGM =	libpgm.a

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

OBJECTS =	asciitopgm.o bioradtopgm.o fstopgm.o hipstopgm.o \
		lispmtopgm.o pbmtopgm.o pgmbentley.o pgmenhance.o pgmhist.o pgmkernel.o \
		pgmnoise.o pgmnorm.o pgmoil.o pgmramp.o pgmtofs.o pgmtolispm.o \
		pgmtopbm.o psidtopgm.o spottopgm.o \
		rawtopgm.o pgmcrater.o pgmedge.o pgmtexture.o

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


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


binaries:	$(BINARIES)

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


merge:		pgmmerge
pgmmerge:	pgmmerge.c $(OBJECTS) $(LIBPGM) $(LIBPBM)
	$(CC) $(ALLCFLAGS) $(LDFLAGS) -o $@ $@.c $(OBJECTS) -lm $(LIBPGM) $(LIBPBM)

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


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):	pgm.h $(DEFPBM) $(LIBPGM) $(LIBPBM)
	$(CC) $(ALLCFLAGS) $(LDFLAGS) -o $@ $@.c $(LIBPGM) $(LIBPBM)

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

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

# And libraries.
$(LIBPBM):
	cd $(PBMDIR) ; make lib
lib:		$(LIBPGM)
$(LIBPGM):	libpgm1.o libpgm2.o
	-rm -f $(LIBPGM)
	ar rc $(LIBPGM) libpgm1.o libpgm2.o
	-ranlib $(LIBPGM)

libpgm1.o:	pgm.h $(DEFPBM) libpgm.h libpgm1.c
	$(CC) $(ALLCFLAGS) -c libpgm1.c
libpgm2.o:	pgm.h $(DEFPBM) libpgm.h libpgm2.c $(DEFLIBPBM)
	$(CC) $(ALLCFLAGS) -c libpgm2.c

# Other dependencies.
bioradtopgm bioradtopgm.o:	bioradtopgm.c
asciitopgm asciitopgm.o:	asciitopgm.c
fstopgm fstopgm.o:		fstopgm.c
hipstopgm hipstopgm.o:		hipstopgm.c
lispmtopgm lispmtopgm.o:	lispmtopgm.c
pbmtopgm pbmtopgm.o:		pbmtopgm.c
pgmbentley pgmbentley.o:	pgmbentley.c
pgmcrater pgmcrater.o:		pgmcrater.c
pgmedge pgmedge.o:		pgmedge.c
pgmenhance pgmenhance.o:	pgmenhance.c
pgmhist pgmhist.o:		pgmhist.c
pgmkernel pgmkernel.o:		pgmkernel.c
pgmnoise pgmnoise.o:		pgmnoise.c
pgmnorm pgmnorm.o:		pgmnorm.c
pgmoil pgmoil.o:		pgmoil.c
pgmramp pgmramp.o:		pgmramp.c
pgmtexture pgmtexture.o:	pgmtexture.c
pgmtopbm pgmtopbm.o:		pgmtopbm.c dithers.h $(DEFPBM)
pgmtofs pgmtofs.o:		pgmtofs.c
pgmtolispm pgmtolispm.o:	pgmtolispm.c
psidtopgm psidtopgm.o:		psidtopgm.c
rawtopgm rawtopgm.o:		rawtopgm.c
spottopgm spottopgm.o:		spottopgm.c

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