# Installation process:
#   If this Maple is older than version 3, we remove the "global" declarations
#	from gvplot.mapleV3.
#   In any case, edit its PATH setting to include the place where we're
#	installing binaries, to ensure it can find "togeomview" and "geomview".
#   If the user didn't tell us where to put the Maple libraries, then
#	try to run Maple and find the first directory on its library path.
#   Then, run Maple to produce the library, and place the resulting
#	"gvplot.m" file in the Maple library,
#	and the edited "gvplot" source file in <library>/src/gvplot.

GEOM = ../../../..
SHELL = /bin/sh

# "make install" uses ${MAPLE} to construct the library file gvplot.m

MAPLE = maple

#
# If you're somehow running this package not under a Geomview installation
# tree, this "include" will fail.  You could uncomment the following
# three lines, set BINDIR and MAPLE_LIB for your site,
# and comment out the "include" on the line below them.
#  INSTALL = inst() { rm -f $4/$5; cp $5 $4/$5; }; inst
#  BINDIR = directory-where-geomview-lives-e.g.-/usr/local/bin
#  MAPLE_LIB = maple-library-directory-e.g.-/usr/local/maple/lib
#
include ${GEOM}/makefiles/Makedefs.global

SRCS = gvplot.mapleV3 gvplot.mapleV4

DISTFILES = README.gvplot ${SRCS} remotegv Makefile

SRCMSG1 = No setting for MAPLE_LIB in .../makefiles/mk.site.${SITE}
SRCMSG2 = software

INSTMSG1 = must use:  ${MAKE} $@ MAPLE_LIB=/name-of-Maple-library-directory
INSTMSG2 = software since MAPLE_LIB not specified

# Leave this comment here; used in binary distribution:
# INSTALL = ${GEOM}/tools/install.bsd

RFILES: FILES

FILES: _always
	@echo ${DISTFILES} | tr ' ' '\12' > FILES


install:	install_self
	@PATH="$$PATH:${MAPLE_LIB}/../bin"; \
	 if [ -n "${MAPLE_LIB}" ]; then \
	    case "`echo 'interface(version); quit;' | ${MAPLE} -q 2>&1`" in \
	    *Release?3*) \
		${MAKE} install_v3 ;; \
	    *Release*4*) \
		${MAKE} install_v4 ;; \
	    SCG2) \
		${MAKE} install_v2 ;; \
	    *) \
		echo "Can't run 'maple' to install gvplot.m." >&2; \
		echo "To install it manually, see the instructions in README.gvplot." >&2; \
		test ;; \
	    esac || echo "Something went wrong; couldn't install Maple-to-OOGL in ${MAPLE_LIB}."; \
	else \
	    echo '*** Warning: ${SRCMSG1};' >&2; \
	    echo '***  not installing Maple-to-OOGL ${SRCMSG2}.' >&2; \
	fi

# From the geomutil/maple2oogl directory, install ourselves in the
# ${GEOM}/maple directory to simplify building the binary distribution.
# Put a Makefile almost like this one there, but don't let install depend on
# install_self there.

install_self:
	${INSTALL} -m 644 -F ${GEOM}/maple gvplot.mapleV3
	${INSTALL} -m 644 -F ${GEOM}/maple gvplot.mapleV4
	${INSTALL} -m 644 -F ${GEOM}/maple README.gvplot
	rm -f ${GEOM}/doc/gvplot.doc; cp README.gvplot ${GEOM}/doc/gvplot.doc
	${INSTALL} -m 644 -F ${GEOM}/maple remotegv
	-sed -e '/^include/d' \
	    -e 's/^GEOM.*=.*/GEOM = ../' \
	    -e '/^install:/s/install_self//' \
	    -e 's/[{]SRCMSG/{INSTMSG/' \
	    -e '/^# INSTALL.*=/s/^# *//' Makefile > ${GEOM}/maple/Makefile.new
	mv -f ${GEOM}/maple/Makefile.new ${GEOM}/maple/Makefile

make_v2:
	rm -f gvplot gvplot.m
	-sed -e '/^[ 	]*global/s/^/#/' \
	    -e '/default_gvdirectories.*:=.*`/s|`;|:${BINDIR}`|' \
	    gvplot.mapleV3 > gvplot
	echo 'read(`gvplot`); save(`gvplot.m`); quit' | ${MAPLE} -q

make_v3:
	rm -f gvplot gvplot.m
	-sed -e '/default_gvdirectories.*:=.*`/s|`;|:${BINDIR}`|' \
	    gvplot.mapleV3 > gvplot
	echo 'read(`gvplot`); save(`gvplot.m`); quit' | ${MAPLE} -q

make_v4:
	rm -f gvplot gvplot.m
	-sed -e '/default_gvdirectories.*:=.*`/s|`;|:${BINDIR}`|' \
	    gvplot.mapleV4 > gvplot
	echo 'read(`gvplot`); save(`gvplot.m`); quit' | ${MAPLE} -q

install_v2:	make_v2
install_v3:	make_v3
install_v4:	make_v4

install_v2 install_v3 install_v4:
	test -d ${MAPLE_LIB} || mkdir ${MAPLE_LIB}
	test -d ${MAPLE_LIB}/src || mkdir ${MAPLE_LIB}/src
	${INSTALL} -m 644 -O -F ${MAPLE_LIB} gvplot.m
	${INSTALL} -m 644 -O -F ${MAPLE_LIB}/src gvplot
	${INSTALL} -m 644 -O -F ${BINDIR} remotegv


distribution:
	(echo "-C"; \
	 echo "${GEOM}"; \
	 sed -e 's:^:makefiles/:' ${GEOM}/makefiles/FILES; \
	 sed -e 's:^:src/bin/geomutil/maple2oogl/:' FILES) | \
	gnutar cTfZ - gvplot.tar.Z

bindist:	install_self
	rm -f maple
	ln -s . maple
	(  echo README.gvplot; \
	   sed	-e '/Makefile/d' \
		-e '/README.gvplot/d' \
		-e 's:^:maple/:' FILES; \
	   echo "-C"; echo "${GEOM}"; echo "maple/Makefile") | \
	  gnutar cTfZ - maple2oogl.tar.Z
	rm -f maple

all:

clean:
	rm -f *.o *~ gvplot.V[1234].m gvplot gvplot.m gvplot.tar.Z

_always:
