# file: Makefile	G. Moody		10 March 2001
#			Last revised:	      18 October 2002
# This file is used to compile the programs used by `plt'.
#
# Under Unix or Linux, set the site-dependent variables below, edit `plt',
# `lwcat', and `gvcat' as appropriate for your system, then type `make install'
# to compile and install `plt' and its associated programs.
#
# MS-Windows users, please read and follow the instructions in `../INSTALL'.
# You *must* use the free Cygwin C compiler and tools -- MSVC and other
# commercial compilers *will not work*.  See `../INSTALL' for details.
# ----------------------------------------------------------------------------
# Site-dependent variables:

# PREFIX is the root of the tree into which everything is ultimately
# installed.
PREFIX=/usr/local

# INSTALL_PREFIX is usually the same as PREFIX, but 'rpm' or 'rpmbuild' resets
# it to the root of a temporary build tree when making a set of RPMs.  We
# need to distinguish PREFIX from INSTALL_PREFIX because the absolute pathname
# (starting with PREFIX) of the installed copy of 'plt.pro' needs to be
# inserted into 'lwcat' when doing a 'make install' (see below).
INSTALL_PREFIX=$(PREFIX)

# BINDIR is the directory into which the executables and shell scripts are
# to be installed.  You must have write permission in BINDIR to complete the
# installation successfully.
BINDIR = $(INSTALL_PREFIX)/bin

# MAN1DIR is the directory in which man pages for applications are kept.
MAN1DIR = $(INSTALL_PREFIX)/share/man/man1

# PSPDIR is the directory in which PostScript prologs are kept.  You will also
# need write permission in PSPDIR.
PSPDIR = $(INSTALL_PREFIX)/lib/ps

# XINCDIR is the directory in which the X11 header (*.h) files are kept.  Only
# read permission is required in XINCDIR.
XINCDIR = /usr/X11R6/include

# XLIBDIR is the directory in which the X11 client libraries are kept.  Only
# read permission is required in XLIBDIR.
XLIBDIR = /usr/X11R6/lib

# CFLAGS is a set of C compiler options.  These may include:
#  -g		   (to save debugging symbols)
#  -O		   (to optimize; usually cannot be used together with -g)
#  -DDEBUG	   (to enable special debugging code; see plt.h)
#  -DNO_VSNPRINTF  (if your C library is missing vsnprintf;  but note that
#	 	    the alternative, using vsprintf, is a security problem,
#		    and that it's better to update your C library if you can)
CFLAGS = -O

# TARGETS is a list of the executables to be compiled by default.  It may
# include any of:
#  lwplt	plt for LaserWriter (and other PostScript devices)
#  makeplthead	generates PostScript bounding box for lwplt
#  xplt		plt for X11
#  xpltwin	window-creation program needed by xplt
TARGETS = lwplt makeplthead xplt xpltwin

# PRINT is the command to be used by `make listing'.
PRINT = enscript -2r

# It should not be necessary to modify anything below this line.
# ----------------------------------------------------------------------------

OBJ = main.o data.o plot.o axis.o tick.o figure.o window.o \
	text.o option.o util.o pterm.o
SRC = main.c data.c plot.c axis.c tick.c figure.c window.c \
	text.c option.c util.c pterm.c lw.c makeplthead.c xpltwin.c xw.c 
INC = plt.h rgb.h
ADM = Makefile gvcat lwcat plt plt.pro pltpng

# `make all' (or just `make') compiles the targets but does not install them.
# You must have write permission in this directory, but nothing more is needed.
all:		lwplt makeplthead
	if [ -d $(XINCDIR) ]; then $(MAKE) xplt xpltwin; fi
	@echo "plt has been compiled successfully.  Install it by typing:"
	@echo "    make install"

# `make install' compiles the targets if necessary, then installs the `plt'
# script and the targets in BINDIR.  It edits `lwcat' to set the correct path
# name for `plt.pro', and installs `lwcat' and `gvcat' in BINDIR and
# `plt.pro' in PSPDIR.  You must have write permission in BINDIR and PSPDIR.
install:	all
	-mkdir -p $(BINDIR)
	-mkdir -p $(MAN1DIR)
	-mkdir -p $(PSPDIR)
	if [ -d $(XINCDIR) ]; then cp xplt xpltwin $(BINDIR); fi
	cp lwplt makeplthead plt pltpng $(BINDIR)
	( if [ `uname | cut -c1-6` = CYGWIN ]; \
	    then cp gvcat.msw $(BINDIR)/gvcat; \
	    else cp gvcat $(BINDIR)/gvcat; \
          fi )
	sed s%PSPDIR%$(PREFIX)/lib/ps% <lwcat >$(BINDIR)/lwcat
	chmod 755 $(BINDIR)/lwcat
	cp plt.pro $(PSPDIR)
	cp ../doc/plt.1 $(MAN1DIR)
	gzip -f $(MAN1DIR)/plt.1
	chmod 644 $(MAN1DIR)/plt.1.gz
	cd ../misc; $(MAKE) INSTALL_PREFIX=$(INSTALL_PREFIX) install
	@echo "plt has been installed successfully.  Test it by typing:"
	@( if [ -s $(BINDIR)/xplt ]; \
	  then echo "    make xdemo"; \
	       echo "in an X terminal window, or by typing:"; fi )
	@echo "    make psdemo"

# 'make mswplt' (for MS-Windows) is now the same as 'make install'.
mswplt:		install

# 'make uninstall':  remove all installed files
uninstall:
	cd $(BINDIR); rm -f ftable gvcat lwplt makeplthead plt pltf pltpng \
	  xplt xpltwin
	rm -f $(MAN1DIR)/plt.1.gz $(LIBDIR)/plt.pro

# PostScript driver for plt
lwplt:		$(OBJ) lw.o
	$(CC) $(CFLAGS) -o $@ $(OBJ) lw.o -lm

# Utility for calculating PostScript bounding boxes
makeplthead:	makeplthead.c
	$(CC) $(CFLAGS) -o $@ makeplthead.c

# X11 driver for plt
xplt:		$(OBJ) xw.o
	$(CC) $(CFLAGS) -o $@ $(OBJ) xw.o -L$(XLIBDIR) -lX11 -lm

# Utility that creates an X11 window in which xplt draws
xpltwin:	plt.h xpltwin.c
	$(CC) $(CFLAGS) -I$(XINCDIR) -o $@ xpltwin.c -L$(XLIBDIR) -lX11

# Rules for compiling *.o files needed by lwplt and xplt.
$OBJ:		plt.h

lw.o:		plt.h rgb.h
	$(CC) $(CFLAGS) -c lw.c

xw.o:		plt.h
	$(CC) $(CFLAGS) -I$(XINCDIR) -c xw.c

# Print a listing of plt sources.
listing:
	$(PRINT) $(INC) $(SRC) $(ADM)

# Print a copy of the plt Tutorial and Cookbook (requires that lpr
# be able to print a PostScript document;  see ../doc/Makefile).
doc:
	cd ../doc; make hardcopy

# Show figures from the plt Tutorial and Cookbook in an X window.
xdemo:		xplt xpltwin
	cd ../doc; make xdemo

# Show figures from the plt Tutorial and Cookbook in a GhostScript window.
psdemo:		lwplt makeplthead
	cd ../doc; make psdemo

# Remove binary and backup files.
clean:
	rm -f $(TARGETS) *.exe *.o *~ core
