#!/bin/sh
#
# Procedure to create PGPLOT makefile
#
#-----------------------------------------------------------------------

if test $# -lt 1; then
  echo "Usage: makemake pgplot_dist operating_system configuration"
  echo " pgplot_dist = The top level directory of the PGPLOT distribution."
  echo "               (ie. The directory in which makemake was found!)."
  echo " operating_system = Operating system name (omit this to get a list)."
  echo " configuration = Optional if there is only one for the specified OS."
  echo "                 (Otherwise you will be presented with a list)"
  exit 1
fi

SRC=$1

# Check for the required PGPLOT sub-directories.

for dir in drivers examples fonts src sys ; do
  if test ! -d $SRC/$dir; then
    echo "Failed to find required PGPLOT directory $SRC/$dir"
    echo "The first argument must name the PGPLOT root directory."
    exit 1
  fi
done

# The second argument is the name of the target operating system.

if test $# -ge 2 -a -d $SRC/sys_$2; then
  OS=$2
  SYSDIR=$SRC/sys_$OS
else
  echo 'The second argument must be one of the following operating systems:'
  cd $SRC
  echo "  `echo sys_* | sed 's/sys_//g'`"
  exit 1
fi

if test -r $SYSDIR/aaaread.me; then
  echo 'For additional information, read file' $SYSDIR/aaaread.me
fi

# The third argument is optional if there is only a single configuration
# but must be specified otherwise.

if test $# -ge 3 -a -f "$SYSDIR/$3.conf"; then
  CONFIG="$SYSDIR/$3.conf"
elif test $# -lt 3 -a -f local.conf; then
  CONFIG="local.conf"
elif test $# -lt 3 -a -f "`ls $SYSDIR/*.conf`"; then
  CONFIG="`ls $SYSDIR/*.conf`"
else
  cd $SRC/sys_$OS
  config_files=`echo *.conf`
  if test "$config_files" = '*.conf'; then
    echo "No configuration files found for $OS"
  else
    echo "The third argument for $OS must be one of the following configuration types:"
    for file in $config_files; do
      echo `echo $file | sed 's/\.conf//'` - `head -1 $file | sed 's/#//'`
    done
  fi
  exit 1
fi

# By default the PGPLOT library is initially placed in the current
# The demos should be explicitly linked against this library.

PGPLOT_LIB="-L\`pwd\` -lpgplot"
CPGPLOT_LIB="-L\`pwd\` -lcpgplot -lpgplot"

# List the default make targets.

DEFAULT_TARGETS="lib grfont.dat prog pgplot.doc"

# Get the configuration variables.

echo "Reading configuration file: $CONFIG"

. $CONFIG

# List the files that will need to be installed by the person who
# is running this script.

INSTALL_LIST="libpgplot.a $SHARED_LIB grfont.dat rgb.txt"

#-----------------------------------------------------------------------
# PGPLOT source directories.
#-----------------------------------------------------------------------
SRCDIR=$SRC/src
DEMDIR=$SRC/examples
FNTDIR=$SRC/fonts
DRVDIR=$SRC/drivers
PGDDIR=$SRC/pgdispd
GENDIR=$SRC/sys

#-----------------------------------------------------------------------
# Device drivers
#-----------------------------------------------------------------------
ARDRIV="ardriv.o"
BCDRIV="bcdriv.o"
CADRIV="cadriv.o"
CCDRIV="ccdriv.o"
CWDRIV="cwdriv.o"
EPDRIV="epdriv.o"
EXDRIV="exdriv.o"
GCDRIV="gcdriv.o"
GIDRIV="gidriv.o"
GLDRIV="gldriv.o"
GODRIV="godriv.o"
GRDRIV="grdriv.o"
GVDRIV="gvdriv.o"
HGDRIV="hgdriv.o"
HIDRIV="hidriv.o"
HJDRIV="hjdriv.o"
HPDRIV="hpdriv.o"
IKDRIV="ikdriv.o"
IMDRIV="imdriv.o"
IRDRIV="irdriv.o"
LADRIV="ladriv.o"
LHDRIV="lhdriv.o"
LIDRIV="lidriv.o"
LJDRIV="ljdriv.o"
LNDRIV="lndriv.o"
LSDRIV="lsdriv.o"
LVDRIV="lvdriv.o"
LXDRIV="lxdriv.o"
MFDRIV="mfdriv.o"
NEDRIV="nedriv.o nexsup.o"
NUDRIV="nudriv.o"
PGDRIV="pgdriv.o"
PKDRIV="pkdriv.o"
PPDRIV="ppdriv.o"
PSDRIV="psdriv.o"
PXDRIV="pxdriv.o"
PZDRIV="pzdriv.o"
QMDRIV="qmdriv.o"
SVDRIV="svdriv.o svblock.o"
TFDRIV="tfdriv.o"
TODRIV="todriv.o"
TTDRIV="ttdriv.o"
TXDRIV="txdriv.o"
VADRIV="vadriv.o"
VBDRIV="vbdriv.o"
VEDRIV="vedriv.o"
VIDRIV="vidriv.o"
VTDRIV="vtdriv.o"
WDDRIV="wddriv.o"
WSDRIV="wsdriv.o"
X2DRIV="x2driv.o figdisp_comm.o"
XEDRIV="xedriv.o"
XWDRIV="xwdriv.o"
ZEDRIV="zedriv.o"
XMDRIV="xmdriv.o pgxwin.o"
TKDRIV="tkdriv.o pgxwin.o"
PKDRIV="pkdriv.o pgxwin.o"
RVDRIV="rvdriv.o pgxwin.o"

# We need a drivers.list file in the current directory, from which to
# determine the drivers to be compiled.

if test -f drivers.list; then
  echo 'Selecting uncommented drivers from ./drivers.list'
else
  cp $SRC/drivers.list .
  echo 'Please specify required drivers by un-commenting them in ./drivers.list.'
  echo "Then re-run $0"
  exit 1
fi

# Get a list of driver names.

DRIV_LIST=`awk '/^[^!]/{printf("$%s\n", $1)}' drivers.list | sort | uniq`

echo "Found drivers `echo $DRIV_LIST | sed 's/\\\$//g'`"

# Convert the list of drivers to the list of dependent object files.
# This involves expanding the $xxDRIV driver-dependency variables,
# and removing duplicate object files.

DRIV_LIST=`eval echo $DRIV_LIST | awk '{for(i=1;i<=NF;i++) print $i}' | sort | uniq | awk '{printf("%s ", $0)} END{printf("\n")}'`

# Add server targets to the default target list if their respective
# drivers have been selected.

if (echo $DRIV_LIST | grep -s x2driv 2>&1 1>/dev/null); then
  DEFAULT_TARGETS="$DEFAULT_TARGETS pgdisp"
  INSTALL_LIST="$INSTALL_LIST pgdisp"
fi

if (echo $DRIV_LIST | grep -s xwdriv 2>&1 1>/dev/null); then
  DEFAULT_TARGETS="$DEFAULT_TARGETS pgxwin_server"
  INSTALL_LIST="$INSTALL_LIST pgxwin_server"
fi

if (echo $DRIV_LIST | grep -s nedriv 2>&1 1>/dev/null); then
  DEFAULT_TARGETS="$DEFAULT_TARGETS pgview.app"
  INSTALL_LIST="$INSTALL_LIST pgview.app"
fi

if (echo $DRIV_LIST | grep -s xmdriv 2>&1 1>/dev/null); then
  DEFAULT_TARGETS="$DEFAULT_TARGETS libXmPgplot.a pgmdemo"
  INSTALL_LIST="$INSTALL_LIST libXmPgplot.a XmPgplot.h"
fi

if (echo $DRIV_LIST | grep -s tkdriv 2>&1 1>/dev/null); then
  DEFAULT_TARGETS="$DEFAULT_TARGETS libtkpgplot.a pgtkdemo pgtkdemo.tcl"
  INSTALL_LIST="$INSTALL_LIST libtkpgplot.a tkpgplot.h"
fi

if (echo $DRIV_LIST | grep -s pkdriv 2>&1 1>/dev/null); then
  DEFAULT_TARGETS="$DEFAULT_TARGETS libptkpgplot.a "
  INSTALL_LIST="$INSTALL_LIST libptkpgplot.a"
fi

if (echo $DRIV_LIST | grep -s rvdriv 2>&1 1>/dev/null); then
  DEFAULT_TARGETS="$DEFAULT_TARGETS librvpgplot.a rvpgplot.h"
  INSTALL_LIST="$INSTALL_LIST librvpgplot.a rvpgplot.h"
fi

# Create a new grexec.f that calls the above drivers.

awk -f $SRC/grexec.awk drivers.list > grexec.f

# Some FORTRAN compilers expect their include files to appear in the
# directory containing the source code. Others expect them to be in the
# directory in which you actually do the compilation. To allow both cases
# copy the include files into the current directory.

cp $SRCDIR/*.inc .

#-----------------------------------------------------------------------
# Routine lists:
#   PG_ROUTINES: basic PGPLOT routines (Fortran-77).
#   PG_NON_STANDARD: non-Fortran-77 aliases for basic routines.
#   GR_ROUTINES: support routines, not called directly by applications
#	(Fortran-77).
#   SYSTEM_ROUTINES: potentially non-portable routines, usually
#	operating-system dependent.
#   OBSOLETE_ROUTINES: obsolete routines used by some programs.
#   DEMOS: demonstration programs
#-----------------------------------------------------------------------
PG_ROUTINES="\
 pgarro.o\
 pgask.o \
 pgaxis.o\
 pgaxlg.o\
 pgband.o\
 pgbbuf.o\
 pgbeg.o \
 pgbin.o \
 pgbox.o \
 pgbox1.o\
 pgcirc.o\
 pgcl.o  \
 pgclos.o\
 pgcn01.o\
 pgcnsc.o\
 pgconb.o\
 pgconf.o\
 pgconl.o\
 pgcons.o\
 pgcont.o\
 pgconx.o\
 pgcp.o  \
 pgctab.o\
 pgcurs.o\
 pgdraw.o\
 pgebuf.o\
 pgend.o \
 pgenv.o \
 pgeras.o\
 pgerr1.o\
 pgerrb.o\
 pgerrx.o\
 pgerry.o\
 pgetxt.o\
 pgfunt.o\
 pgfunx.o\
 pgfuny.o\
 pggray.o\
 pghi2d.o\
 pghis1.o\
 pghist.o\
 pghtch.o\
 pgiden.o\
 pgimag.o\
 pginit.o\
 pglab.o \
 pglcur.o\
 pgldev.o\
 pglen.o \
 pgline.o\
 pgmove.o\
 pgmtxt.o\
 pgncur.o\
 pgnoto.o\
 pgnpl.o \
 pgnumb.o\
 pgolin.o\
 pgopen.o\
 pgpage.o\
 pgpanl.o\
 pgpap.o \
 pgpixl.o\
 pgpnts.o\
 pgpoly.o\
 pgpt.o  \
 pgpt1.o \
 pgptxt.o\
 pgqah.o \
 pgqcf.o \
 pgqch.o \
 pgqci.o \
 pgqcir.o\
 pgqclp.o\
 pgqcol.o\
 pgqcr.o \
 pgqcs.o \
 pgqdt.o \
 pgqfs.o \
 pgqhs.o \
 pgqid.o \
 pgqinf.o\
 pgqitf.o\
 pgqls.o \
 pgqlw.o \
 pgqndt.o\
 pgqpos.o\
 pgqtbg.o\
 pgqtxt.o\
 pgqvp.o \
 pgqvsz.o\
 pgqwin.o\
 pgrect.o\
 pgrnd.o \
 pgrnge.o\
 pgsah.o \
 pgsave.o\
 pgscf.o \
 pgsch.o \
 pgsci.o \
 pgscir.o\
 pgsclp.o\
 pgscr.o \
 pgscrl.o\
 pgscrn.o\
 pgsfs.o \
 pgshls.o\
 pgshs.o \
 pgsitf.o\
 pgslct.o\
 pgsls.o \
 pgslw.o \
 pgstbg.o\
 pgsubp.o\
 pgsvp.o \
 pgswin.o\
 pgtbox.o\
 pgtext.o\
 pgtick.o\
 pgtikl.o\
 pgupdt.o\
 pgvect.o\
 pgvsiz.o\
 pgvstd.o\
 pgvw.o  \
 pgwedg.o\
 pgwnad.o\
"

PG_NON_STANDARD="\
 pgadvance.o\
 pgbegin.o  \
 pgcurse.o  \
 pglabel.o  \
 pgmtext.o  \
 pgncurse.o \
 pgpaper.o  \
 pgpoint.o  \
 pgptext.o  \
 pgvport.o  \
 pgvsize.o  \
 pgvstand.o \
 pgwindow.o \
"

GR_ROUTINES="\
 grarea.o\
 grbpic.o\
 grchsz.o\
 grclip.o\
 grclos.o\
 grclpl.o\
 grctoi.o\
 grcurs.o\
 grdot0.o\
 grdot1.o\
 grdtyp.o\
 gresc.o \
 grepic.o\
 gretxt.o\
 grfa.o  \
 grfao.o \
 grgfil.o\
 grgray.o\
 grimg0.o\
 grimg1.o\
 grimg2.o\
 grimg3.o\
 grinit.o\
 gritoc.o\
 grlen.o \
 grlin0.o\
 grlin1.o\
 grlin2.o\
 grlin3.o\
 grlina.o\
 grmcur.o\
 grmker.o\
 grmova.o\
 grmsg.o\
 gropen.o\
 grpage.o\
 grpars.o\
 grpixl.o\
 grpocl.o\
 grprom.o\
 grpxpo.o\
 grpxps.o\
 grpxpx.o\
 grpxre.o\
 grqcap.o\
 grqci.o \
 grqcol.o\
 grqcr.o \
 grqdev.o\
 grqdt.o \
 grqfnt.o\
 grqls.o \
 grqlw.o \
 grqpos.o\
 grqtxt.o\
 grqtyp.o\
 grquit.o\
 grrec0.o\
 grrect.o\
 grsci.o \
 grscr.o \
 grscrl.o\
 grsetc.o\
 grsets.o\
 grsfnt.o\
 grsize.o\
 grskpb.o\
 grslct.o\
 grsls.o \
 grslw.o \
 grsyds.o\
 grsymk.o\
 grsyxd.o\
 grterm.o\
 grtext.o\
 grtoup.o\
 grtrim.o\
 grtrn0.o\
 grtxy0.o\
 grvct0.o\
 grwarn.o\
 grxhls.o\
 grxrgb.o\
"

SYSTEM_ROUTINES="\
 grdate.o\
 grfileio.o\
 grflun.o\
 grgcom.o\
 grgenv.o\
 grgetc.o\
 grglun.o\
 grgmem.o\
 grgmsg.o\
 grlgtr.o\
 groptx.o\
 grsy00.o\
 grtermio.o\
 grtrml.o\
 grtter.o\
 gruser.o\
"
OBSOLETE_ROUTINES="\
 grchar.o\
 grchr0.o\
 grdat2.o\
 grgtc0.o\
 grinqfont.o\
 grinqli.o\
 grinqpen.o\
 grlinr.o\
 grmark.o\
 grmovr.o\
 grsetfont.o\
 grsetli.o\
 grsetpen.o\
 grtran.o\
 grvect.o\
 pgsetc.o\
 pgsize.o\
"

OPTIONAL_ROUTINES="\
 iand.o\
"

PGDISP_ROUTINES="\
 cleanup.o\
 pgdisp.o\
 figcurs.o\
 getdata.o\
 getvisuals.o\
 handlexevent.o\
 proccom.o\
 resdb.o\
 exposelgwin.o\
 getcolors.o\
 initlgluts.o\
 initlgwin.o\
 initlock.o\
 initwmattr.o\
 mainloop.o\
 resizelgwin.o\
 returnbuf.o\
 waitevent.o\
 updatelgtitle.o\
"

DEMOS="\
 pgdemo1\
 pgdemo2\
 pgdemo3\
 pgdemo4\
 pgdemo5\
 pgdemo6\
 pgdemo7\
 pgdemo8\
 pgdemo9\
 pgdemo10\
 pgdemo11\
 pgdemo12\
 pgdemo13\
 pgdemo14\
 pgdemo15\
 pgdemo16\
 pgdemo17\
"
#
# If any optional system routines are found, add them to the
# list of required system routines.
#

for file in `(echo $OPTIONAL_ROUTINES | sed 's/\.o//g')`; do
  if test -f $SYSDIR/${file}.c -o -f $SYSDIR/${file}.f; then
    SYSTEM_ROUTINES="$SYSTEM_ROUTINES ${file}.o"
  fi
done

#
# System specific C wrapper routines for the standard drivers are
# named ..wrap.c in the appropriate system directory, where .. is the
# two letter ..driv.c prefix of a given driver.
#

for file in `(echo $DRIV_LIST | sed 's/\.o//g')`; do
  if test -f $DRVDIR/${file}.c; then
    wrapper="`(echo $file | sed 's/driv$/wrap/')`"
    if test -f $SYSDIR/${wrapper}.c -o -f $SYSDIR/${wrapper}.m ; then
      SYSTEM_ROUTINES="$SYSTEM_ROUTINES ${wrapper}.o"
    fi
  fi
done

# Don't override any existing (possibly modified version of rgb.txt).

if test ! -f rgb.txt; then
  echo 'Copying color database.'
  cp $SRC/rgb.txt .
fi

echo "Creating make file: makefile"

cat > makefile << EOD
# Makefile for PGPLOT.
# This file is automatically generated. Do not edit.
#
# This generates the PGPLOT binary files (libraries and demos) in the
# current default directory (which need not be the source directory).
#-----------------------------------------------------------------------
SHELL=/bin/sh
#	PGPLOT subdirectories
SRC=$SRC
SRCDIR=$SRCDIR
DEMDIR=$DEMDIR
FNTDIR=$FNTDIR
DRVDIR=$DRVDIR
SYSDIR=$SYSDIR
PGDDIR=$PGDDIR
GENDIR=$GENDIR
XMDIR=$DRVDIR/xmotif
TKDIR=$DRVDIR/xtk
PTKDIR=$DRVDIR/ptk
#
# Fortran compiler and compilation flags
#
FCOMPL=$FCOMPL
FFLAGC=$FFLAGC
FFLAGD=$FFLAGD
#
# C compiler and compilation flags
#
XINCL=$XINCL
MOTIF_INCL=$MOTIF_INCL
TK_INCL=$TK_INCL
PTK_INCL=$PTK_INCL
RV_INCL=$RV_INCL
CCOMPL=$CCOMPL
CFLAGC=$CFLAGC
CFLAGD=$CFLAGD
MCOMPL=$MCOMPL
MFLAGC=$MFLAGC
#
# Pgbind flags.
#
PGBIND_FLAGS=$PGBIND_FLAGS
#
# Loader library-flags
#
LIBS=$LIBS
MOTIF_LIBS=$MOTIF_LIBS
TK_LIBS=$TK_LIBS
PTK_LIBS=$PTK_LIBS
#
#	Loader command for PGPLOT library
#
PGPLOT_LIB=$PGPLOT_LIB
CPGPLOT_LIB=$CPGPLOT_LIB
#
# Shared library creation.
#
SHARED_LIB=$SHARED_LIB
SHARED_LD=$SHARED_LD
#
# The libraries that the shared PGPLOT library depends upon.
# This is for systems that allow one to specify what libraries
# undefined symbols of a shared library reside in. Such systems
# (eg. Solaris 2.x) use this information at run time so that users of
# the library don't have to list a slew of other implementation-specific
# libraries when they link their executables.
#
SHARED_LIB_LIBS=$SHARED_LIB_LIBS
#
# Ranlib command if required
#
RANLIB=$RANLIB
#
# Routine lists.
#
PG_ROUTINES=$PG_ROUTINES
PG_NON_STANDARD=$PG_NON_STANDARD
GR_ROUTINES=$GR_ROUTINES
SYSTEM_ROUTINES=$SYSTEM_ROUTINES
OBSOLETE_ROUTINES=$OBSOLETE_ROUTINES
DRIVERS=$DRIV_LIST
PGDISP_ROUTINES=$PGDISP_ROUTINES
DEMOS=$DEMOS
#
#-----------------------------------------------------------------------
# Target "all" makes everything (except the library of obsolete routines)
#-----------------------------------------------------------------------
all: $DEFAULT_TARGETS
	@echo ' ';echo '*** Finished compilation of PGPLOT ***';echo ' '
	@echo 'Note that if you plan to install PGPLOT in a different'
	@echo 'directory than the current one, the following files will be'
	@echo 'needed.'
	@echo ' '
EOD
	for file in $INSTALL_LIST; do
	  echo "	@echo '       $file'"
	done >> makefile
cat >> makefile << EOD
	@echo ' '
	@echo 'Also note that subsequent usage of PGPLOT programs requires that'
	@echo 'the full path of the chosen installation directory be named in'
	@echo 'an environment variable named PGPLOT_DIR.'
	@echo ' '

#-----------------------------------------------------------------------
# Rules for compiling the .o files
#-----------------------------------------------------------------------
EOD

echo 'Determining object file dependencies.'

# List source code file dependencies explicitly.

for file in `(echo $PG_ROUTINES | sed 's/\.o//g')`; do
echo "${file}.o: \$(SRCDIR)/${file}.f"
echo "	\$(FCOMPL) -c \$(FFLAGC) \$(SRCDIR)/${file}.f"
done >> makefile

for file in `(echo $PG_NON_STANDARD | sed 's/\.o//g')`; do
echo "${file}.o: \$(SRCDIR)/${file}.f"
echo "	\$(FCOMPL) -c \$(FFLAGC) \$(SRCDIR)/${file}.f"
done >> makefile

for file in `(echo $GR_ROUTINES | sed 's/\.o//g')`; do
echo "${file}.o: \$(SRCDIR)/${file}.f"
echo "	\$(FCOMPL) -c \$(FFLAGC) \$(SRCDIR)/${file}.f"
done >> makefile

for file in `(echo $SYSTEM_ROUTINES | sed 's/\.o//g')`; do
  if test -f $SYSDIR/${file}.f; then
    echo "${file}.o: \$(SYSDIR)/${file}.f"
    echo "	\$(FCOMPL) -c \$(FFLAGC) \$(SYSDIR)/${file}.f"
  elif test -f $SYSDIR/${file}.c; then
    echo "${file}.o: \$(SYSDIR)/${file}.c"
    echo "	\$(CCOMPL) -c \$(CFLAGC) \$(SYSDIR)/${file}.c"
  elif test -f $SYSDIR/${file}.m; then
    echo "${file}.o: \$(SYSDIR)/${file}.m"
    echo "	\$(MCOMPL) -c \$(MFLAGC) \$(SYSDIR)/${file}.m"
  elif test -f $GENDIR/${file}.f; then
    echo "${file}.o: \$(GENDIR)/${file}.f"
    echo "	\$(FCOMPL) -c \$(FFLAGC) \$(GENDIR)/${file}.f"
  elif test -f $GENDIR/${file}.c; then
    echo "${file}.o: \$(GENDIR)/${file}.c"
    echo "	\$(CCOMPL) -c \$(CFLAGC) \$(GENDIR)/${file}.c"
  else
    echo "Warning: Unable to find source code for ${file}.o in either $SYSDIR or $GENDIR" 1>&2
  fi
done >> makefile

for file in `(echo $OBSOLETE_ROUTINES | sed 's/\.o//g')`; do
echo "${file}.o: \$(SRCDIR)/${file}.f"
echo "	\$(FCOMPL) -c \$(FFLAGC) \$(SRCDIR)/${file}.f"
done >> makefile

# Emit pgdisp rules.

for file in `(echo $PGDISP_ROUTINES | sed 's/\.o//g')`; do
echo "${file}.o: \$(PGDDIR)/${file}.c"
echo "	\$(CCOMPL) -c \$(CFLAGC) \$(XINCL) -DPGDISP \$(PGDDIR)/${file}.c"
done >> makefile

# Emit driver dependencies.

for file in `(echo $DRIV_LIST | sed 's/\.o//g')`; do
  if test -f $SYSDIR/${file}.f; then
    echo "${file}.o: \$(SYSDIR)/${file}.f"
    echo "	\$(FCOMPL) -c \$(FFLAGC) \$(SYSDIR)/${file}.f"
  elif test -f $SYSDIR/${file}.c; then
    echo "${file}.o: \$(SYSDIR)/${file}.c"
    echo "	\$(CCOMPL) -c \$(CFLAGC) \$(XINCL) \$(SYSDIR)/${file}.c"
  elif test -f $SYSDIR/${file}.m; then
    echo "${file}.o: \$(SYSDIR)/${file}.m"
    echo "	\$(MCOMPL) -c \$(MFLAGC) \$(XINCL) \$(SYSDIR)/${file}.m"
  elif test -f $DRVDIR/${file}.f; then
    echo "${file}.o: \$(DRVDIR)/${file}.f"
    echo "	\$(FCOMPL) -c \$(FFLAGC) \$(DRVDIR)/${file}.f"
  elif test -f $DRVDIR/${file}.c; then
    echo "${file}.o: \$(DRVDIR)/${file}.c"
    echo "	\$(CCOMPL) -c \$(CFLAGC) \$(XINCL) \$(DRVDIR)/${file}.c"
  elif test -f $DRVDIR/${file}.m; then
    echo "${file}.o: \$(DRVDIR)/${file}.m"
    echo "	\$(MCOMPL) -c \$(MFLAGC) \$(XINCL) \$(DRVDIR)/${file}.m"
  else
    echo "Unable to find source code for ${file}.o in $SYSDIR or $DRVDIR/" 1>&2
    exit 1
  fi
done >> makefile

cat >> makefile << \EOD

#-----------------------------------------------------------------------
# The device-driver dispatch routine is generated automatically by
# reading the "drivers.list" file.
#-----------------------------------------------------------------------

DISPATCH_ROUTINE=grexec.o

grexec.o: grexec.f
	$(FCOMPL) -c $(FFLAGC) grexec.f

#-----------------------------------------------------------------------
# Target "lib" is used to built the PGPLOT subroutine library.
# libpgplot.a is the primary PGPLOT object library.
# "shared" is an optional target for operating systems that allow shared
# libraries.
#-----------------------------------------------------------------------

lib : libpgplot.a $(SHARED_LIB)

libpgplot.a : $(PG_ROUTINES) $(PG_NON_STANDARD) $(GR_ROUTINES) \
	   $(DISPATCH_ROUTINE) $(DRIVERS) $(SYSTEM_ROUTINES)
	ar ru libpgplot.a \
		`ls $(PG_ROUTINES) \
		$(PG_NON_STANDARD) $(GR_ROUTINES) $(DISPATCH_ROUTINE) \
		$(DRIVERS) $(SYSTEM_ROUTINES) | sort | uniq`
	$(RANLIB) libpgplot.a

EOD

# Emit the shared library dependency if requested.

if test -n "$SHARED_LIB" -a -n "$SHARED_LD"; then
cat >> makefile << \EOD
$(SHARED_LIB): $(PG_ROUTINES) $(PG_NON_STANDARD) \
	$(GR_ROUTINES) $(DISPATCH_ROUTINE) $(DRIVERS) $(SYSTEM_ROUTINES)
	$(SHARED_LD) `ls $(PG_ROUTINES) \
	$(PG_NON_STANDARD) $(GR_ROUTINES) $(DISPATCH_ROUTINE) \
	$(DRIVERS) $(SYSTEM_ROUTINES) | sort | uniq` $(SHARED_LIB_LIBS)
EOD
fi

cat >> makefile << \EOD

#-----------------------------------------------------------------------
# libpgobs.a contains obsolete routines used by some programs
#-----------------------------------------------------------------------
libpgobs.a : $(OBSOLETE_ROUTINES)
	ar ru libpgobs.a $(OBSOLETE_ROUTINES)
	$(RANLIB) libpgobs.a

#-----------------------------------------------------------------------
# Target "prog" is used to make the demo programs. They can also be made
# individually.
#-----------------------------------------------------------------------
prog: $(DEMOS)

EOD

# If the default pgplot library is shareable then the demos do not
# need to be recompiled. If there is no shareable library then they
# do, so add an extra dependency.

if test ! \( -n "$SHARED_LIB" -a -n "$SHARED_LD" \); then
  EXTRA_DEPENDENCY="libpgplot.a"
fi

for file in $DEMOS; do
echo "${file}: \$(DEMDIR)/${file}.f $EXTRA_DEPENDENCY"
echo "	\$(FCOMPL) \$(FFLAGD) -o ${file} \$(DEMDIR)/${file}.f \$(PGPLOT_LIB) \$(LIBS)"
done >> makefile

cat >> makefile << \EOD

#-----------------------------------------------------------------------
# Target "grfont.dat" is the binary font file.
# This is created from grfont.txt with the "pgpack" program.
#    (a) compile the `pgpack' program; then
#    (b) run `pgpack' to convert the ASCII version of the font file
#    (grfont.txt) into the binary version (grfont.dat). When executed,
#    `pgpack' should report:
#	Characters defined:   996
#	Array cells used:   26732
#-----------------------------------------------------------------------

EOD

(
  if test -f $SYSDIR/pgpack.f; then
    echo "grfont.dat: \$(FNTDIR)/grfont.txt \$(SYSDIR)/pgpack.f"
    echo "	\$(FCOMPL) \$(FFLAGC) -o pgpack \$(SYSDIR)/pgpack.f"
  elif test -f $SYSDIR/pgpack.c; then
    echo "grfont.dat: \$(FNTDIR)/grfont.txt \$(SYSDIR)/pgpack.c"
    echo "	\$(CCOMPL) \$(CFLAGC) -o pgpack \$(SYSDIR)/pgpack.c"
  elif test -f $SYSDIR/pgpack.m; then
    echo "grfont.dat: \$(FNTDIR)/grfont.txt \$(SYSDIR)/pgpack.m"
    echo "	\$(MCOMPL) \$(MFLAGC) -o pgpack \$(SYSDIR)/pgpack.m"
  else
    echo "grfont.dat: \$(FNTDIR)/grfont.txt \$(FNTDIR)/pgpack.f"
    echo "	\$(FCOMPL) \$(FFLAGC) -o pgpack \$(FNTDIR)/pgpack.f"
  fi
  echo "	rm -f grfont.dat"
  echo "	./pgpack <\$(FNTDIR)/grfont.txt"
  echo "	rm -f pgpack"
) >> makefile

cat >> makefile << \EOD

#-----------------------------------------------------------------------
# Documentation files
#-----------------------------------------------------------------------

EOD

PG_SOURCE=`echo " $PG_ROUTINES $PG_NON_STANDARD" | sed -e 's|\.o|\.f|g' -e 's| pg| $(SRCDIR)/pg|g'`
(
 echo "PG_SOURCE=$PG_SOURCE"
 echo "pgplot.doc: \$(PG_SOURCE)"
 echo "	$SRC/makedoc \$(PG_SOURCE) > pgplot.doc"
 echo "pgplot.html: \$(PG_SOURCE)"
 echo "	$SRC/makehtml \$(PG_SOURCE) > pgplot.html"
 echo "pgplot.hlp: \$(PG_SOURCE)"
 echo "	$SRC/makehelp \$(PG_SOURCE) > pgplot.hlp"
 echo "pgplot-routines.tex: \$(PG_SOURCE)"
 echo "	$SRC/maketex \$(PG_SOURCE) > pgplot-routines.tex"
) >> makefile

cat >> makefile << \EOD

#-----------------------------------------------------------------------
# Target "pgxwin_server" is the server program for the XW driver
#-----------------------------------------------------------------------
pgxwin_server: $(DRVDIR)/pgxwin_server.c
	$(CCOMPL) $(CFLAGC) $(XINCL) -o pgxwin_server $(DRVDIR)/pgxwin_server.c $(LIBS)

#-----------------------------------------------------------------------
# Target "pgdisp" is the pgdisp server program for /XDISP driver
#-----------------------------------------------------------------------
pgdisp: $(PGDISP_ROUTINES)
	$(CCOMPL) $(CFLAGC) -o pgdisp $(PGDISP_ROUTINES) $(LIBS)

#-----------------------------------------------------------------------
# Target "pgview.app" is the server program for the NeXT NE driver
#-----------------------------------------------------------------------
pgview.app:
	(cd $(SRC)/sys_next/pgview; make)
	cp $(SRC)/sys_next/pgview/pgview.app .

#-----------------------------------------------------------------------
# Target "libxmpgplot.a" contains the Motif widget driver.
#-----------------------------------------------------------------------

libXmPgplot.a: XmPgplot.o
	ar ru libXmPgplot.a XmPgplot.o
	$(RANLIB) libXmPgplot.a

XmPgplot.h: $(XMDIR)/XmPgplot.h 
	cp $(XMDIR)/XmPgplot.h XmPgplot.h

XmPgplot.o: $(DRVDIR)/pgxwin.h XmPgplot.h $(XMDIR)/XmPgplotP.h $(XMDIR)/XmPgplot.c
	$(CCOMPL) -c $(CFLAGC) -I$(DRVDIR) -I$(XMDIR) $(MOTIF_INCL) $(XMDIR)/XmPgplot.c

#-----------------------------------------------------------------------
# Target "libtkpgplot.a" contains the Tk widget driver.
#-----------------------------------------------------------------------

libtkpgplot.a: tkpgplot.o
	ar ru libtkpgplot.a tkpgplot.o
	$(RANLIB) libtkpgplot.a

tkpgplot.h: $(TKDIR)/tkpgplot.h 
	cp $(TKDIR)/tkpgplot.h tkpgplot.h

tkpgplot.o: $(DRVDIR)/pgxwin.h tkpgplot.h $(TKDIR)/tkpgplot.c
	$(CCOMPL) -c $(CFLAGC) -I$(DRVDIR) -I$(TKDIR) $(TK_INCL) $(TKDIR)/tkpgplot.c

#-----------------------------------------------------------------------
# Target "libptkpgplot.a" contains the Perl/Tk widget driver.
#-----------------------------------------------------------------------

libptkpgplot.a: ptkpgplot.o
	ar ru libptkpgplot.a ptkpgplot.o
	$(RANLIB) libptkpgplot.a

ptkpgplot.h: $(PTKDIR)/ptkpgplot.h 
	cp $(PTKDIR)/ptkpgplot.h ptkpgplot.h

ptkpgplot.o: $(DRVDIR)/pgxwin.h ptkpgplot.h $(PTKDIR)/ptkpgplot.c
	$(CCOMPL) -c -DUSE_PERL $(CFLAGC) -I$(DRVDIR) -I$(PTKDIR) $(PTK_INCL) $(PTKDIR)/ptkpgplot.c

#-----------------------------------------------------------------------
# Target "librvpgplot.a" contains the Rivet-Tk widget driver.
#-----------------------------------------------------------------------

librvpgplot.a: rvpgplot.o
	ar ru librvpgplot.a rvpgplot.o
	$(RANLIB) librvpgplot.a

rvpgplot.h: $(TKDIR)/rvpgplot.h
	cp $(TKDIR)/rvpgplot.h rvpgplot.h

rvpgplot.o: $(DRVDIR)/pgxwin.h rvpgplot.h $(TKDIR)/tkpgplot.c
	$(CCOMPL) -o $@ -c -DUSE_RIVET $(CFLAGC) -I$(DRVDIR) -I$(TKDIR) $(RV_INCL) $(TKDIR)/tkpgplot.c

#-----------------------------------------------------------------------
# Target "install" is required for Figaro.
#-----------------------------------------------------------------------
install:

#-----------------------------------------------------------------------
# Target "clean" is used to remove all the intermediate files.
#-----------------------------------------------------------------------
clean :
	-@rm -f $(PG_ROUTINES) $(PG_NON_STANDARD) $(GR_ROUTINES)\
		$(DISPATCH_ROUTINE) $(DRIVERS) $(SYSTEM_ROUTINES)\
		$(OBSOLETE_ROUTINES) $(PGDISP_ROUTINES) pgmdemo.o\
		XmPgplot.o pgbind tkpgplot.o pgtkdemo.o ptkpgplot.o rvpgplot.o

#-----------------------------------------------------------------------
# Include file dependencies
#-----------------------------------------------------------------------
EOD

(
  cd $SRCDIR
  echo "# The following routines reference pgplot.inc"
  echo
  echo `grep -l pgplot.inc *.f | sed "s/\.f/\.o/g"` : "\$(SRCDIR)/pgplot.inc"
  echo
  echo "# The following routines reference grpckg1.inc"
  echo
  echo `grep -l grpckg1.inc *.f | sed "s/\.f/\.o/g"` : "\$(SRCDIR)/grpckg1.inc"
  echo
) >> makefile

cat >> makefile << \EOD

# Miscellaneous include files required by drivers

griv00.o : $(DRVDIR)/gadef.h $(DRVDIR)/gmdef.h $(DRVDIR)/gphdef.h
grivas.o : $(DRVDIR)/gadef.h
grtv00.o : $(DRVDIR)/imdef.h
pgxwin.o : $(DRVDIR)/pgxwin.h

x2driv.o figdisp_comm.o: $(DRVDIR)/commands.h


cpg:	libcpgplot.a cpgplot.h cpgdemo
	@echo ' '
	@echo '*** Finished compilation of the C PGPLOT wrapper library ***'
	@echo ' '
	@echo 'Note that if you plan to install the library in a different'
	@echo 'directory than the current one, both libcpgplot.a and cpgplot.h'
	@echo 'will be needed.'
	@echo ' '

pgbind: $(SRC)/cpg/pgbind.c
	$(CCOMPL) $(CFLAGC) $(SRC)/cpg/pgbind.c -o pgbind

libcpgplot.a cpgplot.h: $(PG_SOURCE) pgbind
	./pgbind $(PGBIND_FLAGS) -h -w $(PG_SOURCE)
	$(CCOMPL) -c $(CFLAGC) cpg*.c
	rm -f cpg*.c
	ar ru libcpgplot.a cpg*.o
	$(RANLIB) libcpgplot.a
	rm -f cpg*.o

cpgdemo: cpgplot.h $(SRC)/cpg/cpgdemo.c libcpgplot.a
	$(CCOMPL) $(CFLAGD) -c -I. $(SRC)/cpg/cpgdemo.c
	$(FCOMPL) -o cpgdemo cpgdemo.o $(CPGPLOT_LIB) $(LIBS)
	rm -f cpgdemo.o

pgmdemo: pgmdemo.o libXmPgplot.a
	$(FCOMPL) -o pgmdemo pgmdemo.o -L`pwd` -lXmPgplot $(CPGPLOT_LIB) $(MOTIF_LIBS)

pgmdemo.o: $(XMDIR)/pgmdemo.c XmPgplot.h libcpgplot.a cpgplot.h
	$(CCOMPL) $(CFLAGD) -c -I`pwd` $(MOTIF_INCL) $(XMDIR)/pgmdemo.c

pgtkdemo: pgtkdemo.o libtkpgplot.a
	$(FCOMPL) -o pgtkdemo pgtkdemo.o -L`pwd` -ltkpgplot $(CPGPLOT_LIB) $(TK_LIBS)

pgtkdemo.tcl: $(TKDIR)/pgtkdemo.tcl
	cp $(TKDIR)/pgtkdemo.tcl pgtkdemo.tcl
	chmod a+x pgtkdemo.tcl

pgtkdemo.o: $(TKDIR)/pgtkdemo.c tkpgplot.h libcpgplot.a cpgplot.h
	$(CCOMPL) $(CFLAGD) -c -I`pwd` $(TK_INCL) $(TKDIR)/pgtkdemo.c
EOD
