# 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.
#
# Makefile modifications by 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.)

ROOT = ../../..
include $(ROOT)/Makefile_common.mk
include $(ROOT)/Makefile_private.mk

PBM_DIRECTORY = ../pbm
LIBPBM = $(PBM_DIRECTORY)/$(PLATFORM)/libpbm.a

# 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
#LDFLAGS = -s
LDFLAGS =

RANLIB = true
INCLUDE_PBM = -I$(PBM_DIRECTORY)
INCLUDES = -I.. $(INCLUDE_PBM)
ALLCFLAGS = ${CFLAGS} ${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

LIBPGM = $(PLATFORM)/libpgm.a

PORT_BASES =		\
	fitstopgm	\
	fstopgm		\
	hipstopgm	\
	lispmtopgm	\
	pgmbentley	\
	pgmenhance	\
	pgmhist		\
	pgmnorm		\
	pgmoil		\
	pgmramp		\
	pgmtofits	\
	pgmtofs		\
	pgmtolispm	\
	pgmtopbm	\
	psidtopgm	\
	rawtopgm
MATH_BASES =		\
	pgmedge		\
	pgmtexture
BASES = ${PORT_BASES} ${MATH_BASES}
BINARIES = ${BASES}
C_SOURCES = ${BASES:%=%.c}
OBJECTS = ${BASES:%=$(PLATFORM)/%.o}

LIB_BASES =		\
	libpgm1		\
	libpgm2
LIB_C_SOURCES = ${LIB_BASES:%=%.c}
LIB_OBJECTS = ${LIB_BASES:%=$(PLATFORM)/%.o}

HEADERS = 		\
	dithers.h	\
	libpgm.h	\
	newdithers.h	\
	pgm.h

OTHER_SOURCES =		\
	Imakefile	\
	Makefile
	
MAN1_BASES = ${BASES}
MAN3_BASES = libpgm
MAN5_BASES = pgm

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

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

install: install.merge install.man

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

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

# And libraries.
$(LIBPBM):
	cd $(PBMDIR) ; make lib
lib: $(LIBPGM)
$(LIBPGM): ${LIB_OBJECTS}
	rm -f $(LIBPGM)
	ar rc $(LIBPGM) ${LIB_OBJECTS}
	$(RANLIB) $(LIBPGM)

$(PLATFORM)/libpgm1.o: libpgm1.c
	$(CC) ${ALLCFLAGS} -c libpgm1.c -o $@
$(PLATFORM)/libpgm2.o: libpgm2.c
	$(CC) ${ALLCFLAGS} -c libpgm2.c -o $@

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

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)

clean: site

source.html: Makefile ${SOURCES}
	@echo -n "Generating $@..."
	@rm -f $@
	@echo '<HTML>' > $@
	@echo '<Head>' >> $@
	@echo '<Title>PBMPlus PGM 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 PGM 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"
	
site:
	rm -rf $(PLATFORM)
	rm -f core `ls | grep "~$$"`

install.merge: $(PLATFORM)/pgmmerge
	cd $(BIN_DIRECTORY) ; rm -f $(BINARIES) pgmmerge
	cp $(PLATFORM)/pgmmerge $(BIN_DIRECTORY)
	cd $(BIN_DIRECTORY) ;		\
	    for i in ${BASES} ; do	\
		ln -s pgmmerge $$i ;	\
	    done

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











