# 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.
#
# Makefile modifications Copyright (c) 1997 by Wayne Gramlich.
#
# 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.  (Yes, it is exactly the same as the one above.)

# Included make files:
ROOT = ../../..
include $(ROOT)/Makefile_common.mk
include $(ROOT)/Makefile_private.mk

# Install directories:
BIN_DIRECTORY = $(ROOT)/bin/$(PLATFORM)
MAN_DIRECTORY = $(ROOT)/man
MAN1_DIRECTORY = $(MAN_DIRECTORY)/man1
MAN3_DIRECTORY = $(MAN_DIRECTORY)/man3
MAN5_DIRECTORY = $(MAN_DIRECTORY)/man5

# Source directories:
PGM_DIRECTORY = ../pgm
PBM_DIRECTORY = ../pbm

# Includes:
PGM_INCLUDE = -I$(PGM_DIRECTORY)
PBM_INCLUDE = -I$(PBM_DIRECTORY)
INCLUDES = -I.. $(PGM_INCLUDE) $(PBM_INCLUDE)

# Compiler flags:
# Default values, usually overridden by top-level Makefile.

#CC = cc
#CC = gcc -ansi -pedantic -fcombine-regs -fpcc-struct-return
CC = gcc -ansi -pedantic

#COPTs = -O
#COPTS = -g
#COPTS = -g -O
COPTS = -O2

RGBDEF = -DRGB_DB=\"/usr/lib/X11/rgb.txt\"

CFLAGS = ${COPTS} -DSYSV
ALLCFLAGS = ${CFLAGS} $(RGBDEF) ${INCLUDES}

# Linker flags:
#LDFLAGS =	-s
LDFLAGS =	

# Library files:
LIBPGM = $(PGM_DIRECTORY)/$(PLATFORM)/libpgm.a
LIBPBM = $(PBM_DIRECTORY)/$(PLATFORM)/libpbm.a
LIBPPM = $(PLATFORM)/libppm.a

RANLIB =	true

PORT_BASES =		\
	giftoppm	\
	gouldtoppm	\
	ilbmtoppm	\
	imgtoppm	\
	mtvtoppm	\
	pcxtoppm	\
	pgmtoppm	\
	pi1toppm	\
	picttoppm	\
	pjtoppm		\
	ppmdither	\
	ppmhist		\
	ppmmake		\
	ppmquant	\
	ppmrelief	\
	ppmtogif	\
	ppmtoicr	\
	ppmtoilbm	\
	ppmtopcx	\
	ppmtopgm	\
	ppmtopi1	\
	ppmtopict	\
	ppmtopj		\
	ppmtopuzz	\
	ppmtorgb3	\
	ppmtosixel	\
	ppmtotga	\
	ppmtouil	\
	ppmtoxpm	\
	ppmtoyuv	\
	qrttoppm	\
	rawtoppm	\
	rgb3toppm	\
	spctoppm	\
	sputoppm	\
	tgatoppm	\
	ximtoppm	\
	xpmtoppm	\
	yuvtoppm
MATH_BASES =		\
	ppmpat
BASES = ${PORT_BASES} ${MATH_BASES}
BINARIES = ${BASES}
C_SOURCES = ${BASE:%=%.c}
OBJECTS = ${BASES:%=$(PLATFORM)/%.o}

LIB_BASES =		\
	libppm1		\
	libppm2		\
	libppm3		\
	libppm4		\
	libppm5
LIB_C_SOURCES = ${LIB_BASES:%=%.c}
LIB_OBJECTS = ${LIB_BASES:%=$(PLATFORM)/%.o}

SCRIPTS = ppmquantall

HEADERS =		\
	ilbm.h		\
	libppm.h	\
	ppm.h		\
	ppmcmap.h	\
	ppmdraw.h	\
	tga.h		\
	xim.h
OTHER_SOURCES =		\
	Imakefile	\
	Makefile

MAN1_BASES = ${BINARIES} ${SCRIPTS}
MAN3_BASES = libppm
MAN5_BASES = ppm

ALL_C_SOURCES =			\
	${C_SOURCES}		\
	${LIB_C_SOURCES}
MAN_SOURCES =			\
	${MAN1_BASES:%=%.1}	\
	${MAN3_BASES:%=%.3}	\
	${MAN5_BASES:%=%.5}
SOURCES = 			\
	${ALL_C_SOURCES}	\
	${HEADERS}		\
	${MAN_SOURCES}		\
	${OTHER_SOURCES}
ALL_SOURCES =			\
	${SOURCES}		\
	index.html		\
	source.html


.PHONY: all copy install install.merge install.man clean site lib

all: merge
install: install.merge install.man

merge: $(PLATFORM) $(WAIT) $(PLATFORM)/ppmmerge
$(PLATFORM)/ppmmerge: ppmmerge.c $(LIBPPM) ${OBJECTS} $(LIBPGM) $(LIBPBM)
	$(CC) ${ALLCFLAGS} ${LDFLAGS} -o $@ ppmmerge.c ${OBJECTS} \
		-lm $(LIBPPM) $(LIBPGM) $(LIBPBM)

# Rules for objects:
${OBJECTS}:
	$(CC) ${ALLCFLAGS} "-Dmain=$(@:$(PLATFORM)/%.o=%_main)" \
		-c -o $@ $(@:$(PLATFORM)/%.o=%.c)
${LIB_OBJECTS}:
	$(CC) ${ALLCFLAGS} -c $(@:$(PLATFORM)/%.o=%.c) -o $@

# Rules for libraries:
$(LIBPBM):
	cd $(PBM_DIRECTORY) ; make lib
$(LIBPGM):
	cd $(PGM_DIRECTORY) ; make lib
lib: $(PLATFORM) $(WAIT) $(LIBPPM)
$(LIBPPM): ${LIB_OBJECTS}
	rm -f $(LIBPPM)
	ar rc $(LIBPPM) ${LIB_OBJECTS}
	$(RANLIB) $(LIBPPM)

# Platform directory:
$(PLATFORM):
	rm -rf $@
	mkdir $@

# Clean rules:
clean: site

copy: source.html
	set -e ;							\
	if [ "$(COPY_DIRECTORY)" = "" ] ;				\
	  then								\
	    echo "Please set COPY_DIRECTORY in `pwd`" ;			\
	    exit 1 ;							\
	  fi
	rm -rf $(COPY_DIRECTORY)
	mkdir -p $(COPY_DIRECTORY)
	cp ${ALL_SOURCES} $(COPY_DIRECTORY)

site:
	rm -rf $(PLATFORM)
	rm -f core `ls | grep "~$$"`

source.html: Makefile ${SOURCES}
	@echo -n "Generating $@..."
	@rm -f $@
	@echo '<HTML>' > $@
	@echo '<Head>' >> $@
	@echo '<Title>PBMPlus PPM Directory Source Files</Title>' >> $@
	@echo '</Head>' >> $@
	@echo '<Body>' >> $@
	@echo '<P>These are the source files for the' >> $@
	@echo '<A HRef="index.html">PBMPlus Download</A>' >> $@
	@echo 'top-level directory.</P>' >> $@
	@echo '<H1>' >> $@
	@echo 'PBMPlus PPM Directory Source Files' >> $@
	@echo '</H1>' >> $@
	@echo '<P>The following source files are available:</P>' >> $@
	@echo '<UL>' >> $@
	@for i in `ls ${SOURCES} | sort` ; do 				\
		echo "  <LI><A HRef='$$i'>$$i</A></LI>" >> $@ ;		\
		done
	@echo '</UL>' >> $@
	@echo '<HR>' >> $@
	@echo '<Address>' >> $@
	@echo 'This page is' >> $@
	@echo '<A HRef="../../../../copyright.html">' >> $@
	@echo 'Copyright</A> (c) 1998 by' >> $@
	@echo '<A HRef="../../../../gramlich.html">' >> $@
	@echo 'Wayne Gramlich</A> All rights reserved.' >> $@
	@echo '</Address>' >> $@
	@echo '</Body>' >> $@
	@echo '</HTML>' >> $@
	@echo "Done"


# Installation stuff:
install: install.merge install.man

install.merge: $(PLATFORM)/ppmmerge
	mkdir -p $(BIN_DIRECTORY)
	cd $(BIN_DIRECTORY) ; rm -f ${BASES}
	cp $(PLATFORM)/ppmmerge $(BIN_DIRECTORY)
	cd $(BIN_DIRECTORY) ;		\
	    for i in ${BASES} ; do	\
		ln -s ppmmerge $$i ;	\
	    done
	cd $(BIN_DIRECTORY) ; rm -f ${SCRIPTS}
	cp ${SCRIPTS} $(BIN_DIRECTORY)
	cd $(BIN_DIRECTORY) ; chmod +x ${SCRIPTS}

install.man:
	mkdir -p $(MAN_DIRECTORY)
	mkdir -p $(MAN_DIRECTORY)/man1
	for i in ${MAN1_BASES} ; do		\
	    rm -f $(MAN1_DIRECTORY)/$$i.1 ;	\
	    cp $$i.1 $(MAN1_DIRECTORY)/$$i.1 ;	\
	done
	mkdir -p $(MAN_DIRECTORY)/man3
	for i in ${MAN3_BASES} ; do		\
	    rm -f $(MAN3_DIRECTORY)/$$i.3 ;	\
	    cp $$i.3 $(MAN3_DIRECTORY)/$$i.3 ;	\
	done
	mkdir -p $(MAN_DIRECTORY)/man5
	for i in ${MAN5_BASES} ; do		\
	    rm -f $(MAN5_DIRECTORY)/$$i.5 ;	\
	    cp $$i.5 $(MAN5_DIRECTORY)/$$i.5 ;	\
	done

