# Makefile for pbm 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

# Includes:
INCLUDES = -I..

# 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

CFLAGS = ${COPTS} -DSYSV
RGBDEF = -DRGB_DB=\"/usr/lib/X11/rgb\"
TIFFDEF = -DLIBTIFF
#LDFLAGS = -s
LDFLAGS =	

RANLIB =	true
INCLUDES =	-I..
ALLCFLAGS =	${CFLAGS} $(RGBDEF) $(TIFFDEF) ${INCLUDES}

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

BASES =			\
	atktopbm	\
	brushtopbm	\
	cmuwmtopbm	\
	g3topbm		\
	icontopbm	\
	gemtopbm	\
	macptopbm	\
	mgrtopbm	\
	pbmlife		\
	pbmmake		\
	pbmmask		\
	pbmreduce	\
	pbmtext		\
	pbmto10x	\
	pbmtoascii	\
	pbmtoatk	\
	pbmtobbnbg	\
	pbmtocmuwm	\
	pbmtoepson	\
	pbmtog3		\
	pbmtogem	\
	pbmtogo		\
	pbmtoicon	\
	pbmtolj		\
	pbmtomacp	\
	pbmtomgr	\
	pbmtopi3	\
	pbmtoplot	\
	pbmtoptx	\
	pbmtox10bm	\
	pbmtoxbm	\
	pbmtoybm	\
	pbmtozinc	\
	pbmupc		\
	pi3topbm	\
	xbmtopbm	\
	ybmtopbm
C_SOURCES = ${BASES:%=%.c}
BINARIES = ${BASES:%=$(PLATFORM)/%}
OBJECTS = ${BASES:%=$(PLATFORM)/%.o}

LIB_BASES =		\
	libpbm1		\
	libpbm2		\
	libpbm3		\
	libpbm4		\
	libpbm5
LIB_C_SOURCES = ${LIB_BASES:%=%.c}
LIB_OBJECTS = ${LIB_BASES:%=$(PLATFORM)/%.o}

LIBPBM = $(PLATFORM)/libpbm.a

HEADERS =		\
	bitreverse.h	\
	cmuwm.h		\
	g3.h		\
	libpbm.h	\
	macp.h		\
	mgr.h		\
	pbm.h		\
	pbmfont.h

OTHER_SOURCES =		\
	Imakefile	\
	Makefile

MAN1_BASES = ${BASES}
MAN3_BASES = libpbm
MAN5_BASES = pbm

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 clean install lib install.merge install.man site

all: $(PLATFORM) $(WAIT) $(PLATFORM)/pbmmerge

$(PLATFORM)/pbmmerge: pbmmerge.c ${OBJECTS} $(LIBPBM)
	$(CC) ${ALLCFLAGS} ${LDFLAGS} -o $@ pbmmerge.c ${OBJECTS} $(LIBPBM)

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

lib: $(LIBPBM)
$(LIBPBM): ${LIB_OBJECTS}
	rm -f $@
	ar rc $@ ${LIB_OBJECTS}
	$(RANLIB) $@

$(PLATFORM):
	rm -rf $@
	mkdir $@

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 PBM 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 PBM 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)/pbmmerge
	mkdir -p $(BIN_DIRECTORY)
	cd $(BIN_DIRECTORY) ; rm -f ${BASES}
	cp $(PLATFORM)/pbmmerge $(BIN_DIRECTORY)
	cd $(BIN_DIRECTORY) ;		\
	    for i in ${BASES} ; do	\
		ln -s pbmmerge $$i ;	\
	    done

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



