# 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
# and uses the presence/absence of ${M2SRC} to determine whether we're
# running Maple V.3 or later (keep/discard "global" declarations).

MAPLE = maple
M2SRC = m2src


SRCS = gvplot.mapleV1+2 gvplot.mapleV3
LIBFILES = gvplot.V1.m gvplot.V2.m gvplot.V3.m

DISTFILES = README.gvplot ${SRCS} ${LIBFILES} 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:	
	@if [ -n "${MAPLE_LIB}" ]; then \
	    case "`type ${MAPLE}`#`type m2src`" in \
	    */*#*/*) \
		${MAKE} install_v3 ;; \
	    */*#*"not found") \
		${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: ${INSTMSG1};' >&2; \
	    echo '***  not installing Maple-to-OOGL ${INSTMSG2}.' >&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 README.gvplot
	rm -f ${GEOM}/maple/Makefile
	sed -e '/^include/d' \
	    -e 's/^GEOM.*=.*/GEOM = ../' \
	    -e '/^install:/s/install_self//' \
	    -e 's/[{]SRCMSG/{INSTMSG/' \
	    -e '/^# INSTALL.*=/s/^# *//' Makefile > ${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

install_v2:	make_v2
install_v3:	make_v3

install_v2 install_v3:
	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


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.mapleV1+2 gvplot.V[123].m gvplot gvplot.m gvplot.tar.Z

_always:
